A method or a function is the most important part of any object oriented language. A function / method can be defined as: a block of statements which perform a specific task. Methods describe the behavior of an object. Java method overriding is an important feature to create methods in a derived class. This method has the same name as that of method in parent class but with different functionality.

What is Method Overriding?

When a class is inherited and we declare a method in the subclass which is already present in the base class, it is referred to as method overriding. Overriding is done so that the subclass has the ability to define and implement the method specific to the subclass type i.e. the method defined in the subclass will override the functionality of the existing method of the super or base class. Hence, the method of the parent class is called the overridden method and the method in the subclass or the child class is known as the overriding method.

Examplejava method overriding

Method overriding is one way in which java achieves Run time polymorphism.

The method to be invoked is determined at run time and depends on which object invokes it. If an object of parent class invokes the method, then the base / parent class version is executed, but if an object of the subclass is used to invoke the method, then the version in the subclass gets executed.

When overriding a method, some things need to be kept in mind. These are discussed below.

Rules for Java Method Overriding

  • The name of the method in the subclass must be the same as in the parent class.
  • The argument list in the subclass should be exactly the same as that of the overridden method.
  • The return type of both methods i.e. the overriding and the overridden methods should be the same.
  • The access level cannot be restrictive than that of the overridden methods access level.
    • For example: if the method in the super / parent class is declared public then the overriding method in the subclass cannot be private or protected.
  • A method declared static cannot be overridden but it can be re-declared.
    • Java main function cannot be overridden as it is also a static function.
  • Instance methods can be overridden only if they are inherited by the class.
  • A method declared final cannot be overridden.
  • A method cannot be inherited if it cannot be overridden.
  • An overriding method can throw any uncheck exception, regardless of whether the overridden method throws exception or not.
  • Constructors cannot be overridden.
  • A subclass of a different package can only override the non-final methods declared public or protected.

Lets take-up an example

Let us consider a scenario where bank is the class which provides functionality of rate of interest and there are two subclasses overriding these rates with the rate of interest of their own.

java method overriding example

Here Bank is the super class and Bank1 and Bank2 extends the base class.

Example code

 
 
// Parent Class
class BankMain
{
      int roi()
      {
            return 0;
      }
}
// sub class Bank1
class Bank1 extends BankMain
{
      int roi()
      {
            return 7;
      }
}
// sub class Bank2
class Bank2 extends BankMain
{
      int roi()
      {
            return 9;
      }
}
class Bank
{
      public static void main(String args[])
      {
            Bank1 b1 = new Bank1();
            Bank2 b2 = new Bank2();
            System.out.println("Rate of Interest of Bank 1 = " + b1.roi());
            System.out.println("Rate of Interest of Bank 2 = " + b2.roi());
      }
}

Output

java method overriding code example

In the above example, the method roi() of the subclasses override the roi() function of the super class BankMain and thus the objects of the respective subclasses have their respective rate of interests defined in their own roi() functions.

Advantage of Java Method Overriding

The main advantage of method overriding is that it allows the main class to define methods which would be common to all of the subclasses and it also allows the subclasses to define their own specific implementation of some or all of those methods.