Constructors in Dart

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");
  }
}

If you don’t define a constructor in a class, then Dart creates a default constructor for you.

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;
  }
}

You can also use the this keyword to refer to the current instance of the class. Here is an example of a parameterized constructor using the this keyword in Dart:

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;
  }
}

To define a named constructor you need to use the name of the class followed by a dot (.) and the name of the constructor.

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;
  }
}

To define a constant constructor you need to use the const keyword before the name of the constructor. This type of constructors are used when you want to create a compile-time constant which value must not change.

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);
}

The parameters name and age are positioned parameters.

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}) ;
}

The parameters name and age are optional parameters.

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});
}

The parameters name and age are named parameters.

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}); 
}

The parameters name and age are optional named parameters.

*

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}); 
   
}

The parameters name and age are default named parameters. If no value is passed the value for name will be John and the value for age will be 25 respectively.

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 }); 
}

The parameters name and age are default named parameters. If no value is passed the value for name will be John and the value for age will be 25 respectively.

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