Overriding in Java (original) (raw)

Last Updated : 13 May, 2026

Method overriding in Java allows a subclass to provide a specific implementation of a method that is already defined in its parent class. It is one of the key features of runtime polymorphism in object-oriented programming.

class Animal {

void move(){
    System.out.println(
  "Animal is moving."); 
    
}
void eat(){
    
    System.out.println(
  "Animal is eating."); 
    
}

}

class Dog extends Animal{

@Override void move(){ 
    
  // move method from Base class is overriden in this
  // method
    System.out.println("Dog is running.");
}
void bark(){
    
    System.out.println("Dog is barking."); 
    
}

}

public class Geeks { public static void main(String[] args) { Dog d = new Dog(); d.move(); d.eat(); d.bark(); } }

`

Output

Dog is running. Animal is eating. Dog is barking.

**Explanation: The Animal class defines base functionalities like move() and eat(). The Dog class inherits from Animal and overrides the move() method to provide a specific behavior Dog is running. Both classes can access their own methods. When creating a Dog object, calling move() executes the overridden method.

Rules for Method Overriding

Method Overriding in Java

Special Cases in Overriding

1. Calling Parent Method Using super

The super keyword can invoke the parent class method from the overriding method.

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()");
}

}

public class Main{

public static void main(String[] args){
    
    Parent obj = new Child();
    obj.show();
}

}

`

Output

Parent's show() Child's show()

**2. Final Methods Cannot 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{

// Can't be overridden
final void show(){
    
}

}

class Child extends Parent{

// This would produce error
void show() {}

}

`

**Output:

MethodOverriding

3. Static Methods

class Parent{ static void staticMethod(){

    System.out.println("Parent static method");
}
void instanceMethod(){
    
    System.out.println("Parent instance method");
}

}

class Child extends Parent{

static void staticMethod(){
    
    // Hides Parent's static method
    System.out.println("Child static method");
}
@Override
void instanceMethod(){ 
    
    // Overrides Parent's instance method
    System.out.println("Child instance method");
}

}

public class GFG{

public static void main(String[] args){
    
    Parent p = new Child();
    
    // Calls Parent's static method (hiding)
    p.staticMethod();
    
    // Calls Child's overridden instance method
    p.instanceMethod(); 
}

}

`

Output

Parent static method Child instance method

4. Private Methods

class Parent{

private void display(){
    
    System.out.println("Parent private method");
}

}

class Child extends Parent{

void display(){
    
    // This is a new method, not overriding
    System.out.println("Child method");
}

}

public class GFG{

public static void main(String[] args){
    
    Child c = new Child();
    
     // Calls Child's method
    c.display(); 
}

}

`

5. Covariant Return Types

class Parent{

Parent getObject(){
    
    System.out.println("Parent object");
    return new Parent();
}

}

class Child extends Parent{

@Override

// Covariant return type
Child getObject() {  
    System.out.println("Child object");
    return new Child();
}

}

public class GFG{

public static void main(String[] args){
    
    Parent obj = new Child();
    
    // Calls Child's method
    obj.getObject();  
    
}

}

`

Exception-Handling in Overriding

import java.io.IOException;

class Parent { void display() throws IOException { System.out.println("Parent method"); } }

class Child extends Parent { @Override void display() throws IOException { System.out.println("Child method"); } }

public class GFG{

public static void main(String[] args){
    
    // Parent reference, Child object
    Parent obj = new Child(); 
    try{
        
        // Calls Child's overridden method
        obj.display(); 
    } catch (IOException e){
        
        System.out.println("Exception caught: " + e.getMessage());
    }
}

}

`

Real-Life Example: Employee Management System

Let’s understand overriding with a real-world analogy.

Imagine an organization’s Employee Management System. All employees share some behaviors like raiseSalary() and promote(), but the logic differs for different roles like Manager or Engineer. We can create a single Employee array where individual employees are of different types (sales, tech, etc) and call their functions. This simplifies the overall code a lot.

Java `

abstract class Employee {

abstract void raiseSalary();
abstract void promote();

}

class Manager extends Employee{

@Override void raiseSalary(){
    
    System.out.println(
        "Manager salary raised with incentives.");
}

@Override void promote(){
    
    System.out.println(
        "Manager promoted to Senior Manager.");
}

}

class Engineer extends Employee{

@Override void raiseSalary(){
    
    System.out.println(
        "Engineer salary raised with bonus.");
}

@Override void promote(){
    
    System.out.println(
        "Engineer promoted to Senior Engineer.");
}

}

public class Company{

public static void main(String[] args){
    
    Employee[] employees
        = { new Manager(), new Engineer() };

    System.out.println("--- Raising Salaries ---");
    for (Employee e : employees){
        
        e.raiseSalary(); 
    }

    System.out.println("\n--- Promotions ---");
    for (Employee e : employees) {
        e.promote();
    }
}

}

`

Output

--- Raising Salaries --- Manager salary raised with incentives. Engineer salary raised with bonus.

--- Promotions --- Manager promoted to Senior Manager. Engineer promoted to Senior Engineer.

**Explanation: Although both Manager and Engineer objects are referred to using the Employee type, Java calls the overridden methods of the actual objects at runtime, demonstrating dynamic method dispatch (runtime polymorphism).

**Related Article: Method Overloading and Method Overriding