Super Constructor in Dart

In dart, the subclass can inherit all the variables and methods of the parent class, with the use of extends keyword but it can’t inherit constructor of the parent class. To do so we make use of super constructor in the dart. There are two ways to call super constructor: 

  1. Implicitly
  2. Explicitly

When calling explicitly we make use of super constructor as:  

Child_class_constructor() :super() {
...
}

Implicit super: In this case, the parent class is called implicitly, when there is object creation of child class. Here we don’t make use of the super constructor but when the child class constructor is invoked then it calls default parent class constructor. 

Example: Calling parent constructor taking no parameter(s).  

Dart



filter_none

edit
close

play_arrow

link
brightness_4
code

// Dart program for calling parent
// constructor taking no parameter
class SuperGeek {
    
  // Creating parent constructor
  SuperGeek(){
    print("You are inside Parent constructor!!");
  }
}
   
class SubGeek extends SuperGeek {
    
  // Creating child constructor
  SubGeek(){
    print("You are inside Child constructor!!");
  }
}
   
void main() {
  SubGeek geek = new SubGeek();
}

chevron_right


 

Output:  

You are inside Parent constructor!!
You are inside Child constructor!!

Explicit super: 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. 

Example: Calling parent constructor taking parameter(s). 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

class SuperGeek {
    
  // Creating parent constructor
  SuperGeek(String geek_name){
    print("You are inside Parent constructor!!");
    print("Welcome to $geek_name");
  }
}
   
class SubGeek extends SuperGeek {
    
  // Creating child constructor
  // and calling parent class constructor
  SubGeek() : super("Geeks for Geeks"){
    print("You are inside Child constructor!!");
  }
}
   
void main() {
  SubGeek geek = new SubGeek();
}

chevron_right


 

Output: 

You are inside Parent constructor!!
Welcome to Geeks for Geeks
You are inside Child constructor!!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.