JAVA Tutorial



METHOD OVERRIDING IN JAVA


Method Overriding in Java

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.

What is Method Overriding?

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.

Why Use Method Overriding?

  • Dynamic Polymorphism: Method overriding enables dynamic method dispatch, meaning the method that gets called is determined at runtime based on the object's actual class.
  • Modify Parent Class Behavior: Overriding allows subclasses to change or extend the behavior of methods in the parent class, enabling more specific functionality.
  • Code Reusability: By overriding methods, we can reuse the code in the superclass and only modify what is necessary in the subclass.

Syntax of Method Overriding

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.

Key Points of Method Overriding

  • Same Method Signature: The method in the subclass must have the same method signature as in the superclass (same name, return type, and parameters).
  • Cannot Override Static Methods: Static methods belong to the class, not instances, so they cannot be overridden. Instead, they can be hidden.
  • Access Modifiers: The access level of the overriding method cannot be more restrictive than the method being overridden. For example, you can't change a public method to private.
  • Constructor Cannot Be Overridden: Constructors are not inherited, so they cannot be overridden.

Method Overriding Example: Dog and Animal

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."

Method Overriding with Polymorphism

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.

Overriding vs Overloading

It’s important to understand the difference between method overriding and method overloading:

  • Method Overriding: Occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.
  • Method Overloading: Occurs when a class has multiple methods with the same name but different parameter lists.

Method Overloading Example

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).

Summary

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.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review