If the method present in subclass is similar to the method present in parent class, it is called as method overriding in Java. Method overriding is based on the polymorphism concept of object oriented programming. Method overriding allows the developer to create two methods with same and interface. Method overriding allows the programmer to write the code which is extensible and flexible. Method overriding enables the developer to introduce new function with slight change in code. Method overriding is the representation of true polymorphic behaviour. In method overriding the name of method remains same while the logic of those methods are entirely different.
- SEE: Java interview Q&A available here
Rules for Method Overriding in Java:
There are several rules to follow while method overriding in Java. if the developer fails to follow any of these rules, it will result in a compiler error.
- The basic rule for method overriding is one can override a method present in subclass while overriding a method in same class is not possible.
- The name and signature of overriding method must be similar to the name and signature of method present in parent class and interface or subclass.
- Method overriding in Java cannot reduce the accessibility of overridden method. If the overridden method is public it can’t be changed into private or protected methods. But overriding a method can increase its accessibility. Accessibility of private and protected methods can be increased by method overriding.
- Method overriding in Java cannot throw exceptions which are in higher hierarchy than the overridden method.
- Overriding of methods like static, final and private are not possible in Java. Methods like static and private are bonded with compile time using static binding process. The static binding process is not resolved during runtime which results in a compile time error. It is possible to hide private and static methods if another method is declared with similar subclass and signature.
- In Java, dynamic binding is used to call the overridden method. Dynamic binding is used during runtime based on the object type.
- To extend the abstract class or implement the interface, overriding of all abstract method is required.
- Use of @override annotation is recommended while overriding a method in Java. Usage of @override annotation is not compulsory but it is widely regarded as the best Java coding practice to follow.
Example for Method Overriding in Java
* Java program to demonstrate method overriding.
* Overridden methods are resolved during runtime.
public class CollectTest
public static void main (String args[ ])
Runnable tsk1=new tsk1();
tsk1.run(); // call overridden method in Task
tsk1= new PeriodTsk();
Class Tsk1 implements Runnable
public void run()
System.err.println(“ The overridden method run() in PerodTsk class”);
Run method overridden in Task class
Overriden method run() in PeriodTsk class
- Download: Final year projects for free
In the above example runnable interface is used. This runnable interface has an abstract method. There are two classes which are task and PeriodicTask respectively. These classes implements runnable interface and also override the run method. When run() method is called in the same thread it is overridden. In this program run() is overridden into two separate classes and call to run() method is resolved in runtime.
Important Points to Remember While Method Overriding:
- Constructors in Java cannot be overridden.
- Argument set of overriding methods should be similar.
- Methods which are overridden should have same return type.
- Only one Java access modifier is allowed in a overridden method.
- Checked new or broader exception cannot be thrown by overridden method.
- Any unchecked exception can be thrown by overridden method.
- Methods like final and static cannot be overridden.
- Private methods are not inherited by subclass so it is not possible to override private method in subclass.
- Polymorphism can be used in method overriding.
- The type of overriding method will be decided by the object type during the runtime.