Method Overriding in Java

When a method in child class has the same name and the same signature (same number and type of parameters) as the method in the parent class then the sub class is overriding the method in the super class and this process is known as method overriding in Java.

Example of method overriding in Java

Output

In the example there is a Class A with a method add() which is overridden in the sub class B. When add() method is invoked on an object of type B then the add() method of the class B is called as it overrides the add() method of the parent class A.

Using super to call parent class method

You can use super to call the parent class version of the overridden method.

Output

Rules for method overriding in Java

  1. When the methods have the same name and the signature is also same in both the super and sub classes methods then only it is considered as method overriding. If methods have the same name but the arguments are not same then these are considered as overloaded methods and that becomes method overloading.
  2. Return type of the overridden method in the sub class should be same or the sub type of the return type of the super class method. That is known as the covariant return type in Java.
  3. The visibility of the overridden method can’t be reduced. For example if a method is public in super class the overridden method in the child class can’t be protected or default as that would mean reducing the visibility. On the other hand if method is protected in super class then it can be public in the overridden method.
  4. If method in the super class throws an exception then the subclass overridden method can declare the same exception as declared in the superclass method or subclass can declare the subtype exception of the exception declared in the superclass method.
  5. A method declared final in the super class can’t be overridden in the subclass.

Method overriding in Java and run time polymorphism

Run time polymorphism in Java is achieved through method overriding. Which of the overridden methods has to be called is resolved at run time rather than at compile time. This process is also known as dynamic method dispatch in Java.

A super class reference can also refer to the sub class object, which of the overridden method has to be called depends upon the reference currently held by the super class reference and that decision is made at the run time.

Method overriding in Java – Run time polymorphism example

Let’s try to clarify it with an example. In the example there is a super class Payment with a method doPayment(double amount).

This class is extended by three sub classes and all of them implement the method doPayment.

Output

In the MainClass a Super class reference variable is declared which refers to different sub class instances at different times and make the method call. Method doPayment() of that child class is called which is referred by the super class reference at that time.

That’s all for the topic Method Overriding in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Static Keyword in Java
  • Finally Block in Java
  • LinkedList Internal Implementation in Java
  • Race Condition in Java
  • How to Reverse a String in Java
  • What is Big Data
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.