Constructors in Dart
Tags: dart, todayilearned, beginners
Published: 2019-09-16 05:10:51

* 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

```dart
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

```dart
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

```dart
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
```dart
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
* [Fuchsia OS official site](https://fuchsia.dev/)
* [Dart official site](https://dart.dev/)
* [Flutter official site](https://flutter.dev/)


Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com