Table of contents
1.
Introduction
2.
Dart Constructor
3.
Creating Constructors
4.
Types of Constructors
4.1.
Default Constructors
4.2.
Parameterized Constructors
4.3.
Named Constructors
5.
Dart Super Constructor
6.
Types of Super Constructors
6.1.
Implicit Constructor
6.2.
Explicit Constructor
7.
Frequently Asked Questions
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Constructors and Super Constructors

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss about constructors and super constructors in Dart. You will learn about various types of constructors available in Dart and sample programs that will depict the implementation of these different types of constructors. In the next section, we will start by digging deeper into Dart Constructors.

Dart Constructor

Constructors are used for the initialization of an object at the time of the creation of the object. The constructor's name is the same as the name of the class. It is invoked automatically by the program compiler when the object is created. It is not mandatory to create a constructor in a class. The program compiler uses a default constructor if a constructor is not described explicitly. This default constructor is dropped when you specify a manual constructor. Constructors are similar to other methods of the class, although they have some differences as well. For example, no explicit return data type is associated with a constructor. 

Creating Constructors

The following is the syntax to create a constructor in Dart. For example, if we have a class named Ninja, we can specify the constructor as follows.

class Ninja{
Ninja(){
// body of the constructor
}
}

Types of Constructors

There are three different types of constructors that are available in Dart. They are discussed below.

Default Constructors

These are also known as no-argument constructors. As mentioned earlier, the compiler automatically makes the default constructor if the programmer does not explicitly specify it. It is generally used to assign values to the different class properties. For example, the following program shows the usage and implementation of the default constructor.

Code:

class Ninja{
  late String ninja_name;
  late String ninja_batch;

  Ninja(){
    ninja_name = "Mr X";
    ninja_batch = "A1";
  }

  void get_ninja_details(){
    print("ninja_name: " + ninja_name);
    print("ninja_batch: " + ninja_batch);
  }
}

void main(){
  Ninja obj = new Ninja();
  obj.get_ninja_details();
}

Output:

ninja_name: Mr X
ninja_batch: A1

Parameterized Constructors

Parameterized constructors refer to those constructors which take additional arguments as input. They are quite commonly used to initialize objects of a class. For example, the following program shows the usage and implementation of the parameterized constructors.

Code:

class Ninja{
  late String ninja_name;
  late String ninja_batch;

  Ninja(String ninja_name_, String ninja_batch_){
    ninja_name = ninja_name_;
    ninja_batch = ninja_batch_;
  }

  void get_ninja_details(){
    print("ninja_name: " + ninja_name);
    print("ninja_batch: " + ninja_batch);
  }
}

void main(){
  Ninja obj = new Ninja("Mr X", "A1");
  obj.get_ninja_details();
}

Output:

ninja_name: Mr X
ninja_batch: A1

Named Constructors

Sometimes, it is required by the scenario to declare multiple constructors for the same class. For such cases, we use named constructors in Dart.

Code:

class Ninja{
  late String ninja_name;
  late String ninja_batch;

  Ninja(String ninja_name_, String ninja_batch_){
    print("Invoking parameterized constructor");
    ninja_name = ninja_name_;
    ninja_batch = ninja_batch_;
  }

  Ninja.named_constructor_1(String ninja_name_, String ninja_batch_){
    print("Invoking named constructor");
    ninja_name = ninja_name_;
    ninja_batch = ninja_batch_;
  }
}

void main(){
  Ninja obj = new Ninja("Mr X", "A1");
  Ninja obj2 = new Ninja.named_constructor_1("Mr X", "A1");
}

Output:

Invoking parameterized constructor
Invoking named constructor

Dart Super Constructor

Dart is an object-oriented programming language, and one of the fundamental pillars of object-oriented programming is inheritance. In cases of inheritance, all the methods and variables of the parent class are inherited to the respective child class. But this is not the case with constructors, i.e., the constructor of the parent class is not inherited by its child class. We can solve this problem with the help of the super() method in Dart, which allows us the subclass to call the default and parameterized constructors of the superclass.

Types of Super Constructors

There are two types of super constructors in Dart. They are discussed as follows.

Implicit Constructor

When an object of the subclass is created using the keyword “new”, it automatically invokes the default constructor of the parent class. The following example discusses the usage of implicit super constructors.

Code:

class ParentClass{
  ParentClass(){
    print("The constructor of the parent class is invoked");
  }
}

class SubClass extends ParentClass
{
  SubClass(): super(){
    print("The constructor of the sub class is invoked");
  }
 
  void say_hello(){
    print("Hello from Coding Ninjas");
  }  
}
 
void main(){
  SubClass obj= new SubClass();
  obj.say_hello();
}

Output:

The constructor of the parent class is invoked
The constructor of the subclass is invoked
Hello from Coding Ninjas

Explicit Constructor

These are used to invoke parameterized constructors of the parent class from the child class. The following example discusses the usage of explicit super constructors. 

Code:

class ParentClass{
  ParentClass(int arg0, String arg1){
    print("The constructor of the parent class is invoked");
    print("ARG0: ${arg0}");
    print("ARG1: " + arg1);
  }
}

class SubClass extends ParentClass
{
  SubClass(): super(1, "This is an argument to the constructor of parent class received from the subclass constructor"){
    print("The constructor of the subclass is invoked.");
  }
 
  void say_hello(){
    print("Hello from Coding Ninjas");
  }  
}
 
void main(){
  SubClass obj= new SubClass();
  obj.say_hello();
}

Output:

The constructor of the parent class is invoked
ARG0: 1
ARG1: This is an argument to the constructor of the parent class received from the subclass constructor
The constructor of the subclass is invoked.
Hello from Coding Ninjas

Frequently Asked Questions

  1. Is it mandatory to add a constructor to your class while writing a program?
    No, it is not mandatory to add a constructor to your program. The compiler will automatically add a constructor if you don't add it explicitly.
     
  2. How can I create an object of the subclass?
    The object of the subclass can be created by using the new keyword along with the invocation to the constructor.
     
  3. Is it possible to add multiple constructors to my Dart class?
    Yes, it is possible to use multiple constructors in a single Dart class with the help of named constructors in Dart.

Conclusion

In this article, we have extensively discussed constructors and super constructors in Dart and saw sample programs having their implementation. You can also read the blog Dart Classes and Objects on the Coding Ninjas Website to learn more about classes and objects in Dart.

We hope this blog has helped you enhance your knowledge regarding Dart Constructors and Super Constructors. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass