In this tutorial you will learn about the Dart Constructors and its application with practical example.
Dart Constructors
Constructor is a special method that is used to initialize an object when it is created. Constructor is called automatically when an object is instantiated; it is mainly used to set initial values for instance variables. The constructor has the same name as of class it belongs to. Constructor is syntactically similar to a instance method; but constructors have no explicit return type. It is not mandatory to write a constructor for a class. All classes have its own default constructor, if you don’t create any constructor for a class, the compiler will automatically creates a default constructor every class by assigning the default values to the member variables. But if you define your own constructor, the default constructor will be ignored.
Example:-
Lets say we have class by the name Employee, we will create object for Employee class as below –
1 |
Employee emp = new Employee(); |
This will invoke the default constructor of the Employee class.
Creating Constructors In Dart
A constructor has same name as that of the class, and doesn’t return any value. Suppose if we have class Test, then the constructors name should also be Test.
Syntax:-
1 2 3 4 5 |
class ClassName { ClassName() { //constructor body } } |
There are two important rules to be kept in mind while creating a constructor.
- The Constructor name should be the same name as the class name. Suppose if we have class Test, then the constructors name should also be Test.
- The Constructor cannot have a explicit return type
Example:-
1 2 3 4 5 6 7 8 |
void main() { Employee emp = new Employee('EMP001'); } class Employee{ Employee(String empCode) { print(empCode); } } |
Output:-
1 |
EMP001 |
Types of Constructors
There are following type of Constructors in Dart, they are
- Default Constructor (or) no-arg Constructor
- Parameterized Constructor
- Named Constructor
Default Constructor (or) no-arg constructor
As the name specifies the constructor that has no parameter is known as default constructor. If you don’t create any constructor for a class, the compiler will automatically creates a default constructor(with no arguments) for the class. And if we create a constructor with no-arguments or arguments then the compiler will not create a default constructor. Default constructor provides the default values to the member variables.
Syntax:-
1 2 3 4 5 |
class ClassName { ClassName() { //constructor body } } |
Example:-
1 2 3 4 5 6 7 8 |
void main() { Employee emp = new Employee(); } class Employee{ Employee() { print("W3Adda - Default Constructor of Employee class called"); } } |
Output:-
Parameterized Constructor
Constructors can also take parameters, which is used to initialize instance variables. Most often, we will need a constructor that accepts one or more parameters. A constructor that accepts parameters is known as parameterized constructor. If we want to initialize instance variables with own values, then we are required to use a parameterized constructor.
Syntax:-
1 2 3 4 5 |
class ClassName { ClassName(parameter_list) { //constructor body } } |
Example:-
1 2 3 4 5 6 7 8 9 10 |
void main() { print("W3Adda - Dart Parameterized Constructor"); Employee emp = new Employee('EMP001'); } class Employee{ Employee(String empCode) { print(empCode); } } |
Output:-
Named constructors
In Dart, named constructors allows a class to define multiple constructors.
Syntax:-
1 |
ClassName.constructor_name(param_list) |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void main() { Employee emp1 = new Employee(); Employee emp2 = new Employee.namedConst('EMP001'); } class Employee{ Employee() { print("W3Adda - Default Constructor Invoked"); } Employee.namedConst(String empCode) { print("W3Adda - Named Constructor Invoked"); print(empCode); } } |
Output:-