×

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass ( also known as the parent class ) variables, methods and the constructors in its subclass ( also known as the child class ). It can also refer to the properties and methods of the superclass. The use case of the ' super ' keyword is to remove the ambiguity of the same name of parent and child class for the compiler. 

Advantages of the ' super ' keyword:

  • It prevents the overriding of the parent class method in the derived class. 
  • It can also be used to call the parameterized constructor of the parent class. 
  • The main advantage of the ' super ' keyword is that it can be used to access the data members of the parent class when both the parent and child class have a member with the same name.
  • Now it’s the time to understand the syntax of ' super ' keyword using the programs in Dart language.  

Using a ' super ' keyword with variables:

We use this keyword with variables when the child class and parent class have the same variable ( the name is the same ). If we do not use ‘ super ’ keyword, this results in ambiguity for the compiler. 

Using this keyword, we can access the variable of the parent class in the child class. 

The syntax is given below :

Super.varName ;  

Example :

Consider the following in Dart that explains the concept of ‘ super ’ keyword :

// definition of the parent class ' teacher '
class teacher
{   
    // initializing the data members of the 
    // parent class ' teacher '
    var name = ' Dimple ' ;
    var e_id = 1 ;
    var course = ' BCA ' ;
}   
// definition of the child class ' student '
class student extends teacher  
{   
    // initializing the data members of the 
    // child class ' student '
    var name = ' Yukta ' ;
    var s_id = 1 ;
    var course = ' BCA ' ;
    // member function display( ) to print the values
    void display( )   
    {   
        // printing the value of the name variable 
        // of the parent class ' teacher '
        print( " The name of the teacher is : " ) ;
        print( super.name ) ;
        // printing the value of the name variable 
        // of the child class ' student '
        print( " The name of the student is : " ) ;
        print( name ) ;
    }   
} 
void main( ) 
{   
  // creating the object of the base class ' student ' 
  student s1 = new student( ) ;  
  s1.display( ) ;  
} 

Output :

 The name of the teacher is : 
 Dimple 
 The name of the student is : 
 Yukta 

Explanation :

In the above code, we had two classes : 

1. Teacher class = The parent class that contains ' name ', ' e_id ', and ' course ' as the data members of the class. 

2. Student class = The child class that inherits the parent class ' teacher '. This class also contains ' name ', ' course ', and ' s_id ' as the data members of the class, and the display( ) function to display the values. 

Now, we want to print the name of both the teacher and the student. But the variable holding these values in their respective classes has the same name, ' name '. So, if we write a statement to print the name, the compiler will print the name of the student only because the variable ' name ' is local to the class ' student '. In order to print both the values in the child class, we used the super keyword with the ' name ' variable to print the name of the teacher. 

Using a 'super' keyword with the parent class method:

We use this keyword with methods when the child class and parent class have the same name for this method. If we do not use super keyword, this results in ambiguity for the compiler. 

Using this keyword, we can access the method of the parent class in the child class. 

The syntax is given below :

super.methodName( ) ;

Example :

Consider the following code in Dart that explains the concept of ‘ super ’ keyword

// definition of the parent class ' teacher '
class teacher
{   
    // initializing the data members of the 
    // parent class ' teacher '
    var t_name = ' Dimple ' ;
     // member function display( ) to print the values
    void display( )   
    {   
         // printing the value of the ' t_name ' variable 
        // of the parent class ' teacher '
        print( " The name of the teacher is : " ) ;
        print( t_name ) ;
    } 
    }   
// definition of the child class ' student '
class student extends teacher  
{   
    // initializing the data members of the 
    // child class ' student '
    var s_name = ' Yukta ' ;
    // member function display( ) to print the values
    void display( )   
    {   
        // printing the value of the ' s_name ' variable 
        // of the parent class ' student '
        print( " The name of the student is : " ) ;
        print( s_name ) ;
        // accessing the member function display( ) of the
        // main class ‘ teacher ’
        super.display( ) ;
    }   
} 
void main( ) 
{  
  // creating the object of the base class ' student ' 
  student s1 = new student( ) ;  
  s1.display( ) ;  
}  

Output :

 The name of the student is : 
 Yukta 
 The name of the teacher is : 
 Dimple 

Explanation -

In the above code, we had two classes : 

1. Teacher class = The parent class that contains the data member ' t_name ', and the member function ' display ' that prints the value of t_name.

2. Student class = The child class that inherits the parent class ' teacher '. This class contains the data member ' s_name ', and the member function of the same name as parent's class function ' display ' that prints the value of s_name. 

Now, we want to call the member function of both the teacher and the student. But they have the same name ' display '. So we access the display( ) function of the super class ' teacher ' using the ' super ' keyword in the display( ) function of the sub class ' student '. 

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

This ' super ' keyword can even be used to access the base class's constructor. It can call both the parameterized and default constructor as per the requirement. 

The syntax is given below :

:super( ) ;

Example :

Consider the following example that explains the concept of the ‘ super ’ keyword

// definition of the main class ' teacher ' 
class teacher
{   
    // default constructor teacher( ) of the parent class ' teacher '
    teacher( )  
    {   
        print( " This is the default constructor of parent class ' teacher ' " ) ;   
    }   
}   
// definition of the child class ' student ' inheriting the main class ' teacher '
class student extends teacher  
{              
    // accessing the constructor of base class in the child class
    student( ):super( )  
    {                   
        print(" This is the default constructor of child class ' student '" ) ;   
    }   
}  
 void main( ) 
{  
  // creating the object of the child class 
  student s1 = new student();   
} 

Output :

 This is the default constructor of parent class ' teacher ' 
 This is the default constructor of child class ' student '

Related Topics

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.