Method overriding is a feature in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables the subclass to modify the behavior of a method inherited from the parent class.
Method overriding occurs when a subclass defines a method with the same signature (name, return type, and parameters) as a method in its superclass. When the method is called on an object of the subclass, the overridden version of the method is executed instead of the superclass version.
In Java, the overriding method must have the same name, return type, and parameters as the method in the superclass. You can use the @Override
annotation to indicate that a method is being overridden.
// Superclass class Animal { void sound() { System.out.println("Animals make a sound."); } } // Subclass class Dog extends Animal { @Override void sound() { System.out.println("Dog barks."); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); myAnimal.sound(); // Output: Animals make a sound. Dog myDog = new Dog(); myDog.sound(); // Output: Dog barks. } }
The method sound()
in the subclass Dog overrides the method with the same name in the superclass Animal. The @Override
annotation is used to ensure that the method is properly overridden.
Letβs consider an example with a class Animal and a subclass Dog. We'll demonstrate method overriding by modifying the behavior of the sound()
method:
// Superclass: Animal class Animal { void sound() { System.out.println("Animals make a sound."); } } // Subclass: Dog class Dog extends Animal { @Override void sound() { System.out.println("The dog barks."); } } public class Main { public static void main(String[] args) { Animal animal = new Animal(); animal.sound(); // Calls Animal's sound method Dog dog = new Dog(); dog.sound(); // Calls Dog's overridden sound method } }
In this case, when the sound()
method is called on an object of type Dog, the overridden version is executed, producing the output: "The dog barks."
Polymorphism allows you to call the overridden method through a reference of the superclass. This is particularly useful when the object is of a subclass type, but the reference is of the superclass type.
// Superclass: Animal class Animal { void sound() { System.out.println("Animals make a sound."); } } // Subclass: Dog class Dog extends Animal { @Override void sound() { System.out.println("The dog barks."); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); // Reference type is Animal, object is Dog animal.sound(); // Output: The dog barks. } }
Here, even though the reference type is Animal, the actual object is of type Dog, so the overridden method in the Dog class is called, demonstrating polymorphism in action.
Itβs important to understand the difference between method overriding and method overloading:
class Calculator { // Method overloading: same method name but different parameter types int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // Calls add(int, int) System.out.println(calc.add(5.5, 10.5)); // Calls add(double, double) } }
In the above example, the add()
method is overloaded, meaning there are two methods with the same name but different parameter types (one accepts integers, the other accepts doubles).
Method overriding allows subclasses to provide a specific implementation for methods defined in their parent classes. This is essential for achieving polymorphism and modifying inherited behavior. Always ensure that the method signature is identical in both the superclass and subclass to correctly override a method.
Quick Tip:
Use the @Override
annotation to make it clear that a method is being overridden. It helps avoid errors and ensures that the method is indeed overriding a parent method.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!