Add Your Heading Text Here

Dart constructor is an interesting topic which is used to initialise the objects of the class.

What is Constructor?

Basically, constructors are the special type of methods with the same name as the class but with no return type.

Constructors are used to initialise the data members(properties and behaviours) of an object at the time of object creation.

Syntax

// ConstuctorName must be same as the class name
ConstuctorName(){               
}

Types of Constructors

There are three types of constructors available in dart language.

  • Default Constructor
  • Parameterised Constructor
  • Named Constructor

Default Constructor

Constructor with no arguments is known as Default Constructor.

Each class can have only one Default Constructor.

If user don’t create the Default Constructor for particular class then compiler creates a Default (No-argument) Constructor for the class.

For ex :

class Television
{
  Television()
  {
     print("Television On!");
  }
}

void main()
{
/* new Keyword is not necessary with Dart 2 Release.*/
/* Round braces after Class Name  (i.e., Television) will Initialise the object and
 Call the Default Constructor whether you have created it or not.*/

  Television T1 = new Television();
}

Output :

Television On!

Parameterised Constructor

Constructors with one or more parameters (arguments) are known as parameterised constructor.

In dart, class can have only one parameterised constructor. But, can have more than one named constructor.

Syntax :

constructorName(parameterList);

For ex :

class Television
{
    Television(int a, int b)
    {
       print("Sum of 2 Numbers is " +(a+b).toString());
    }
}
void main()
{
  Television T1=new Television(5,4);
}

Output :

Sum of 2 Numbers is 9.

Named Constructor

In dart, class can’t have multiple constructors. A class can have either 1 default constructor or 1 parameterised constructor. To fulfil your multiple constructor requirement dart has a concept called Named Constructor.

Named Constructors are defined as ClassName following with “.” following VariableConstructorName.

Syntax :

ClassName.ConstructorName(parameterList){
}

For ex :

class Television
{
    Television(int a, int b)
    {
        print("Sum is : ${a+b}");
    }
    Television.Channel(String a, String b)
    {
        print("Channel 1 is " + a +" And Channel 2 is "+b);
    }
    /* Named Constructors can be defined as a private constructor by starting the constructor name with _ */
    Television._Channel(String a, String b,int c)
    {
        print("Channel 1 is " + a +" And Channel 2 is "+ b + " And Channel Rank is " + c.toString());
    }
}

void main()
{
    Television T1 = new Television(5, 4);
    Television T2 = new Television.Channel("Movie TV", "Sports TV");
    Television T3 = new Television._Channel("Movie TV", "Sports TV", 5);
}

Output :

Sum is : 9
Channel 1 is Movie TV And Channel 2 is Sports TV
Channel 1 is Movie TV And Channel 2 is Sports TV And Channel Rank is 5

In this way, constructors are useful to initialise the class members or initialise the resources which you want at the time of object creation.

It would be better to understand another topic “This Keyword” at this Moment to better enhance the use of constructors.

This Keyword

This keyword is used to refer the current instance(Object) or methods of the class.

General purpose of this keyword is to differentiate the parameters passes in method and instance variables if they have same name.

For ex :

class Actors
{
    String Name;
    int Age;
    Actors(String Name, int Age)
    {
        Name = Name;
        Age = Age;
    }
}
void main()
{
    Actors actor = new Actors("Tom Cruise", 40);
    print(actor.Name);
    print(actor.Age);
}

Output :

null
null

In above case, Name and Age will generate ambiguity for compiler. As instance variables and parameters passed in constructors have the same name.

So the values assigned in constructor to the Name and Age will not be assigned to the instance variable’s Age and Name.

In that case This keyword takes place to remove this kind of ambiguities introduced for compiler.

Let me explain this concept by correcting above example.

class Actors{
	String Name;
	int Age;
	Actors(String Name,int Age){
		this.Name=Name;
		this.Age=Age;
		// this.Test(); is same as the Test(); Function Call
		this.Test();
		
	}
	void Test(){
		print(“This Keyword”);
	}
}
void main(){
	Actors actor=new Actors("Tom Cruise",40);
  	print(actor.Name);
print(actor.Age);
}

Output :

Tom Cruise
40
This Keyword

Here you can see we have used this.Name and this.Age is used for referencing the instance variables of the class.

Here we also have made function call for Test() method in constructor using this keyword.

So “this keyword” is helpful to referencing the instance variables or methods of the class.

You can have command over the constructor and this keyword by practicing conventional programs.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons