Understanding Method Overriding

Method overriding in Java allows a subclass to provide a specific implementation of a method that is already defined in its superclass. To override a method, the subclass must declare a method with the same name, return type, and parameter list as the one in the superclass. The method in the subclass can then provide its own implementation, which replaces the implementation provided by the superclass. This allows for polymorphic behavior, where the appropriate method to execute is determined at runtime based on the actual type of the object. Overriding is a key feature of achieving runtime polymorphism in Java's object-oriented programming paradigm.

  • Basic Syntax of Method Overriding:
  • In Java, method overriding involves creating a new implementation for a method already defined in a superclass. The basic syntax for method overriding is as follows:

    class Superclass {
      returnType methodName(parameters) {
        // Superclass method implementation
      }
    }
    class Subclass extends Superclass {
      returnType methodName(parameters) {
        // Subclass method implementation (overrides superclass method)
      }
    }

    1.Define a superclass with a method that you intend to override.
    2.Create a subclass that extends the superclass.
    3.Define a method in the subclass with the same name, return type, and parameter list as the method in the superclass.
    4.Provide a new implementation for the method in the subclass. This implementation will replace the one in the superclass.
    When an instance of the subclass is used, calling the overridden method will execute the implementation provided in the subclass, rather than the one in the superclass. This dynamic dispatch of method calls is a fundamental feature of polymorphism in Java.


  • Examples:
  • Solution

    // Superclass
    class Animal { void sound() { System.out.println("Animal makes a sound"); } } // Subclass class Dog extends Animal { //Override void sound() { System.out.println("Dog barks"); } } // Main class class Main { public static void main(String[] args) { Animal animal = new Animal(); animal.sound(); Dog dog = new Dog(); dog.sound(); } }

    OUTPUT:
    Animal makes a sound
    Dog barks

    i.We have a superclass Animal with a method sound(), which prints "Animal makes a sound".
    ii.We then create a subclass Dog which extends Animal.
    iii.In the Dog class, we override the sound() method with a new implementation that prints "Dog barks".
    iv.In the main method, we create instances of both Animal and Dog. When we call the sound() method on each object, the appropriate implementation is executed based on the actual type of the object: "Animal makes a sound" for Animal and "Dog barks" for Dog. This demonstrates method overriding in action.


  • Advantages of method overriding:
  • Method overriding in Java offers several advantages:
    1.Polymorphism: Method overriding is a fundamental feature of polymorphism in Java. It allows different classes to provide their own implementation of methods with the same name and signature, enabling code to work with objects of different types through a common interface.
    2.Run-time Polymorphism:Method overriding enables dynamic dispatch, where the appropriate method implementation to execute is determined at runtime based on the actual type of the object. This provides flexibility and allows for more dynamic behavior in the program.
    3.Code Reusability: By overriding methods defined in a superclass, subclasses can reuse the behavior implemented in the superclass without duplicating code. This promotes code reuse and reduces redundancy in the codebase.

  • Conclusion of method overriding:
  • In Java, method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This facilitates polymorphic behavior, where a subclass object can be treated as its superclass type. Key points include ensuring method signature compatibility, dynamic method resolution at runtime, and the ability to invoke the subclass's method through a superclass reference. This mechanism enhances code reusability, extensibility, and flexibility in object-oriented programming.