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.

Variables in class are known as properties when declared inside a class. Varibales are used to define the state of the class.

Functions when declared inside a class are called methods. Functions (methods) are used to modify the variables (state/properties) of the class.

Familiar with the topic? Play Quiz

Declaring a Class

To declare the classes in dart we use the class keyword with class name followed by the curly brackets:

Syntax

  class ClassName {
  // class members
}
  

Here is an example of a class in Dart, We are declaring the class with the name Person having the name and age properties.

Example

  class Person {
  String name = "Jamie";
  int age = 30;
}
  

Creating an Object

To create objects of dart class

  ClassName objectName = ClassName();
  

Here is an example of creating an object in Dart:

  Person person = Person();
  

Accessing Class Members

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

  

print(person.name);
print(person.age);
  

Output

  Jamie
30
  

code Try Yourself

Cascade Operators

You can access more than two properties or methods using cascade operators. It is denoted by (..).

  person
..name
..age; 
  

Class variables and methods

In class there are two types of variables static and non-static varibales in class. Let’s understand the difference between them.

Non-static Variables and Methods

Non-static variables

Non static variables are the normal variables that store values and can only be accessed with the object of class. These kind of variables are known as class properties.

Example

  class MyClass {

  int age = 20; 
}
  

Accessing the non-static variable

  MyClass myObject = MyClass(); 
print(myObject.age);
  

Non-static methods

Non static methods are the normal functions that does something and gives the result out from it. Non-static methods can only be accessed with the help of class object.

Example

  class MyClass {

  int calculateAge() { 
    int age = 10; 

    return age; 
  }
}
  

Accessing the non-static methods variable

  MyClass myObject = MyClass(); 
print(myObject.calculateAge());
  

Static Variables and Methods

Static variables

Static variables are the normal variables with a static keyword,that store values and can be accessed with the name of the class. These kind of variables are known as class static properties.

Example

  class MyClass {

  static int age = 20; 
}
  

Accessing the static variable

  print(MyClass.age);
  

Static Methods

Static methods are the normal functions that does something and gives the result out from it with a static keyword before return type. Static methods can only be accessed with the help of class name.

Example

  class MyClass {

 static int calculateAge() { 
    int age = 10; 

    return age; 
  }
}
  

Accessing the static methods variable

  
print(MyClass.calculateAge());
  

Constructors in Dart

A constructor is a special method that is used to initialize objects. The name of the constructor is the same as the name of the class.

Default Constructor in Dart

A default constructor is a constructor that has no parameters. Here is an example of a default constructor in Dart:

  class Person {
  String name;
  int age;
  
  Person() {
    print("Default Constructor");
  }
}
  

Note:

Parameterized Constructor in Dart

A parameterized constructor is a constructor that has parameters. Here is an example of a parameterized constructor in Dart:

  class Person {
  String name;
  int age;
  
  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}
  

Note:

  class Person {
  String name;
  int age;
  
  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}
  

Named Constructor in Dart

A named constructor is a constructor that has a name. Here is an example of a named constructor in Dart:

  class Person {
  String name;
  int age;
  
  Person.namedConstructor(String name, int age) {
    this.name = name;
    this.age = age;
  }
}
  

Note:

Constant Constructor in Dart

A constant constructor is a constructor that creates a compile-time constant. Here is an example of a constant constructor in Dart:

  class Person {
  String name;
  int age;
  
  const Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}
  

Note:

Defining the different types of parameters in Dart Class Constructors

Parameters are the variables that are passed to the constructor. They are curical in the process of creating objects. Let’s learn about the different types of parameters in Dart Class Constructors.

Positioned Parameters in Dart Class Constructors

Positioned parameters are the parameters that are passed to the constructor in the same order as they are defined in the constructor. Here is an example of a class constructor with positioned parameters in Dart:

  class Person {
  String name;
  int age;
  
  Person(String name, int age);
}
  

Note:

Optional Parameters in Dart Class Constructors

Optional parameters are the parameters that are passed to the constructor in any order. Here is an example of a class constructor with optional parameters in Dart:

  
class Person {
  String name;
  int age;
  
  Person({String name, int age}) ;
}
  

Note:

Named Parameters in Dart Class Constructors

Named parameters are the parameters that are passed to the constructor using their names. Here is an example of a class constructor with named parameters in Dart:

  
class Person {
  String name;
  int age;
  
  Person({String name, int age});
}
  

Note:

When you want any named constructor to be optional, you can use the ? operator. Here is an example of a class constructor with optional named parameters in Dart:

  
class Person {
  String name;
  int age;
  
  Person({String? name, int? age}); 
}
  

Note:

When you want to make any named parameters required you just have to annoate it with the required keyword. Here is an example of a class constructor with required named parameters in Dart:

  
class Person {
  String name;
  int age;
  
  Person({required String name, required int age}); 
   
}
  

Note:

When you want to assign a default value inside a constructor, you can use the = operator. Here is an example of a class constructor with default named parameters in Dart:

  
class Person {
  String name;
  int age;
  
 Person({ this.name = "John", this.age = 25 }); 
}
  

Note:

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.

Example

  class Person {
  final String name;
  final int age;

  Person({required this.name, required this.age});
}

void main() {
  Person person1 = Person(name: "John", age: 25);
  Person person2 = Person(name: "John", age: 25);

  print(person1 == person2);
}
  

Output

  false
  

code Try Yourself

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:

Example

  class Person {
 final String name;
  final int age;

  Person({required this.name, required this.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 {
  final String name;
  final int age;

  Person({required this.name, required this.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 = Person(name: "John", age: 25);
  Person person2 = Person(name: "John", age: 25);

  print(person1 == person2);
}
  

Output

  true
  

code Try Yourself

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

Quiz