Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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 '