Class and Objects in Dart

Class

Class is a blueprint for creating an object, it contains all the necessary details about the features of an object and the functionalities of the object.

E.g., If a Car is a class, its characteristics can be name, color, no of seats, etc. These characteristics are also called properties. The car can also have functionalities like start, reverse, park, etc. These functionalities are also called methods of a class.

Declaring Class In Dart

You can declare a class in dart using the class keyword followed by class name and curly braces {}. You can declare a class by the following syntax:

Syntax

class ClassName {
// body of a class
}

In the above syntax:

  • The class keyword is used for defining the class.
  • ClassName is the name of the class.
  • Class name always start with a capital letter.
  • Body of the class consists of properties and functions.

Example Of Declaring A Class In Dart

class Animal {
  String? name;
  int? numberOfLegs;
  int? lifeSpan;

  void display() {
    print("Animal name: $name.");
    print("Number of Legs: $numberOfLegs.");
    print("Life Span: $lifeSpan.");
  }
}

In the above example, the class keyword is used to define the class Animal. Animal class can contain different characteristics like its name, the number of legs, and life span, which are properties. It has the method display(), which displays the animal details when it is called. The ? is used for null safety. We will learn about null safety later.

Object

Objects are the instance of the class. It can be taken as any real-life object such as an animal, table, or book. Each object has a state and behavior. State denotes the physical properties of that object, whereas behavior is the task that can be performed by that object. Each object is an individual unit produced from the class and is treated independently.

Declaring Object In Dart

Once you create a class, it’s time to declare the object. You can declare an object by the following syntax:

Syntax

ClassName objectName = ClassName();

Example Of A Class And Object In Dart

Example 1

class Animal {
  String? name;
  int? numberOfLegs;
  int? lifeSpan;

  void display() {
    print("Animal name: $name.");
    print("Number of Legs: $numberOfLegs.");
    print("Life Span: $lifeSpan years.");
  }
}

void main() {
  Animal animal = Animal();
  animal.name = "Cat";
  animal.numberOfLegs = 4;
  animal.lifeSpan = 15;

  animal.display();
}

Show Output
Here, Animal is a class name with properties name, number of legs, and lifespan. It has a method called display which displays animal details. In the main method, we created an object of class Animal with the name animal. We set animal details and called the display method from the object name. In this way, you can use class and objects in dart.

Example 2

class Car{
  //properties of car
  String? name;
  String? color;
  int? numberOfSeats;
  
  //functions of car
  void start(){
    print("Car Started.");
  }
  
  void reverse(){
    print("Car Reversed.");
  }
  
  void park(){
    print("Car Parked.");
  }
}

void main(){
  //object of car created
  Car car=Car();
  
  //properties for car setted
  car.name="Tesla model 3";
  car.color="Black";
  car.numberOfSeats=5;
  
  //functions of car called
  car.start();
  car.reverse();
  car.park();
}
Show Output

Additional Notes

  • The main method is the entry point for the program so, it is always needed to see the result.
  • The new keyword can be used to create a new object, but it is not necessary to do so.
  • Both predefined, as well as the dynamic data type, is supported.