🛠 Flutter world will be ready by the end of September, 2024 🚀🔥
Learn Dart Programming
Object Oriented Programming
Class and Objects in Dart

Classes and Dart Objects

A class is a blueprint for creating objects. A class encapsulates data for the object. Dart is an object-oriented programming language. Everything in Dart is an object. Objects are instances of classes.

Declaring a Class

Syntax for declaring a class in Dart is as follows:

class ClassName {
  // class members
}

Here is an example of a class in Dart:

class Person {
  String name;
  int age;
}

Creating an Object

Syntax for creating an object in Dart is as follows:

ClassName objectName = new ClassName();

Here is an example of creating an object in Dart:

Person person = new Person();

Accessing Class Members

You can access class members using the dot (.) operator. Here is an example of accessing class members in Dart:

person.name = "John";
person.age = 25; 
 
print(person.name);
print(person.age);

Output

John
25

Comparing two objects of the same class

Now, we have enough understanding of dart programming to comparing varibales like int, double, String etc. But, what about comparing two objects of the same class?

If we comparing two String variables, of same class we get the return value as true or false based on the value. However, thats not true for class objects. Let's learn by example.

class Person {
  String name;
  int age;
}
 
void main() {
  Person person1 = new Person();
  person1.name = "John";
  person1.age = 25;
 
  Person person2 = new Person();
  person2.name = "John";
  person2.age = 25;
 
  print(person1 == person2);
}

Output

false

Two dart objects have the same value but they are not equal. This is because, object creation happens in the heap memory. So, when we compare two objects, we are comparing the memory address of the two objects which we will different everytime we create the two or more objects even with the same value.

We can solve this problem by overriding the == operator. This is called operator overloading.

Operator Overloading in Dart

Operator overloading is a feature of object-oriented programming languages that allows the creation of multiple operators with the same name which can be used with different types of operands.

In Dart, we can override the == operator by overriding the == method. Here is an example of overriding the == operator in Dart:

class Person {
  String name;
  int age;
 
  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Person &&
          runtimeType == other.runtimeType &&
          name == other.name &&
          age == other.age;
 
  @override
  int get hashCode => name.hashCode ^ age.hashCode;
}

Now, if we compare two objects of the same class, we will get the correct result. Here is an example of comparing two objects of the same class in Dart:

 
class Person {
  String name;
  int age;
 
  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Person &&
          runtimeType == other.runtimeType &&
          name == other.name &&
          age == other.age;
 
  @override
  int get hashCode => name.hashCode ^ age.hashCode;
}
 
void main() {
  Person person1 = new Person();
  person1.name = "John";
  person1.age = 25;
 
  Person person2 = new Person();
  person2.name = "John";
  person2.age = 25;
 
  print(person1 == person2);
}

Output

true

Thus, it is important to override the == operator when we are comparing two objects of the same class.

Conclusion

In this tutorial, we learned about classes and objects in Dart. We also learned about operator overloading in Dart. In the next tutorial, we will learn about constructors in Dart.