Method Overriding in Java
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass, using the exact same method...
Replacing Parent Behavior in a Subclass
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass, using the exact same method signature: same name, same parameter list, and a compatible return type. Overriding is the mechanism that makes runtime polymorphism work in Java. When you call a method through a parent-type reference that actually points to a child object, Java's JVM calls the child's version at runtime rather than the parent's.
Rules for Valid Overriding The overriding method must have the same name and parameter types as the method in the parent class. The return type must be the same or a subtype (covariant return). The access level can be widened—protected in the parent can become public in the child—but not narrowed. The overriding method cannot declare checked exceptions that the parent method did not declare. Private methods cannot be overridden because they are invisible to subclasses. Static methods are hidden, not overridden.
The @Override Annotation Placing @Override above an overriding method is a best practice. If the annotation is present and the method does not actually override anything in a superclass or interface—perhaps because of a typo in the method name—the compiler reports an error immediately. Without @Override, the same typo would silently create a new method rather than overriding the intended one, leading to a subtle runtime bug where the parent's behavior runs unexpectedly.
Using super to Invoke the Parent Version Inside an overriding method, you can still call the parent's version using super.methodName(). This is useful when you want to extend the parent's behavior rather than replace it entirely: call super.save() first to handle the parent's saving logic, then add the child-specific saving logic afterward. You cannot skip a level and call a grandparent's version directly; you can only call super in the immediate parent.
Code Example: class Vehicle { String describe() { return "I am a vehicle."; } }
class ElectricCar extends Vehicle { @Override String describe() { return super.describe() + " I run on electricity."; } }
Vehicle v = new ElectricCar(); System.out.println(v.describe()); // I am a vehicle. I run on electricity.
Interview Tip: Know the precise difference between overriding and hiding. When an instance method is overridden, the child's version is called regardless of the reference type—this is dynamic dispatch. When a static method is "overridden" (hidden), the version called depends on the declared type of the reference at compile time—this is static dispatch. The distinction matters for exam questions involving static methods in inheritance hierarchies.