Using super keyword with constructor

We can use the super keyword to access the parent class constructor. The super keyword can call both parameterized and non-parameterized constructors depending on the situation. The syntax is given below.

Syntax:

:super(); 

Example –

  1. // Base class called Parent  
  2. class Parent  
  3. {   
  4.     Parent()   
  5.     {   
  6.         print(“This is the super class constructor”);   
  7.     }   
  8. }   
  9.   
  10. // Child class Super  
  11. class Child extends Parent   
  12. {              
  13.     Child():super()   // Calling super class constructor  
  14.     {                   
  15.         print(“This is the sub class constructor”);   
  16.     }   
  17. }  
  18.   
  19. void main() {  
  20.  // Creating object of sub class  
  21. Child c = new Child();   
  22. }  

Output

This is the super class constructor
This is the sub class constructor

Explanation
The syntax of Dart language is too close to C# language We called the parent class constructor using the super keyword, which is separated by : (colon). The delegate a superclass constructor, we should put the superclass as an initializer.

Leave a comment

Your email address will not be published. Required fields are marked *