What is constructor?A constructor is a different type of function which is created with same name as its class name. The constructor is used to initialize an object when it is created. When we create the object of class, then constructor is automatically called. It is quite similar to class function but it has no explicit return type. The generative constructor is the most general form of the constructor, which is used to create a new instance of a class. It is option to declare within the class. All class have own constructor but if we don't declare or forget then Dart compiler will create default constructor automatically by passing the default value to the member variable. If we declare own constructor, then default constructor will be ignored. Example - Suppose we have a class name Student and we will create an object of it as follow. It invoked the default constructor of the Student class. Creating Constructor in DartAs we have mentioned, a constructor has the same name as its class name and it doesn't return any value. Suppose if we have class Student then the constructor name should be also Student. Syntax: We must remember the following two rules while creating a constructor.
Let's understand the following example. Example - Output The name is: Jones The age is: 26 Explanation: In the above example, we created a constructor function Student() which is same as the class name. We passed two passed parameter in the constructor and when we instantiated an object of Student class and passed value it automatically called the constructor then printed the result. Types of ConstructorsThere are three types of constructors in Dart as given below.
Default Constructor or no-arg ConstructorA Constructor which has no parameter is called default constructor or no-arg constructor. It is automatically created (with no argument) by Dart compiler if we don't declare in the class. The Dart compiler ignores the default constructor if we create a constructor with argument or no argument. The syntax is given below. Syntax: Let's understand the following example. Example - Output The example of the default constructor Parameterized ConstructorWe can also pass the parameters to a constructor that type of constructor is called parameterized constructor. It is used to initialize instance variables. Sometimes, we need a constructor which accepts single or multiple parameters. The parameterized constructors are mainly used to initialize instance variable with own values. The syntax is given below. Syntax: Let's understand the following example. Example - Output The name is: Jones The age is: 26 Explanation - In the above example, we declared a parameterized constructor which has two parameter name and age. We created an object of the Student class and passed the appropriate value to the constructor. It printed the name and age as an output to the screen. Named ConstructorsThe named constructors are used to declare the multiple constructors in single class. The syntax is given below. Syntax: Let's understand the following example. Example - Output The example of the named constructor The branch is: Computer Science Next TopicDart this Keyword |