Method Overriding in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

We know that we can access the methods that are present in the superclass from a subclass by making use of the super keyword or by simply creating objects of the subclass. Though, there may be different occasions when we want the subclass object to do things differently to the same method when invoked using subclass objects. We can achieve this by defining the same method again in the subclass with the same name, same arguments and same return type as in the same method that is present inside the superclass.

Now, when we invoke that method, the method present in the subclass will be invoked and executed instead of the method that is defined in the superclass. This process where we defined a method with the same name, same argument and the same return type in the subclass which is already present in the superclass is known as method overriding.

Rules for Method Overriding

  • The method which we want to override can only be written in the subclass, not in the same class.

  • The return type should be the same as in the method present in the superclass.

  • The argument list should be the same as in the method present in the superclass.

  • We cannot override a method if it is declared static or final.

  • If we can't inherit a method, we can't override it.


Let's take a look at an example of method overriding in dart.

Consider the example shown below −

 Live Demo

class ParentClass {
   void displayName(){
      print("Inside the Parent class Method");

class ChildClass extends ParentClass {
   void displayName(){
      print("Inside the Child class Method");

void main(){
   ParentClass pClassObject = new ParentClass();
   ChildClass cClassObject = new ChildClass();

In the above example, we have two classes named ParentClass and ChildClass and the ParentClass is the superclass and the ChildClass is the subclass which is inheriting the superclass, and we declared a method named displayName in both the classes and inside the main function, when we are trying to invoke the method.


Inside the Parent class Method
Inside the Child class Method
Published on 21-May-2021 13:09:09