×

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 Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

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

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

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.

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.

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

6 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 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 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 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 If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute 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 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.