Java Method Overriding
Java Method Overriding is an important feature of Object-Oriented Programming (OOP) that allows a child class to provide its own implementation of a method already defined in its parent class. It is the foundation of runtime polymorphism and enables Java programs to execute different behaviors for different objects using the same method name.
Method overriding is commonly used when a child class needs to customize or extend the behavior inherited from its parent class while keeping the same method signature.
What is Method Overriding in Java?
Method overriding occurs when a subclass defines a method with the same name, return type, and parameter list as a method in its superclass. When the overridden method is called using a parent class reference, Java executes the child class version at runtime.
Syntax of Method Overriding
class Parent {
void display()
{
}
}
class Child extends Parent {
@Override
void display()
}
{
}
Java Method Overriding Example
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 obj = new Dog();
obj.sound();
}
}
How Method Overriding Works
When a parent class reference points to a child class object, Java checks the actual object type during runtime. If the child class has overridden the method, the JVM executes the child class implementation instead of the parent class method. This process is called dynamic method dispatch.
Rules for Method Overriding
- The parent and child methods must have the same method name.
- The parameter list must be identical.
- The return type should be the same or covariant.
- The child method cannot have more restrictive access than the parent method.
- Only inherited methods can be overridden.
- Static, final, and private methods cannot be overridden.
- Constructors cannot be overridden.
The @Override Annotation
The @Override annotation tells the compiler that a method is intended to
override a method from the parent class. If the method signature is incorrect, the
compiler generates an error, helping developers avoid mistakes.
class Animal
{
void sound() {
System.out.println("Animal");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog");
}
}
Advantages of Method Overriding
Runtime Polymorphism
Allows methods to behave differently depending on the object.
Flexibility
Child classes can customize inherited behavior.
Code Reusability
Existing parent class functionality can be reused.
Maintainability
Makes large Java applications easier to update and extend.
Method Overriding vs Method Overloading
| Method Overriding | Method Overloading |
|---|---|
| Occurs between parent and child classes. | Occurs within the same class. |
| Requires inheritance. | Inheritance is not required. |
| Runtime polymorphism. | Compile-time polymorphism. |
| Same method signature. | Different parameter lists. |
Real-World Example
Consider a banking application where the parent class Account has a
method named calculateInterest(). Different account types such as
SavingsAccount and CurrentAccount override this method
to calculate interest using different formulas. The application simply calls
calculateInterest(), and the correct implementation executes automatically.
Summary
Java Method Overriding enables a child class to provide its own implementation of a parent class method. It is the basis of runtime polymorphism and dynamic method dispatch. By using method overriding, developers can build flexible, reusable, and maintainable object-oriented applications while allowing child classes to customize inherited behavior.