Overriding methods in Java is a fundamental concept that enables developers to modify the behavior of base class methods within a subclass. This functionality lies at the heart of inheritance, one of the four core principles of object-oriented programming (OOP).

Consider a scenario where you need to alter the implementation of a method present in a superclass, but retain the existing logic for other methods. Instead of creating a new method or manipulating the superclass, method overriding provides a clean, efficient solution.

Understanding Method Overriding in Java
Method overriding in Java allows a subclass to provide its own implementation of a method that is already present in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. However, the access modifier of the overridden method in the subclass can be broader or the same but cannot be narrower than the superclass method.

When a method is invoked on an object, the Java Virtual Machine (JVM) determines which method to execute based on the object's dynamic type at runtime. This feature is known as dynamic method dispatch.
Method Hiding vs. Method Overriding

It's crucial to distinguish between method hiding and method overriding, as the two concepts are sometimes confused. Method hiding occurs when a variable in a subclass hides a variable or method in the superclass, leading to a compile-time error if the hidden variable is accessed through a superclass reference. In contrast, method overriding involves replacing the logic of a superclass method with new logic in the subclass.
In method hiding, the subclass variable is a new variable, distinct from the superclass variable. In method overriding, the subclass method is a new implementation of the same method present in the superclass.
Key Rules for Method Overriding in Java

To ensure method overriding works correctly, it's essential to follow these rules:
- The method in the subclass must have the same name, return type, and parameters as the method in the superclass. Changing any of these characteristics results in a new method, not method overriding.
- The access modifier of the overridden method in the subclass cannot be narrower than the superclass method. If the access modifier in the superclass is protected, the subclass can use protected or public, but not private or default (package-private).
- The return type of the overridden method in the subclass must be the same or a subclass of the superclass's return type. Providing a return type that is not compatible with the superclass method leads to a compile-time error.
- The exception declarations in the overridden method cannot be broader than the superclass method. Adding checked exceptions to the subclass method that are not declared in the superclass method results in a compile-time error. However, the subclass can add unchecked exceptions or reduce the number of exceptions compared to the superclass method.
Breaking these rules can lead to runtime errors, unexpected behavior, or compile-time errors, which can make debugging and maintaining the codebase more challenging.

Method Overriding Examples in Java
Let's consider a real-world example to illustrate method overriding in action. Suppose you're working on an e-commerce application with a base class called Product and various subclasses like Book, Electronic, and Clothing.



















The Product class might have a method called displayDetails() that prints some basic product information. Each subclass can override this method to provide a more specialized implementation of displayDetails() suitable for their unique data and requirements.
Overriding displayDetails() for Books
In the Book subclass, the displayDetails() method might include additional information like the author's name and the number of pages.
```java public class Book extends Product { private String author; private int pages; public Book(String title, String author, int pages) { super(title); this.author = author; this.pages = pages; } @Override public void displayDetails() { super.displayDetails(); System.out.println("Author: " + author); System.out.println("Pages: " + pages); } } ```
By overriding the displayDetails() method in the Book class, we can provide a more specialized implementation that displays both the basic product information and book-specific details like the author's name and the number of pages.
Method overriding plays a vital role in Java, enabling developers to create powerful, expressive, and maintainable code. By using this concept, you can ensure that your code is extensible, modular, and easy to understand. Embrace method overriding as an essential tool in your toolbox, and harness its power to create robust, efficient, and flexible applications.
Now that you've learned the intricacies of method overriding in Java, it's time to put this knowledge to use in your next development project. Experiment with method overriding in various contexts, and watch as your code becomes more nuanced and refined. Happy coding!