Home > Software engineering >  Why is it necessary to use constructors in dart programming language classes?
Why is it necessary to use constructors in dart programming language classes?

Time:07-06

I'm a beginner learning dart from the book dart apprentice and I reached where they were discussing constructors in dart classes, the book was implying that constructors create instances of the class which I understood but I needed more info about constructors. So I googled and some results repeated what was already in the book about it being used to create instances of a class while others also showed that it's used to instantiate class properties, but my problem is with the other answer which I found that they are used to instantiate properties of a class, but my question is: I instantiate all class properties when I create the class by declaring the property variables, like this:

class UserClass{
userClassProperty = "";
anotherUserClassProperty = ""; }

why is the constructor also needed to instantiate class properties?

CodePudding user response:

Often, values are unique to every class instance.

Consider the following example:

class Point {
  final int x;
  final int y;

  const Point(this.x, this.y);

  double get distanceToOrigin => sqrt(x * x   y * y);
}

If the x and y values were defined inside the class, it would be pretty useless. Instead, different Point objects can be instantiated with different values, which means the same code can be used for different situations.

CodePudding user response:

Ok, so constructors instantiate or start a class by collecting all the data the class needs to start to start working. Constructors are so important that the dart compiler provides one even if you don't explicitly create one. For example, you create a class for mammals like this :

class Mammal{
String name = "cat";
int numberOfLegs = 2; 
}

Although you don't explicitly add a constructor the dart compiler adds a default constructor like this :

class Mammal{
    User(); //This is added by dart during the class instantiation by default.
    String name = "cat";
    int numberOfLegs = 2; 
 }

Yeah, that's how important constructors are to the dart compiler.

And also on the topic of why are they necessary even when you declare all the properties by yourself in the class, as hacker1024 said it would make the class pretty useless, as the point of the existence of classes is to create variants but with different properties. Not adding a constructor to your class and defining all the properties in the class would mean that your class doesn't take properties arguments which in turn also means that different variants of your class can't be created. Again this goes directly against the point of the existence of dart classes. For example, you have a class like this :

class Mammals{
 Strig name = "Human";
 int numberOfLegs = 2;
 bool hasFur = false; 
}

final cat = Mammal();
final human = Mammal();
print(cat.numberOfLegs); //Prints 2
//2
print(human.numberOfLegs); //Also prints 2
//2
print(cat.hasFur);
// false 

Yeah, this class is problematic. Cats with 2 legs? You would agree with me that that's really not how things are in reality. And also the class is pretty useless in the sense that it's not modular, no matter which kind of mammal we create be it a cat, a sheep or even a cow the name property is going to be the default one we set, that is "Human". When we create a class to simulate mammals we want to be able to define what kind of properties it has, not use some fixed values. So you want to create a class which has a constructor like this :

class Mammals{
Mammals(String name,int noOfLegs, bool hasFur){
this.name = name;
this.noOfLegs = noOfLegs;
this.hasFur = hasFur;
 }
String name = "";
int noOfLegs = 0;
bool hasFur = False;
}

final cat = Mammal("Cat", 4, True); //Now you can pass in the properties ou want.
final human = Mammal("Human", 2, false);
print(cat.name); //This prints the customized name of the object cat instead of some fixed value
//Cat 
print(human.name); //This prints the customized name of the object human

Now we have two instances of the class with separate property values.
Although this adds a little more code, the modularity benefit is worth it.

  • Related