×

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. 

Related Topics

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.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

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

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 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 If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

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

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 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 Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

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

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.