Dart super Keyword
The instant parent class object of the active child class is indicated by the super keyword. In its child class, it is utilized to call superclass constructors and functions. The primary goal of the super keyword is to eliminate ambiguity between parent classes and subclasses that share the same method name. Additionally, the superclass methods and attributes are referred to by it.
Usage of static Keyword
- The super keyword can access parent class data members in the child class when the parent and child classes have members with the same name.
- In the child class, it is utilized to access the constructor of the parent class.
- The superclass method that the subclass overrides can be accessed by using the super keyword.
Using a super keyword with variables
When variables in a child class have the same name as variables in a superclass, this circumstance occurs. So, the Dart compiler may encounter ambiguity. The superclass variables in its child class are then accessible to the super keyword. Below is the syntax.
Syntax
Super.varName
Example
// Super class Car
class Car
{
int speed = 180;
}
// sub class Bike extending Car
class Bike extends Car
{
int speed = 110;
void display()
{
//print varible of the base class (Bike)
print("The speed of car: ${super.speed}");
}
}
void main() {
// Creating object of sub class
Bike b = new Bike();
b.display();
}
Output
The speed of car: 180
Explanation
The superclass Car in the code above is declared with a speed variable, and the subclass Bike inherits it.
We utilized the super keyword to access the parent class variable because the sub class also has a variable speed. After creating the child class B object, we called the display method to report the superclass variable’s value.
The value of the subclass variable will be printed if print(speed) is used in place of print(super.speed).
The code above can be copied, pasted into a notepad or dartpad, and used to output the value without using the super keyword. The differences between the two outcomes are visible.
Using the super keyword with parent class method
The super keyword is used to access the parent class method in the child class, as was previously stated. The parent class method can be used in the child class by using the super keyword if the names of the parent and child classes are the same. Below is the syntax.
Syntax
super.methodName;
Example
// Base class Super
class Super
{
void display()
{
print("This is the super class method");
}
}
// Child class inherits Super
class Child extends Super
{
void display()
{
print("This is the child class");
}
// Note that message() is only in Student class
void message()
{
// will invoke or call current class display() method
display();
// will invoke or call parent class displa() method
super.display();
}
}
void main() {
// Creating object of sub class
Child c = new Child();
// calling display() of Student
c.message();
}
Output
This is the child class method
This is the super class method
Explanation
We generated the method with the same name in both the parent class and the child class in the code above. Since the display() method exists in both the parent and child classes, method overriding is taking place.
Thus, we made a message() function in the child class, used the super keyword to invoke the parent class method, and then produced the child class object. The two show() method statements were printed on the screen when we used the message() method using the object.
It should be noted that the superclass method can only be overridden by the subclass method. We don’t need to use super keywords if it doesn’t.
Using super keyword with constructor
The parent class constructor can alternatively be accessed by using the super keyword. Depending on the circumstance, the super keyword can invoke constructors that are parameterized or not. Below is the syntax.
Syntax
:super();
Example
// Base class called Parent
class Parent
{
Parent()
{
print("This is the super class constructor");
}
}
// Child class Super
class Child extends Parent
{
Child():super() // Calling super class constructor
{
print("This is the sub class constructor");
}
}
void main() {
// Creating object of sub class
Child c = new Child();
}
Output
This is the super class constructor
This is the sub class constructor
Explanation
The Dart language’s syntax is too similar to that of C#. The super keyword—separated by :—was used to invoke the constructor of the parent class. (colon). The superclass should be initialized by the delegate of a superclass constructor.