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:

SubClassConstructor():super() {  

} 

    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 –

    // Parent class  
    
    class Superclass {  
    
              Superclass(){  
    
                   print("This is a superclass constructor");  
    
      
    
                    }  
    
    }  
    
    class Subclass extends Superclass  
    
    {  
    
              Subclass(){  
    
                    print("This is a subclass constructor");  
    
               }  
    
               display(){  
    
                   print("Welcome to javatpoint");  
    
    }  
    
    }  
    
    void main(){  
    
              print("Dart Implicit Superclass constructor call");  
    
              // We create a object of sub class which will invoke subclass constructor.  
    
              // as well as parent class constructor.   
    
              Subclass s = new Subclass();  
    
              // Calling sub class method  
    
              s.display();  
    
    }  

      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 –

      // Parent class  
      
      class Superclass {  
      
                Superclass(String msg){  
      
                     print("This is a superclass constructor");  
      
                      print(msg);  
      
        
      
                      }  
      
      }  
      
      class Subclass extends Superclass  
      
      {  
      
                Subclass():super("We are calling superclass constructor explicitly "){  
      
                      print("This is a subclass constructor");  
      
                       
      
                 }  
      
                 display(){  
      
                     print("Welcome to javatpoint");  
      
      }  
      
      }  
      
      void main(){  
      
                print("Dart Implicit Superclass constructor example");  
      
                // We create an object of sub class which will invoke subclass constructor.  
      
                // as well as parent class constructor.   
      
                Subclass s = new Subclass();  
      
                // Calling sub class method  
      
                s.display();  
      
      }  

        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

        Leave a Reply

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