![]() |
VOOZH | about |
In Dart, the subclass can inherit all the variables and methods of the parent class with the use of the extends keyword, but it can't inherit the constructor of the parent class. To do so, we make use of a super constructor in the dart. There are two ways to call a super constructor:
In this case, the parent class is called implicitly when there is object creation of the child class. Here, we don't make use of the super constructor, but when the child class constructor is invoked, then it calls the default parent class constructor.
Example:
You are inside Parent constructor!!
You are inside Child constructor!!
If the parent constructor is default then we call it as followed in implicit super, but if it takes parameters, then the superclass is invoked as shown in the syntax mentioned above.
When calling explicitly, we make use of super constructor as:
Child_class_constructor() :super() {
...
}
Example:
You are inside Parent constructor!!
Welcome to Geeks for Geeks
You are inside Child constructor!!
In Dart, a subclass inherits all the properties and methods of its parent class by using the extends keyword. However, it does not inherit the constructors of the parent class. To address this, the superclass constructor is invoked using the super keyword. This can be done implicitly, where the parent's default constructor is automatically called, or explicitly, where parameters are passed to the parent constructor using super(). This process ensures that the parent class is properly initialized before the child class constructor is executed.