×

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

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 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.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 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.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 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 Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.