Class, Constructor & Instance
Classes are like blueprints of information that can be created in one place and accessed and reused in different areas. The initial block of code containing the properties and logic is the class, while the ones referenced and used in other places are called instances. Say you have a building plan class
, and different contractors come for copies to construct buildings. Their buildings become instances
of your blueprint class
, and whatever changes or materials they use for their buildings do not affect your blueprint except in special cases.
// A Person class
class Person {
//properties of the Person class
final String name;
final int age;
}
Constructors, on the other hand, are unique methods, usually with the name of the class, and their job is to create an instance of the class. The constructor is called anytime you create instances of the class. And it determines what properties and values an object will have when created. There are three types of constructors, namely
Default Constructor; they have no parameters.
class Person { Person() { // constructor body } }
A Parameterized Constructor takes parameters used to set the value of instance variables.
void main() { // Creating Instance of class Person 11 = new Person("parameterized"); } // Creating Class named Person class Person{ // Creating parameterized Constructor Shot(String name) { print("This is the $name constructor"); } }
Named constructors: solve the problem of having numerous constructors with the same name. It allows you to create several constructors, each with its name.
// Creating Class Person
class Person{
// Creating parameterized Constructor named detaiilConstructor
Person.detailConstructor(String name) {
print("$name lives on the highland.");
}
// Creating default constructor named detailedConstructor2
Person.detailConstructor2() {
print("Maria is a Flutter developer");
}
}
void main() {
// Creating Instance of class
Person p1 = new Person.detailConstructor("Maria");
Person p2 = new Person.detailConstructor2();
}
SUBCLASSING
Subclasses inherit the properties and some of the functionalities of the main class. By default, normal functions in a class
are inherited by subclasses. A superclass (main class) can be accessed with the keyword super
in your subclass.
void main() {
final v = Vehicle(4);
print(v.toString());
print(Car());
print(Bicycle());
}
// Creating a Vehicle class
class Vehicle {
final int wheelCount;
const Vehicle(this.wheelCount);
@override
String toString() {
return '$runtimeType with $wheelCount wheels.';
}
}
// Car a subclass of Vehicle
class Car extends Vehicle {
const Car() : super(4);
}
class Bicycle extends Vehicle {
const Bicycle() : super(2);
}
Methods
Methods are functions implemented in a class and can be used to perform specific actions on instances of that class.
void main() {
final car = Car(); // an instance car of the Car class
car.drive(speed: 29); // drive method is called
print('Speed is ${car.speed}');
car.drive(speed: 31);
car.stop(); // stop method is called
}
// A class called car
class Car {
int speed = 0;
// A drive method requiring an int speed
void drive({
required int speed,
}) {
this.speed = speed;
print('Accelerating to $speed km/h');
}
// A stop method for stopping the car
void stop() {
speed = 0;
print('Stopping...');
print('Stopped');
}
}
Getters And Setters In Classes
In Dart, a getter is a method that retrieves the value of a private variable. It permits you to access the value of a variable without giving direct access to the variable from outside of the object. If the getter retrieves the value of a private variable, then a setter is a method that sets the value of a private variable of an object. The setter allows you to change the value of a variable in a controlled way rather than allowing unrestricted access to the variable.
void main() {
Student s1 = new Student();
s1.stud_name = 'Nitin';
s1.stud_age = 0;
print(s1.stud_name);
print(s1.stud_age);
}
class Student {
String name;
int age;
// a getter to retrieve name
String get stud_name {
return name;
}
// setter to set stud_name
void set stud_name(String name) {
this.name = name;
}
void set stud_age(int age) {
if(age <= 0) {
print("Age should be greater than 5");
} else {
this.age = age;
}
}
int get stud_age {
return age;
}
}