Dart super Keyword

In this tutorial you will learn about the Dart super Keyword and its application with practical example.

Dart super Keyword

The super keyword is a reference variable which is used to refer immediate parent class object. It is used to refer the superclass properties and methods. This is possible because when we create an instance of subclass, an instance of its parent class is created implicitly which we can refer using the super keyword. The most common use of the super keyword is to eliminate the ambiguity between superclasses and subclasses that have variables and methods with the same name.

Usage of super Keyword

1) The super keyword can be used to access the data members of parent class when both parent and child class have member with same name.
2) The super keyword can be used to access the method of parent class when child class has overridden that method. 3) The super keyword can be used to explicitly call the no-arg and parameterized constructor of parent class

Use super keyword to access parent class variables

When you have a variable in sub class which is already exists in its super class; then the super keyword can be used to access variable in super class.

Syntax:-

Example:-

In the following program, we have a variable num declared in the SubClass, the variable with the same name is already present in the SuperClass.

Output:-

Here, if we use print(num); instead of print(super.num); it will print 100 instead of 50.

Recommended:-  Dart Keywords

Use super keyword to invoke parent class method

When a subclass contains a method already present in the superclass then it is called method overriding. In method overriding call to the method from subclass object will always invoke the subclass version of the method. However, using the super keyword we are allowed to invoke superclass version of the method.

Syntax:-

Example:-

Output:-

Note:-When subclass doesn’t override the superclass method then there is no need to use the super keyword to call the superclass method.

In this tutorial we have learn about the Dart super Keyword and its application with practical example. I hope you will like this tutorial.