Method Overriding in Java

Method overriding in Java is when we have an instance method in a subclass with the same signature (name and the same number of parameters with the same type) and the same return type as an existing method in the superclass. In that case, we say that the subclass’s method overrides the superclass’s method.

This feature is useful when we have a method in the superclass, which is about the same method we need in the subclass but not so much the same that we can use it. In this case, we will define a new method in the subclass with the same name, parameters, and the return type must be the same. In this method, we will implement the functionality we need, which will be related only to the sub-class.

Rules for method overriding

  • The method in the subclass must have the same name as the one in the superclass.
  • The method in the subclass must have the same parameters as the one in the superclass.
  • The return type must be the same, or a subtype of the return type of the method declared in a superclass.

Let’s look at one example when we don’t have method overriding:

class Colour {

  public void printColour() {
    System.out.println("Colour...");
  }
}

class Blue extends Colour {
}

class Test {

  public static void main(String[] args) {
    Colour colour = new Blue();
    colour.printColour();
  }
}
Output: Colour…
 
The superclass method is called because it is not overridden in the subclass. This is okay if we don’t want specific functionality for the Blue class, but the one from the superclass is good enough for us.
 
But if we want to have something specific to the blue colour in the printColour() method, we have to override it.

Example of method overriding:

class Test {

  public static void main(String[] args) {
    Colour colour = new Blue();
    colour.printColour();
  }

}

class Colour {

  public void printColour() {
    System.out.println("Colour...");
  }
}

class Blue extends Colour {

  public void printColour() {
    System.out.println("Blue colour...");
  }
}
Output: Blue colour…
 
The output is now from the method we defined in the subclass.
 
Since this method has the same name as the superclass method and has the same number of parameters ( in this case, none ), this method overrides the superclass method and will always be called if it is called using the instance of a Blue class.

We used polymorphism here, which means that we assigned an object of a subclass to the variable that has the superclass type.
 
We’ll cover the polymorphism in upcoming lessons.

What can not be overridden?

  • Final methods – A method that is declared with the final keyword can not be overridden. We create final methods to prevent subclasses from overriding them and changing their behavior.
  • Static methods – A static method can not be overridden, but we will not get an error if we create a special version of that method in the subclass. But each time when we try to call it using the reference of the superclass, the version from the superclass will always be called. That is called shadowing.

That’s it!

 

 

Leave a Reply

Your email address will not be published.