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
- Method must be inherited from parent class
- Method name, return type, and parameters must be the same
- Access level cannot be more restrictive than the parent method
@Overrideannotation is recommended (optional but improves readability)- 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
- 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
- Method must be inherited
- Same method name and parameters
- Return type must be same (or covariant type)
- Private, static, and final methods cannot be overridden
- 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
- Supports runtime polymorphism
- Provides specific implementation for child class
- Enhances code maintainability and flexibility
- 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
- Method Overriding → redefining parent method in child class
- Supports runtime polymorphism
- Rules: same method name, parameters, return type; cannot override final/static/private methods
- Enhances flexibility, reusability, and code maintainability