In the previous post, I have discussed Classes and Objects and continuing to that in this post I will cover Constructors.
So what a constructor is?
As in the previous post, we have created an object car inside the main function using the new keyword, it only creates a generic object without any properties or we can say it is just a spot in memory. And in the next few lines when we assign values to their attributes then it is completely constructed.
Although, constructing directly inside the main function is a good idea but there is another way also by which we can form a constructor inside the class itself.
But stop I am forgetting something. Before we go deep into constructors I want to tell you something about a very important thing this. this is used to denote the current value. Maybe it is not making sense at this time but it will be clear once we use it.
Now coming back to constructors, it is a function or method like entity but without any return type. There are three types of constructors and they are as follows:
So what a constructor is?
As in the previous post, we have created an object car inside the main function using the new keyword, it only creates a generic object without any properties or we can say it is just a spot in memory. And in the next few lines when we assign values to their attributes then it is completely constructed.
Although, constructing directly inside the main function is a good idea but there is another way also by which we can form a constructor inside the class itself.
But stop I am forgetting something. Before we go deep into constructors I want to tell you something about a very important thing this. this is used to denote the current value. Maybe it is not making sense at this time but it will be clear once we use it.
Now coming back to constructors, it is a function or method like entity but without any return type. There are three types of constructors and they are as follows:
- Basic Method: So in this method, the constructor is defined inside the class itself as shown
class Car{
//Instance variables, member variables
String name;
String color;
int model;
//Method 1 to use constructor to construct a object of a class:
Car(String name, String color, int model) {
this.name = name;
this.color= color;
this.model = model;
}
}
main(){
//Then to create the object we can use
var car = new Car("Buggati", "red", 123);
}
Here you have noticed the use of this. Think that instead of this.name = name we have written directly name = name then it will produce an error as we cannot assign the same thing to itself so to avoid such confusion this is used and it says that this attribute of the class is the value passed.
- Named Constructors: Generally, the name of the constructor is same as the name of the class but in case of named constructors we can give a name to the constructor and create an object of the class using the same named constructor.
class Car{
//Instance variables, member variables
String name;
String color;
int model;
//Method 2 also known as named constructors:
Car.initialize() {
name = "Honda";
model = 3789;
}
}
main(){
// Then inside the main function to initialize the new object we use
var secondCar = new Car.initialize();
// In this case the values are already provided but these can be overwritten.
}
- Syntactic Sugar Constructor: Up to now whatever method we have used is very good but as I told you dart is designed to make things simpler and short so this type of constructor is most easy and shortest method.
class Car{
//Instance variables, member variables
String name;
String color;
int model;
//Method 3 also known as syntactic sugar constructor(better and easy way):
Car(this.name, this.color this.model);
//and that's it.
}
main(){
//Then to create the object we can use
var car = new Car("Buggati", "red", 123);
}
So in this post, we have seen about constructors and different types of constructors. Hope you enjoy it. In the next post, I will talk about Inheritance Concept. If you like then please follow the blog series and if you have any doubt or any suggestion then please tell me in the comment section.
Comments
Post a Comment