Dart super Keyword

 

Dart super Keyword

The super keyword is used to denote the instant parent class object of the current child class. It is used to invoke superclass methods, superclass constructor in its child class. The super keyword's main objective is to remove the confusion between parent class and subclass with the same method name. It is also used to refer to the superclass properties and methods.

Usage of static Keyword

  • When parent class and child class have members with the same name, then super keyword can be accessed data members of parent class in child class.
  • It is used to access the parent class constructor in the child class.
  • Using the super keyword, we can access the superclass method that is overridden by the subclass.

Using a super keyword with variables

This situation arises when the child class has the same name variables as superclass variables. So there is a chance of ambiguity for Dart compiler. Then super keyword can access the superclass variables in its child class. The syntax is given below.

Syntax:

  1. Super.varName  

We can more understand by the following example.

Example -

  1. // Super class Car   
  2. class Car  
  3. {   
  4.     int speed = 180;   
  5. }   
  6.     
  7. // sub class Bike extending Car   
  8. class Bike extends Car   
  9. {   
  10.     int speed = 110;   
  11.     
  12.     void display()   
  13.     {   
  14.         //print varible of the base class (Bike)  
  15.         print("The speed of car: ${super.speed}");  
  16.     }   
  17. }   
  18. void main() {  
  19. // Creating object of sub class  
  20. Bike b = new Bike();  
  21. b.display();  
  22. }    

Output

The speed of car: 180

Explanation:

In the above code, we defined the superclass as Car, which has a speed variable, and then it inherited by the subclass Bike.

The sub class also has variable speed, so we used the super keyword to access the parent class variable. We created the object of child class b and called a display method that printed the value of superclass variable.

If we use the print(speed) instead of print(super.speed), it will print the value the subclass variable.

You can copy the above code, paste it into your dartpad or notepad, and print the value without using the super keyword. You can see the difference between both results.

Using the super keyword with parent class method

As discussed earlier, the super keyword is used to access the parent class method in child class. If the child class and parent class consist of the same name, then we can use the super keyword to use the parent class method in child class. The syntax is given below.

Syntax:

  1. super.methodName;  

Let's understand the following example

Example -

  1. // Base class Super   
  2. class Super   
  3. {   
  4.     void display()   
  5.     {   
  6.         print("This is the super class method");   
  7.     }   
  8. }   
  9.   
  10. // Child class inherits Super  
  11. class Child extends Super   
  12. {   
  13.     void display()   
  14.     {   
  15.         print("This is the child class");   
  16.     }   
  17.   
  18.     // Note that message() is only in Student class   
  19.     void message()   
  20.     {   
  21.         // will invoke or call current class display() method   
  22.         display();   
  23.   
  24.         // will invoke or call parent class displa() method   
  25.         super.display();   
  26.     }   
  27. }   
  28.   
  29. void main() {  
  30.  // Creating object of sub class  
  31. Child c = new Child();   
  32. // calling display() of Student   
  33. c.message();   
  34.     }   

Output

This is the child class method
This is the super class method

Explanation -

In the above code, we created the function with the same name in both parent class and child class. The display() method is present in both parent class and child class that means It is a situation of method overriding.

So we created a message() method in the child class, inside it, we called parent class method using the super keyword, and then created the object of the child class. Using the object, we called the message() method that printed both display() methods statements to the screen.

Note - The super can be used only if the subclass method overrides the superclass method. If it doesn't then we don't require using super keyword.

Using super keyword with constructor

We can also 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:

  1. :super();  

Let's understand the following example.

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

Comments