Dart Classes and Objects are the fundamental concepts of object-oriented programming language.
A Class is a user-defined blueprint or prototype which is used to create objects.
An Object represents real life entities. Objects determine the behaviour of a class.
As we said classes are blueprints. Consider a real-life object a Car maybe. So it has some properties or attributes like colour, name, model and some methods too like Drive, Break. We can sum up all the properties and methods of a car in a Car Class.
Another real-life example. Consider a Person. He has a name, age, address and so many other properties. He has some things to do which are methods or function like work, sleep. This all can be covered up in a Person Class.
Dart Class
The syntax of declaring dart class.
class class_name { // Properties (Variables) // Constructor // Methods or Functions // Getters and Setters }
Sample Code
class Car { //properties String colour, name; //Methods void driving() {} void brake() {} }
A class has been created with the name Car. “class” is the keyword. It has two properties colour and name. Car class also have two functions named driving and brake.
Dart Object
In Dart, we can create objects with and without the new keyword. There is no need to write new keyword.
var car = Car();
Syntax of accessing properties and methods
// Accessing Properties objectName.property_name; // Accessing Methods objectName.method_name();
Sample Code
// Accessing properties car.name; car.colour; // Accessing methods car.driving(); car.brake();
We can create as many objects as we want. For now to understand the concepts. Let’s create two objects and share code and the output. To print value like ${firstCar.name} within a string I am using String interpolation.
void main() { var firstCar = Car(); firstCar.name = 'Prius'; firstCar.colour = 'White'; print('1st car name is ${firstCar.name} and colour is ${firstCar.colour}'); firstCar.driving(); var secondCar = Car(); secondCar.name = 'Honda Fit'; secondCar.colour = 'Black'; print('2nd car name is ${secondCar.name} and colour is ${secondCar.colour}'); secondCar.brake(); } class Car { //properties String colour, name; //Methods void driving() { print('Car is driving'); } void brake() { print('Brake has been applied'); } }
Output
1st car name is Prius and colour is White Car is driving 2nd car name is Honda Fit and colour is Black Brake has been applied Process finished with exit code 0
So far we are accessing Car class properties in the main function. What if we want to print the value within the class.
Sample Code
void main() { var firstCar = Car(); firstCar.name = 'Prius'; firstCar.colour = 'White'; // print('1st car name is ${firstCar.name} and colour is ${firstCar.colour}'); firstCar.driving(); var secondCar = Car(); secondCar.name = 'Honda Fit'; secondCar.colour = 'Black'; // print('2nd car name is ${secondCar.name} and colour is ${secondCar.colour}'); secondCar.brake(); } class Car { //properties String colour, name; //Methods void driving() { print('${this.colour} ${this.name} is driving'); } void brake() { print('${this.colour} ${this.name} is not driving'); } }
Output
I have committed the output code in the main function and printing the attributes within the Car class.
White Prius is driving Black Honda Fit is not driving
Dart Constructors
Now we have pretty much idea of Dart Classes and Objects. Constructors are also part of the class.
A Constructor is used to initialize the object of a class. Each class has a constructor. If a Constructor is not declared a no arguments default constructor is provided by the compiler.
Dart Parameterized Constructor
The syntax of Constructor in Dart
Class_name(parameter_list) { //constructor body }
Sample Code
Car(String col, String na) { colour = col; name = na; }
Dart Classes Objects and Constructors with Example
void main() { var firstCar = Car('Prius', 'White'); firstCar.driving(); var secondCar = Car('Honda Fit', 'Black'); secondCar.brake(); } class Car { //properties String colour, name; Car(String col, String na) { colour = col; name = na; } //Methods void driving() { print('${this.colour} ${this.name} is driving'); } void brake() { print('${this.colour} ${this.name} is not driving'); } }
Output
Prius White is driving Honda Fit Black is not driving
Well in Dart we can optimize the constructor code as well. Let me write the constructor code in a much better way. The code is trimmed.
Car(this.colour, this.name);
Run the code
void main() { var firstCar = Car('Prius', 'White'); firstCar.driving(); var secondCar = Car('Honda Fit', 'Black'); secondCar.brake(); } class Car { //properties String colour, name; Car(this.colour, this.name); //Methods void driving() { print('${this.colour} ${this.name} is driving'); } void brake() { print('${this.colour} ${this.name} is not driving'); } } /*Output Prius White is driving Honda Fit Black is not driving*/
Dart Name Constructor
Well, In Dart we can create our own named constructor too. The syntax is very simple. Just write the class name then dot and name for the constructor. The named constructor can be empty and can be parameterized.
Here is the syntax.
Class.constructorName(){ //Body }
Example from our current code. I will use a custom parameterized constructor. customCon is the custom constructor and we have called it by Class name followed by custom constructor name Car.customCon(“Grey”, “Toyota Camry”);
void main() { var firstCar = Car('Prius', 'White'); firstCar.driving(); var secondCar = Car('Honda Fit', 'Black'); secondCar.brake(); var thirdCar = Car.customCon("Grey", "Toyota Camry"); } class Car { //properties String colour, name; Car(this.colour, this.name); Car.customCon(this.colour, this.name) { print('Custom Car name is ${this.name} and color is ${this.colour}'); } //Methods void driving() { print('${this.colour} ${this.name} is driving'); } void brake() { print('${this.colour} ${this.name} is not driving'); } }
Output
Prius White is driving Honda Fit Black is not driving Custom Car name is Toyota Camry and color is Grey
I hope I helped you to understand the concept of Dart classes and Objects. Please do let me know in the comments box.