Dart Super Constructor

 

Dart Super Constructor

The child class can inherit all properties (methods, variables) and behavior of parent expect parent class constructor.& The superclass constructor can be invoke in sub class by using the super() constructor. We can access both non-parameterized and parameterized constructor of superclass. Accessing the constructor of superclass is slightly different in the Dart. The syntax is given below.

Syntax:

  1. SubClassConstructor():super() {  
  2. }  

Implicit super

As we know that the constructor is automatically called when we instantiate a class. When we create the object of sub class, it invokes the constructor of sub class which implicitly invokes the parent class's default(non-parameterized) constructor. We can use super() constructor in our subclass to invoke superclass constructor. Let's understand the following example.

Example -

  1. // Parent class  
  2. class Superclass {  
  3.           Superclass(){  
  4.                print("This is a superclass constructor");  
  5.   
  6.                 }  
  7. }  
  8. class Subclass extends Superclass  
  9. {  
  10.           Subclass(){  
  11.                 print("This is a subclass constructor");  
  12.            }  
  13.            display(){  
  14.                print("Welcome to javatpoint");  
  15. }  
  16. }  
  17. void main(){  
  18.           print("Dart Implicit Superclass constructor call");  
  19.           // We create a object of sub class which will invoke subclass constructor.  
  20.           // as well as parent class constructor.   
  21.           Subclass s = new Subclass();  
  22.           // Calling sub class method  
  23.           s.display();  
  24. }  

Output:

Dart Implicit Superclass constructor example
This is a superclass constructor
This is a subclass constructor
Welcome to javatpoint

Explicit super

If the superclass constructor consists of parameters then we require to call super() constructor with argument in to invoke superclass constructor in subclass explicitly. Let's understand the following example.

Example -

  1. // Parent class  
  2. class Superclass {  
  3.           Superclass(String msg){  
  4.                print("This is a superclass constructor");  
  5.                 print(msg);  
  6.   
  7.                 }  
  8. }  
  9. class Subclass extends Superclass  
  10. {  
  11.           Subclass():super("We are calling superclass constructor explicitly "){  
  12.                 print("This is a subclass constructor");  
  13.                  
  14.            }  
  15.            display(){  
  16.                print("Welcome to javatpoint");  
  17. }  
  18. }  
  19. void main(){  
  20.           print("Dart Implicit Superclass constructor example");  
  21.           // We create an object of sub class which will invoke subclass constructor.  
  22.           // as well as parent class constructor.   
  23.           Subclass s = new Subclass();  
  24.           // Calling sub class method  
  25.           s.display();  
  26. }  

Output:

Dart explicit Superclass constructor example
This is a parameterized superclass constructor
We are calling superclass constructor explicitly
This is a subclass constructor
Welcome to javatpoint

Comments