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:

  1. Same Method Name:

    • The method in the subclass must have the same name as the method in the superclass.
  2. Same Parameter List:

    • The parameter list (type and number of parameters) in the overriding method must be identical to that of the overridden method.
  3. 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.
  4. 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 be protected or public, but not private.
  5. 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 method sound(). The subclasses Dog and Cat both override the sound() method to provide their specific implementations.
  • In the main method, an Animal reference is used to hold objects of Dog and Cat. When sound() is called on myAnimal, the overridden method in the actual object type (either Dog or Cat) is executed, demonstrating dynamic method dispatch.

Advantages of Method Overriding:

  1. Runtime Polymorphism:

    • Method overriding supports runtime polymorphism, allowing for more flexible and dynamic code.
  2. Code Reusability:

    • It allows subclasses to inherit and extend the behavior of their superclass without modifying the original class.
  3. 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.