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.
- The constructor has the same name as the class.
- The constructor doesn’t have a return type.
- A constructor is automatically called when the object is created.
- 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.
Learn more about Dart and Flutter
- List in Dart
- Abstract class and Abstract Methods in Dart
- Interface in Dart
- Constructors in Dart
- Arrow Function in Dart
- User Defined Function in Dart
- Functions in Dart
- Switch case in Dart
- Conditionals in Dart
- Operators in Dart
- Keywords in Dart
- Variables in Dart
- Data Types in Dart
Top comments (0)