loading...
Cover image for Constructors in Dart

Constructors in Dart

jay_tillu profile image Jay Tillu😎 ・3 min read
  • A constructor is a special method (function) of a class that helps to create an object. As its name indicates its constructs the new object.

  • It helps to create the object by initializing values to instance variables. So we can say that its main goal is to provide values to instance variables.

How constructor is different from other methods of Class


There are some major differences between the normal method and constructor.

  1. The constructor has the same name as the class.
  2. The constructor doesn’t have a return type.
  3. A constructor is automatically called when the object is created.
  4. If we don’t specify a constructor, the default no-argument constructor will be created.

Syntax of declaring constructor

class_name (arguments) {  //If there is a block of code.
  // Constructor body
}

or

class_name (arguments); // If there is no block of code.

Sample Code

class Student{
  Student(int enNum){
    print(enNum);
  }
}

main(){
  var myStudent = new Student(15);
}

Output
15

Named Constructor


  • After seeing the advantages of the constructor you might want to create multiple constructors. But as we know constructor holds the name of the class. So, in that case, you might ask then how can we create multiple constructors and treat them differently. Then here comes a Named Constructor in the picture.

  • By using named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.

Syntax of defining a named constructor

class_name.constructor_name (arguments){ 
   // If there is block of code use this syntax
   // Statements
}

or

class_name.constructor_name (arguments); 
   // If there is not block of code use this syntax

Sample Code

class Employee {
  int empID;
  String empName;
  String empDept;

  Employee.ID(this.empID); // Named Constructor Creation

  Employee.name(this.empName);

  Employee.department(this.empDept);
}

main() {
  var myEmployee01 = new Employee.ID(15);
  var myEmployee02 = new Employee.department("Testing");
  var myEmployee03 = new Employee.name("Ashu");

  print(myEmployee01.empID);
  print(myEmployee02.empDept);
  print(myEmployee03.empName);
}

Output
15
Testing
Ashu

So, guys, that’s it for constructors in a dart. Again this is also the core concept of Object Oriented Programming. So practice it a little bit. Please feel free to share with me if I miss something. Till Then Keep Loving, Keep Coding. And I surely catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

For More Information Please Visit Following Links

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
Twitter
Facebook
Instagram
Medium
or just mail me at jayviveki13@gmail.com

Posted on by:

jay_tillu profile

Jay Tillu😎

@jay_tillu

🌏 विधायास्य धमॅस्य संरक्षणम् 🌍 ☀️ Society before Self ☀️ 🙏🏻 Humanism by Religion 🙏🏻 💻 Work for Mankind 💻 🌐 Believes in वसुधैव कुटुम्बकम् 🌐

Discussion

pic
Editor guide