๐ Introduction
In the previous articles of this series, we learned:
- What inheritance is
- Different types of inheritance in Java
Now comes one of the most important and powerful concepts in Java OOP:
๐ Method Overriding
Up to now, we saw that a child class can inherit methods from a parent class.
But what if the child class wants to change the behavior of that method?
For example:
- All animals make sound
- But dogs bark, cats meow
So the behavior is different, even though the method is the same.
This is exactly what method overriding solves.
๐ง What is Method Overriding?
Method overriding occurs when:
A child class provides its own implementation of a method that is already defined in the parent class.
In simple words:
๐ Child class replaces parent class method behavior.
๐งฉ Real-Life Analogy
Think of a parent class = Animal
It has a method:
makeSound()
Now:
- Dog โ barks
- Cat โ meows
Same method name โ different behavior
๐ This is method overriding.
๐ป Basic Example of Method Overriding
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
๐ Line-by-Line Explanation
Parent class:
class Animal
This is the base class.
Method in parent:
void sound()
This method defines a general behavior.
Child class:
class Dog extends Animal
Dog inherits from Animal.
Overridden method:
void sound()
Same method name as parent.
๐ This is method overriding.
New behavior:
System.out.println("Dog barks");
Dog changes the behavior of sound().
๐งพ Output
Dog barks
๐ Parent method is not executed, child version runs.
โ๏ธ Important Rules of Method Overriding
โ Rule 1: Method Name Must Be Same
void sound()
If the name changes โ it is NOT overriding.
โ Rule 2: Parameters Must Be Same
void sound()
If parameters differ โ it becomes method overloading, not overriding.
โ Rule 3: Return Type Must Be Same (or compatible)
int getValue()
Return type must match or be covariant.
โ Rule 4: Access Modifier Rules
Child class method cannot be more restrictive.
| Parent | Child Allowed |
|---|---|
| public | public |
| protected | protected / public |
โ Rule 5: Cannot Override Certain Methods
โ final methods
โ static methods
โ private methods
๐ Runtime Polymorphism (Most Important Concept)
This is where method overriding becomes powerful.
๐ป Example
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
๐ Explanation
Animal a = new Dog();
๐ Reference type = Animal
๐ Object type = Dog
a.sound();
Which method will run?
๐ Output:
Dog barks
๐ง Why?
Because Java decides the method at runtime, not compile time.
This is called:
๐ Runtime Polymorphism
๐ท Using @Override Annotation
@Override
void sound() {
System.out.println("Dog barks");
}
โ Why Use It?
โ Avoids mistakes
โ Ensures method is actually overridden
โ Improves readability
โ ๏ธ If Method is Wrong
@Override
void sounds() { }
โ Compiler error
๐ Helps catch mistakes early
๐ Method Overriding vs Method Overloading
| Feature | Overriding | Overloading |
|---|---|---|
| Class | Different classes | Same class |
| Method name | Same | Same |
| Parameters | Same | Different |
| Purpose | Change behavior | Multiple versions |
๐งจ Tricky Interview Example
class A {
void show() {
System.out.println("Class A");
}
}
class B extends A {
void show() {
System.out.println("Class B");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
๐ง Output
Class B
๐ Even though reference is A, object is B.
โ ๏ธ Common Mistakes & Confusions
โ Mistake 1: Changing Parameters
void sound(int x)
โ Not overriding
โ Overloading
โ Mistake 2: Static Methods
static void show()
โ Static methods are not overridden
โ They are hidden
โ Mistake 3: Private Methods
private void show()
โ Not inherited โ cannot override
โ Mistake 4: Thinking Compile-Time Decision
Many students think:
Method is chosen based on reference type
โ Wrong
๐ It is chosen based on object type at runtime
โ Mistake 5: Forgetting @Override
Leads to silent bugs.
๐ฏ Real-Life Example
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with key");
}
}
class Bike extends Vehicle {
@Override
void start() {
System.out.println("Bike starts with kick");
}
}
๐ง Key Points to Remember
โ Method overriding happens in inheritance
โ Same method โ different behavior
โ Achieved at runtime
โ Enables polymorphism
โ @Override is recommended
โ Cannot override final, static, private
๐ Conclusion
Method overriding is one of the most powerful features of Java, allowing developers to create programs that are flexible, dynamic, and scalable.
It enables a child class to customize or replace the behavior of a parent class method, making programs more realistic and adaptable.
In this article, we covered:
- What method overriding is
- How it works
- Rules of overriding
- Runtime polymorphism
@Overrideannotation- Common mistakes and confusions
Understanding method overriding is crucial because it forms the foundation of runtime polymorphism, which is heavily used in real-world applications and interviews.