Java: Method Overriding

Method Overriding is a concept that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes. When a method in a child class has the same name, same parameters or signature and same return type(or sub-type) as a method in its parent class, then the method in the child class is said to override the method in the parent class.

See Also:

Example of Method Overriding

Here print() method having the same signature in parent and child class i.e child class print() method overriding on parent class print() method.

public class Person
{
public void print(String name)
	{
	//some code here
	}
}
public class Employee extends Person
{
	@Override
	public void print(String name)
	{
	//some code here
	}

}

How to execute the overridden method?

Type of the object of a class being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.

  • Parent class object is used to invoke the method of the parent class.
  • A child class object is required to execute the method of the child class.

When should we use the overriding method?

It can be used when combining inheritance with overridden methods, a parent class can define the general form of the methods that will be used by all of its child class.

Ways to implement method overriding or runtime polymorphism?

  1. The access modifier of the overridden method in child class can allow more, but not less. For example, A parent class method with access modifier protected can be made public in child class but not private. if you use private will generate a compile-time error.
  2. a final method can not be overridden.
  3. private methods can not be overridden.
  4. The overriding method must have the same return type (or a subtype of it).
  5. We can call the parent class method in the overriding method using super keyword.
  6. We can not override constructors as parents and child class can never have a constructor with the same name.
  7. Abstract methods in an abstract class or interface are meant to be overridden in derived concrete classes otherwise a compile-time error will be thrown.
  8. The presence of a synchronized/strictfp modifier with the method has no effect on the rules of overriding, i.e. it’s possible that a strictfp/synchronized method can override a strictfp/non-synchronized one and vice-versa.
  9. Overriding and Exception-Handling

Rule 1: If the super-class overridden method does not throw an exception, the subclass overriding method can only throw the unchecked exception, throwing checked exception will lead to a compile-time error.

Rule 2: If the super-class overridden method does throw an exception, the subclass overriding method can only throw the same, subclass exception. Throwing parent exception in the Exception hierarchy will lead to a compile-time error. Also, there is no issue if the subclass overridden method is not throwing any exception.
See Also: Method Overloading Exception Handling with Example

  • Static methods can not be overridden(Method Overriding vs Method Hiding)
Super Class Instance Method Super Class Static Method
Sub Class Instance Method   Overrides Generates a compile-time error
Sub Class Static Method Generates a compile-time error Hides