Java Method overriding
Method overriding in Java is a feature that allows a subclass (or derived class) to provide a specific implementation of a method that is already defined in its superclass (or base class). When a method is overridden, the subclass version of the method is called instead of the superclass version when an instance of the subclass is used. This allows for dynamic method dispatch, where the method that gets executed is determined at runtime based on the object's actual type.
Key Characteristics of Method Overriding:
Same Method Name:
- The method in the subclass must have the same name as the method in the superclass.
Same Parameter List:
- The parameter list (type and number of parameters) in the overriding method must be identical to that of the overridden method.
Return Type:
- The return type of the overriding method can be the same as or a subtype (covariant return type) of the return type declared in the original overridden method.
Access Modifiers:
- The access modifier of the overriding method must be the same or more accessible than that of the overridden method. For example, if the superclass method is
protected
, the overriding method in the subclass can beprotected
orpublic
, but notprivate
.
- The access modifier of the overriding method must be the same or more accessible than that of the overridden method. For example, if the superclass method is
Dynamic Method Dispatch:
- Method overriding supports runtime polymorphism. The method that gets executed is determined at runtime based on the actual object type, not the reference type.
Example of Method Overriding
class Animal {
// Method in the superclass
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overriding the sound method in the subclass
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
// Overriding the sound method in the subclass
@Override
void sound() {
System.out.println("Cat meows");
}
}
// Main class to demonstrate method overriding
public class Main {
public static void main(String[] args) {
Animal myAnimal; // Declare an Animal reference
myAnimal = new Dog(); // Instantiate Dog
myAnimal.sound(); // Output: Dog barks
myAnimal = new Cat(); // Instantiate Cat
myAnimal.sound(); // Output: Cat meows
}
}
Explanation:
- In this example, we have a superclass called
Animal
with a methodsound()
. The subclassesDog
andCat
both override thesound()
method to provide their specific implementations. - In the
main
method, anAnimal
reference is used to hold objects ofDog
andCat
. Whensound()
is called onmyAnimal
, the overridden method in the actual object type (eitherDog
orCat
) is executed, demonstrating dynamic method dispatch.
Advantages of Method Overriding:
Runtime Polymorphism:
- Method overriding supports runtime polymorphism, allowing for more flexible and dynamic code.
Code Reusability:
- It allows subclasses to inherit and extend the behavior of their superclass without modifying the original class.
Improved Code Maintenance:
- Changes in behavior can be made at the subclass level, promoting easier maintenance and updates to the code.
Summary:
- Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
- It is characterized by the same method name, same parameter list, and the ability to change the implementation while maintaining the same interface.
- Overriding supports runtime polymorphism, enhancing flexibility and reusability in object-oriented programming.