Class and object in Dart : Learn from real-life examples
Let’s explore class and object in Dart programming language with fruitful examples.
What is a class?
A class is like a blueprint or a template for creating objects. It defines the properties and behaviours that an object can have.
What is an object?
An object is an instance of a class. Objects are created based on the specifications defined in the classes.
What is a constructor?
The constructor serves as a specialized method invoked during the creation of an object, facilitating the initialization of the object with specific values.
Real-Life Example
Let’s consider a class called ‘Car’
Let’s consider a car that has different properties such as model, brand, and color. We can drive a car at various speeds; we can stop or even idle for a period of time, as these are parts of its behaviors. Moreover, we can customize or buy different styles of cars from the market too.
How to create a class in Dart?
class Car
{
String? model;
String? brand;
int? year;
String? color;
}
You might be wondering, where do these ‘?‘ question marks come from in the class declaration? It’s a part of null safety in Dart.
Setting up a constructor for our class, Car
Car(String model, String brand, int year, String color)
{
this.model = model;
this.brand = brand;
this.year = year;
this.color = color;
}
In the above snippet, the constructor ‘Car’ is defined with parameters for ‘model,’ ‘brand,’ ‘year,’ and ‘color.’ The ‘this‘ keyword is used to automatically assign the passed values to the corresponding properties of the class.
Behaviours
void info() {
print('Car model: $model \n Color: $color \n Brand: $brand');
}
void start()
{
print('$model is starting...');
}
void drive() {
print('Iam driving $brand\'s $model car ');
}
void stop()
{
print('$model is stopping..');
}
Create multiple cars by instantiating objects
Let’s create objects for the cars
Car myNewCar = Car('IGNIS','Maruti',2023,'Nexa Blue');
Car myEvCar = Car('PUNCH ev', 'TATA', 2024, 'White');
Performing specific tasks :
myNewCar.info();
myNewCar.start();
myNewCar.drive();
myNewCar.stop();
Complete Program Example : class_object.dart
Use DartPad or the Dart command line for executing the following program
class Car
{
String? model;
String? brand;
int? year;
String? color;
Car(String model, String brand, int year, String color)
{
this.model = model;
this.brand = brand;
this.year = year;
this.color = color;
}
void info() {
print('Car model: $model \n Color: $color \n Brand: $brand');
}
void start()
{
print('$model is starting...');
}
void drive() {
print('I am driving $brand\'s $model car ');
}
void stop()
{
print('$model is stopping..');
}
}
void main()
{
Car myNewCar = Car('IGNIS','Maruti',2023,'Nexa Blue');
myNewCar.info();
myNewCar.start();
myNewCar.drive();
myNewCar.stop();
Car myEvCar = Car('PUNCH ev', 'TATA', 2024, 'White');
myEvCar.start();
myEvCar.stop();
}
Output
Car model: IGNIS
Color: Nexa Blue
Brand: Maruti
IGNIS is starting...
I am driving Maruti's IGNIS car
IGNIS is stopping..
PUNCH ev is starting...
PUNCH ev is stopping..
Overview
Classes provide a way to structure your code, making it more organized and reusable. Objects, on the other hand, allow you to create instances with specific attributes and behaviors.
Using classes and objects in Dart : Advantages
Encapsulation
Encapsulation is the bundling of data (attributes) and the methods (functions) that operate on the data within a single unit (class).
It helps in hiding the internal implementation details of an object from the outside world. This enhances security and prevents unintended interference.
Reusability
After defining a class, you have the ability to instantiate multiple objects (instances) of that class, all sharing the same structure and behavior.
Inheritance
Dart supports inheritance, allowing one class to inherit the properties and methods of another class.
nheritance promotes code reuse by allowing you to create a new class based on an existing one. This facilitates the extension and modification of existing functionalities without modifying the original class.
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enhances flexibility by enabling you to use a single interface to represent different types of objects.
You have learned how to create a class and an object in Dart. Congratulations!
Happy Darting!