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 Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms. Together, it means the conveyance or representation of a message in many forms. In programming, polymorphism is achieved by the following methods :

1. Method Overloading : This is the concept of overloading a function with same name but different parameters and return type.

For example : A function sum( ) to add two numbers can be overloaded to accept the arguments of different types like integer, decimal or double.

2. Method Overriding : This is the concept of overriding (re-defining) the method of the base class in the derived class. The method has the same name, same arguments and same return type. When the compiler executes the call statement, it calls the method of the derived class instead of the base class. Let us understand this in detail in this article.

Method Overriding

It is a technique wherein the derived class overrides the method of the parent class. This is done with the help of inheritance that is, when the child class extends the base class. By inheritance, the child class has access to all the methods of the parent class, allowing it to re-define the method of the parent class with the same name, arguments and return type.

The use case of method overriding can be when we want the same function to have some different functionality in the derived class.

Important Points to remember :

  1. We can override the methods only in the child class, not in the parent class.
  2. The methods in the derived and the base class should be exactly same. They must have the same name, same arguments, same return type. However, the definition may or may not be the same.
  3. A method declared with 'final' or 'static' keywords cannot be overridden in the child class.
  4. As we know that we cannot derive the constructors of the base class. Therefore, constructors cannot be overridden in the child class.

Example :

Consider the following example in Dart that explains the concept of method overriding :

// definition of the base class ' Base '
class Base 
{
// defining a method show( ) in the base class
void show( )
{
print( " This is the show( ) function of the base class ' Base ' " ) ;
}
}
// definition of the derived class ' Derived ' 
class Derived extends Base 
{ 
// Overriding the show( ) method of the base class ' Base '
void show( )
{
print( " This is the show( ) function of the derived class ' Derived ' " ) ;
}
}
void main( ) 
{
// creating the objects of the base class and the derived class
Base b = new Base( ) ;
Derived d = new Derived( ) ;
// calling the show( ) function of both the ' Base ' and the ' Derived '
// to depict the method overriding
// calling the show( ) function of the base class
b.show( ) ;
// calling the show( ) function of the derived class
d.show( ) ;
}

Output :

This is the show( ) function of the base class ' Base '
This is the show( ) function of the derived class ' Derived '

Example 2 :

Let us consider another example where two child classes inherits a base class :

// definition of the base class ' Base '
class Base
{
  // defining the show( ) method of the base class 
  void show( )
  {
    print( " This is the show( ) function of the base class. " ) ;
  }
}
// definition of the derived class ' Derived ' inheriting the class ' Base '   
class Derived extends Base 
{
  // overriding the show( ) method of the base class
  void show( )
  {
    print( " This is the show( ) function of the derived class. " ) ;
  }
}
// definition of the derived class ' Derived2 ' inheriting the class ' Base '   
class Derived2 extends Base 
{   
  // overriding the show( ) method of the base class
  void show( )
  {
    print( " This is the show( ) function of the derived2 class. " ) ;
  }
}
  
void main( ) 
{ 
  // Creating the objects of the above defined classes
  Base b = new Base( ) ;
  Derived d1 = new Derived( ) ;
  Derived2 d2 = new Derived2( ) ;
    
  // calling the show( ) function using the objects 
  // of all the three classes to depict method overriding
  b.show( ) ;
  d1.show( ) ;
  d2.show( ) ;
}

Output:

This is the show( ) function of the base class. 
This is the show( ) function of the derived class. 
This is the show( ) function of the derived2 class.

Method overriding using the 'super' keyword

In all the examples above, we performed overriding of the function in the derived classes and called them in the main( ) function by objects of the respective classes. We can call the method of the parent class without creating its object. This can be simply done by using the 'super' keyword in the derived class while overriding the method.

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

Advantage of method overriding

The biggest advantage of method overriding is that we can skip defining the function in the base class. The sub class can provide the implementation to that same method as per the needs without making any amendments in the superclass method. This functionality bears the sweeter fruit when we want the same function to behave differently in the derived class apart from the functionality it already has in the main class.

Conclusion :

As a conclusion, you can remember the following points in regard to method overriding in Dart:

  1. The overriding method (that of the child class) must have the same prototype as the overridden method (that of the base class). By prototype we mean the return type, the list of arguments and the sequence and order of the arguments must be the same as the parent class.  
  2. The overriding (re-defining) of the method must be done in the child class, and not in the base class. 
  3. Always remember that the constructor of the base class cannot be inherited in the child class. 
  4. In order to override a method, it is essential to first inherit it.