Dart Constructor

 

What is constructor?

A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return type. The generative constructor is the most general form of the constructor, which is used to create a new instance of a class.

It is option to declare within the class. All class have own constructor but if we don't declare or forget then Dart

compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored.

Example -

Suppose we have a class name Student and we will create an object of it as follow.

Student std = new Student()  

It invoked the default constructor of the Student class.

Creating Constructor in Dart

As we have mentioned, a constructor has the same name as its class name and it doesn't return any value. Suppose if we have class Student then the constructor name should be also Student.

Syntax:

  1. class ClassName {  
  2.      ClassName() {  
  3. }  
  4. }  

We must remember the following two rules while creating a constructor.

  • The constructor name should be same as the class name.
  • The constructor doesn't have explicit return type.

Let's understand the following example.

Example -

  1. void main() {  
  2.       // Creating an object   
  3.       Student std = new Student("Jones",26);  
  4. }  
  5. class Student{  
  6.      // Declaring a construstor   
  7.      Student(String str, int age){  
  8.           print("The name is: ${str}");  
  9.           print("The age is: ${age}");  
  10.   
  11.              }  
  12. }  

Output

The name is: Jones
The age is: 26

Explanation:

In the above example, we created a constructor function Student() which is same as the class name. We passed two passed parameter in the constructor and when we instantiated an object of Student class and passed value it automatically called the constructor then printed the result.

Types of Constructors

There are three types of constructors in Dart as given below.

  • Default Constructor or no-arg Constructor
  • Parameter Constructor
  • Named Constructor

Default Constructor or no-arg Constructor

A Constructor which has no parameter is called default constructor or no-arg constructor. It is automatically created (with no argument) by Dart compiler if we don't declare in the class. The Dart compiler ignores the default constructor if we create a constructor with argument or no argument. The syntax is given below.

Syntax:

  1. class ClassName {  
  2.    ClassName() {  
  3.    // constructor body  
  4.    }  
  5. }  

Let's understand the following example.

Example -

  1. void main() {  
  2.       // Call constructor automatically when we creates an object   
  3.       Student std = new Student();  
  4. }  
  5.   
  6. class Student{  
  7.      // Declaring a construstor   
  8.      Student(){  
  9.           print("The example of the default constructor");  
  10.              }  
  11. }  

Output

The example of the default constructor

Parameterized Constructor

We can also pass the parameters to a constructor that type of constructor is called parameterized constructor. It is used to initialize instance variables. Sometimes, we need a constructor which accepts single or multiple parameters. The parameterized constructors are mainly used to initialize instance variable with own values. The syntax is given below.

Syntax:

  1. class ClassName {  
  2.    ClassName(parameter_list)  
  3.   // constructor body  
  4. }  

Let's understand the following example.

Example -

  1. void main() {  
  2.       // Creating an object   
  3.       Student std = new Student("Jones",26);  
  4. }  
  5. class Student{  
  6.      // Declaring a parameterized constructor   
  7.      Student(String str, int age){  
  8.           print("The name is: ${str}");  
  9.           print("The age is: ${age}");  
  10.   
  11.              }  
  12. }  

Output

The name is: Jones
The age is: 26

Explanation -

In the above example, we declared a parameterized constructor which has two parameter name and age. We created an object of the Student class and passed the appropriate value to the constructor. It printed the name and age as an output to the screen.

Named Constructors

The named constructors are used to declare the multiple constructors in single class. The syntax is given below.

Syntax:

  1. className.constructor_name(param_list)  

Let's understand the following example.

Example -

  1. void main() {  
  2.       // Creating an object   
  3.       Student std1 = new Student();  // object for Default constructor  
  4.       Student std2 = new Student.namedConst("Computer Science");  // object for parameterized constructor  
  5. }  
  6.   
  7. class Student{  
  8.      // Declaring a construstor   
  9.      Student(){  
  10.           print("The example of the named constructor");  
  11.              }  
  12.       // Second constructor  
  13.      Student.namedConst(String branch){  
  14.           print("The branch is: ${branch}");  
  15.   
  16.            }  
  17. }  

Output

The example of the named constructor
The branch is: Computer Science

Comments