Method overriding in Java is a powerful feature that allows a subclass to provide its own implementation of a method defined in its superclass, essentially overriding the superclass's method. This is particularly useful when the default behavior of the superclass method is not suitable for the subclass. Let's explore method overriding with examples inspired by w3schools tutorials.

Java fosters code reusability and promotes object-oriented programming through concepts like inheritance and polymorphism, with method overriding being a critical facet of the latter. By overriding a method, a subclass can modify its behavior while maintaining the same method signature, potentially enhancing performance or tailoring functionality.

Understanding Method Overriding
To grasp method overriding, let's first consider a simple example of a superclass and a subclass. Imagine we have a Animal superclass and a Dog subclass:

public class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("The dog barks");
}
}
Overriding the makeSound() Method

The Dog class extends the Animal class and overrides the makeSound() method. When the makeSound() method is called on a Dog object, the subclass's implementation (i.e., it barks) will be executed, not the superclass's implementation.
Let's verify this with a simple program:
Animal myAnimal = new Animal();
Animal myDog = new Dog();
myAnimal.makeSound(); // Output: "The animal makes a sound"
myDog.makeSound(); // Output: "The dog barks"

Method Overriding Rules
To ensure proper functioning, method overriding in Java adheres to certain rules. Key rules include:
- The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.
- The access modifier of the subclass's method must be the same or broader than the superclass's method. For example, you can't change a superclass public method to private in the subclass.
- The subclass cannot reduce the visibility of the method (e.g., from public to private or protected).

The compiler enforces these rules to maintain the integrity of method overriding.
Method Overriding vs. Method Overloading



















Before we delve deeper into method overriding, let's differentiate it from method overloading, another crucial concept in Java.
Method overloading, also known as compiler-time polymorphism, allows multiple methods to exist in the same class with different parameters. On the contrary, method overriding, or runtime polymorphism, lets a subclass provide a different implementation of a method inherited from its superclass.
Key Differences
Here's how method overriding and method overloading differ:
- Method Overriding: Occurs between a superclass and its subclass. The subclass provides a different implementation of the same method as the superclass, maintaining the method signature.
- Method Overloading: Occurs within the same class. Multiple methods can have the same name but different parameters, enabling the methods to behave differently depending on the arguments passed during invocation.
Now that you understand the basic differences between method overriding and method overloading, let's explore some advanced scenarios in method overriding.
Method Overriding with Object Class Methods
In Java, every class is a subclass of the Object class, which serves as the root of the class hierarchy. The Object class provides several methods that you can override to customize your class's behavior. These methods include equals(), hashCode(), toString(), and others. Let's demonstrate this with the equals() method:
public class Student {
private String name;
private int id;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Student student = (Student) obj;
return id == student.id && Objects.equals(name, student.name);
}
}
Here, the Student class overrides the equals() method from the Object class to compare two Student objects based on their name and id fields.
Method overriding is an essential aspect of Java programming, enabling developers to create reusable and highly customizable code. By mastering method overriding, you can design efficient and robust applications that leverage the full power of object-oriented programming.