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

Method overriding is one of the ways by which Java achieves Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.
Example of Method Overriding in Java
Below is the implementation of the Java Method Overriding:
Java
class Parent {
void show() { System.out.println( "Parent's show()" ); }
}
class Child extends Parent {
@Override void show()
{
System.out.println( "Child's show()" );
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
|
Output
Parent's show()
Child's show()
Rules for Java Method Overriding
1. Overriding and Access Modifiers
The access modifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass. Doing so will generate a compile-time error.
Java
class Parent {
private void m1()
{
System.out.println( "From parent m1()" );
}
protected void m2()
{
System.out.println( "From parent m2()" );
}
}
class Child extends Parent {
private void m1()
{
System.out.println( "From child m1()" );
}
@Override public void m2()
{
System.out.println( "From child m2()" );
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.m2();
Parent obj2 = new Child();
obj2.m2();
}
}
|
Output
From parent m2()
From child m2()
2. Final methods can not be overridden
If we don’t want a method to be overridden, we declare it as final. Please see Using Final with Inheritance.
Java
class Parent {
final void show() {}
}
class Child extends Parent {
void show() {}
}
|
Output
13: error: show() in Child cannot override show() in Parent
void show() { }
^
overridden method is final
3. Static methods can not be overridden(Method Overriding vs Method Hiding):
When you define a static method with the same signature as a static method in the base class, it is known as method hiding. The following table summarizes what happens when you define a method with the same signature as a method in a super-class.
Overrides |
Generates a compile-time error |
Generates a compile-time error |
Hides |
Java
class Parent {
static void m1()
{
System.out.println( "From parent "
+ "static m1()" );
}
void m2()
{
System.out.println(
"From parent "
+ "non - static(instance) m2() " );
}
}
class Child extends Parent {
static void m1()
{
System.out.println( "From child static m1()" );
}
@Override public void m2()
{
System.out.println(
"From child "
+ "non - static(instance) m2() " );
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Child();
obj1.m1();
obj1.m2();
}
}
|
Output
From parent static m1()
From child non - static(instance) m2()
4. Private methods can not be overridden
Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override private methods in a subclass.(See this for details).
Java
class SuperClass {
private void privateMethod()
{
System.out.println(
"This is a private method in SuperClass" );
}
public void publicMethod()
{
System.out.println(
"This is a public method in SuperClass" );
privateMethod();
}
}
class SubClass extends SuperClass {
private void privateMethod()
{
System.out.println(
"This is a private method in SubClass" );
}
public void publicMethod()
{
System.out.println(
"This is a public method in SubClass" );
privateMethod();
}
}
public class Test {
public static void main(String[] args)
{
SuperClass obj1 = new SuperClass();
obj1.publicMethod();
SubClass obj2 = new SubClass();
obj2.publicMethod();
}
}
|
Output
This is a public method in SuperClass
This is a private method in SuperClass
This is a public method in SubClass
This is a private method in SubClass
5. The overriding method must have the same return type (or subtype)
From Java 5.0 onwards it is possible to have different return types for an overriding method in the child class, but the child’s return type should be a sub-type of the parent’s return type. This phenomenon is known as the covariant return type.
Java
class SuperClass {
public Object method()
{
System.out.println(
"This is the method in SuperClass" );
return new Object();
}
}
class SubClass extends SuperClass {
public String method()
{
System.out.println(
"This is the method in SubClass" );
return "Hello, World!" ;
}
}
public class Test {
public static void main(String[] args)
{
SuperClass obj1 = new SuperClass();
obj1.method();
SubClass obj2 = new SubClass();
obj2.method();
}
}
|
Output
This is the method in SuperClass
This is the method in SubClass
6. Invoking overridden method from sub-class
We can call the parent class method in the overriding method using the super keyword.
Java
class Parent {
void show() { System.out.println( "Parent's show()" ); }
}
class Child extends Parent {
@Override void show()
{
super .show();
System.out.println( "Child's show()" );
}
}
class Main {
public static void main(String[] args)
{
Parent obj = new Child();
obj.show();
}
}
|
Output
Parent's show()
Child's show()
Overriding and Constructor
We can not override the constructor as the parent and child class can never have a constructor with the same name(The constructor name must always be the same as the Class name).
Overriding and Exception-Handling
Below are two rules to note when overriding methods related to 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 a checked exception will lead to a compile-time error.
Java
class Parent {
void m1() { System.out.println( "From parent m1()" ); }
void m2() { System.out.println( "From parent m2()" ); }
}
class Child extends Parent {
@Override
void m1() throws ArithmeticException
{
System.out.println( "From child m1()" );
}
@Override
void m2() throws Exception
{
System.out.println( "From child m2" );
}
}
|
Output
error: m2() in Child cannot override m2() in Parent
void m2() throws Exception{ System.out.println("From child m2");}
^
overridden method does not throw Exception
Rule #2
If the superclass overridden method does throw an exception, the subclass overriding method can only throw the same, subclass exception. Throwing parent exceptions in the Exception hierarchy will lead to compile time error. Also, there is no issue if the subclass overridden method is not throwing any exception.
Java
class Parent {
void m1() throws RuntimeException
{
System.out.println( "From parent m1()" );
}
}
class Child1 extends Parent {
@Override
void m1() throws RuntimeException
{
System.out.println( "From child1 m1()" );
}
}
class Child2 extends Parent {
@Override
void m1() throws ArithmeticException
{
System.out.println( "From child2 m1()" );
}
}
class Child3 extends Parent {
@Override
void m1()
{
System.out.println( "From child3 m1()" );
}
}
class Child4 extends Parent {
@Override
void m1() throws Exception
{
System.out.println( "From child4 m1()" );
}
}
|
Output
error: m1() in Child4 cannot override m1() in Parent
void m1() throws Exception
^
overridden method does not throw Exception
Overriding and Abstract Method
Abstract methods in an interface or abstract class are meant to be overridden in derived concrete classes otherwise a compile-time error will be thrown.
Overriding and Synchronized/strictfp Method
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 synchronized/strictfp method can override a non-synchronized/strictfp one and vice-versa.
Note:
- In C++, we need virtual keyword to achieve overriding or Run Time Polymorphism. In Java, methods are virtual by default.
- We can have multilevel method-overriding.
Java
class Parent {
void show() { System.out.println( "Parent's show()" ); }
}
class Child extends Parent {
void show() { System.out.println( "Child's show()" ); }
}
class GrandChild extends Child {
void show()
{
System.out.println( "GrandChild's show()" );
}
}
class Main {
public static void main(String[] args)
{
Parent obj1 = new GrandChild();
obj1.show();
}
}
|
Output
GrandChild's show()
Method Overriding vs Method Overloading
1. Overloading is about the same method having different signatures. Overriding is about the same method, and same signature but different classes connected through inheritance.

2. Overloading is an example of compiler-time polymorphism and overriding is an example of run-time polymorphism.
FAQs on Java Method Overriding
Q1. What is Method Overriding?
As stated earlier, overridden methods allow Java to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods. Overridden methods are another way that Java implements the “one interface, multiple methods” aspect of polymorphism. Dynamic Method Dispatch is one of the most powerful mechanisms that object-oriented design brings to bear on code reuse and robustness. The ability to exist code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool. Overridden methods allow us to call methods of any of the derived classes without even knowing the type of derived class object.
Q2. When to apply Method Overriding? (with example)
Overriding and Inheritance: Part of the key to successfully applying polymorphism is understanding that the superclasses and subclasses form a hierarchy that moves from lesser to greater specialization. Used correctly, the superclass provides all elements that a subclass can use directly. It also defines those methods that the derived class must implement on its own. This allows the subclass the flexibility to define its methods, yet still enforces a consistent interface. Thus, by combining inheritance with overridden methods, a superclass can define the general form of the methods that will be used by all of its subclasses. Let’s look at a more practical example that uses method overriding. Consider an employee management software for an organization, let the code has a simple base class Employee, and the class has methods like raiseSalary(), transfer(), promote(), .. etc. Different types of employees like Manager, Engineer, ..etc may have their implementations of the methods present in base class Employee. In our complete software, we just need to pass a list of employees everywhere and call appropriate methods without even knowing the type of employee. For example, we can easily raise the salary of all employees by iterating through the list of employees. Every type of employee may have its logic in its class, we don’t need to worry because if raiseSalary() is present for a specific employee type, only that method would be called.

Java
class Employee {
public static int base = 10000 ;
int salary() { return base; }
}
class Manager extends Employee {
int salary() { return base + 20000 ; }
}
class Clerk extends Employee {
int salary() { return base + 10000 ; }
}
class Main {
static void printSalary(Employee e)
{
System.out.println(e.salary());
}
public static void main(String[] args)
{
Employee obj1 = new Manager();
System.out.print( "Manager's salary : " );
printSalary(obj1);
Employee obj2 = new Clerk();
System.out.print( "Clerk's salary : " );
printSalary(obj2);
}
}
|
Output
Manager's salary : 30000
Clerk's salary : 20000
Related Article
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!