Java Method Overriding – Complete Guide with Examples


Learn Java method overriding, how to redefine parent class methods in child classes, its rules, advantages, and examples to implement runtime polymorphism in object-oriented programming.

Method Overriding in Java – Complete Detailed Tutorial

Method Overriding is a feature in Java that allows a child class to provide its own implementation of a method already defined in the parent class.

It is an essential concept for runtime polymorphism.

1. Key Points About Method Overriding

  1. Method must be inherited from parent class
  2. Method name, return type, and parameters must be the same
  3. Access level cannot be more restrictive than the parent method
  4. @Override annotation is recommended (optional but improves readability)
  5. Can override non-static and non-final methods only

2. Syntax of Method Overriding


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

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

3. Example – Basic Method Overriding


class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
Animal a = new Animal();
a.sound(); // calls parent method

Dog d = new Dog();
d.sound(); // calls overridden method
}
}

Output:


Animal makes a sound
Dog barks

4. Example – Using Parent Reference for Child Object

  1. Runtime polymorphism occurs when parent reference points to child object

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}

public class Main {
public static void main(String[] args) {
Animal a = new Cat(); // parent reference to child object
a.sound(); // calls overridden method (runtime polymorphism)
}
}

Output:


Cat meows

5. Rules of Method Overriding

  1. Method must be inherited
  2. Same method name and parameters
  3. Return type must be same (or covariant type)
  4. Private, static, and final methods cannot be overridden
  5. Access modifier: child ≥ parent (cannot reduce visibility)

6. Example – Access Modifiers in Overriding


class Parent {
protected void show() {
System.out.println("Parent show");
}
}

class Child extends Parent {
public void show() { // allowed: wider access
System.out.println("Child show");
}
}

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

Output:


Child show

7. Advantages of Method Overriding

  1. Supports runtime polymorphism
  2. Provides specific implementation for child class
  3. Enhances code maintainability and flexibility
  4. Allows dynamic method dispatch

8. Example – Using Method Overriding with Super Keyword


class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
super.sound(); // call parent method
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}

Output:


Animal makes a sound
Dog barks

9. Summary

  1. Method Overriding → redefining parent method in child class
  2. Supports runtime polymorphism
  3. Rules: same method name, parameters, return type; cannot override final/static/private methods
  4. Enhances flexibility, reusability, and code maintainability