Overriding and Overloading are two forms of Polymorphism that students frequently confuse in exam code traces. This deep dive breaks down the rules, compiler traps, and memory dispatch mechanics once and for all.
Imagine a coffee machine with buttons labeled brew(), brew(Sugar), and brew(Sugar, Milk). They are all in the SAME machine. They share the SAME action name (brew), but take DIFFERENT inputs. The compiler knows at compile time exactly which button you pressed based on the ingredients you handed it!
Imagine your base phone (Parent class) has a method displayNotification() that shows a plain white popup. When you upgrade to iOS 18 (Subclass), it replaces / overrides displayNotification() with a slick Dynamic Island widget. The signature is EXACTLY the same, but the subclass provides a custom, upgraded implementation that replaces the parent's logic at runtime!
Examiners love testing boundary cases where code fails to compile due to subtle rule violations. Memorize this comparison matrix:
| Rule Category | Method Overloading (Compile-Time) | Method Overriding (Runtime) |
|---|---|---|
| Where It Occurs | Within the same class (or inherited methods). | Between Parent Class & Child Subclass (or Interface & Implementation). |
| Method Name | MUST be identical. | MUST be identical. |
| Parameter List | MUST be different (count, order, or data types). | MUST be exact same count, types, and order. |
| Return Type | Can be anything (different or same). Cannot overload on return type alone! | MUST be same or a Covariant Subtype (e.g. parent returns Animal, child returns Dog). |
| Access Modifiers | Can be changed freely (private, public, etc.). |
CANNOT reduce visibility! (e.g., parent public cannot become child protected or private). |
| Exception Declarations | Can throw any checked or unchecked exceptions. | Child CANNOT declare broader or new checked exceptions than declared by parent! |
| Static / Final / Private Methods | Static, final, and private methods CAN be overloaded. | Static methods undergo Method Hiding (not overriding!). Final & Private methods CANNOT be overridden. |
The code below causes a COMPILE-TIME ERROR:
The compiler determines overloading strictly by the Method Signature (Method Name + Parameter List). It ignores return type when matching!
In Part 2.1 exams, you will get polymorphic assignment questions like: Parent p = new Child();
How does Java decide which version of a method or field to invoke? Here is the golden rule:
new).=).When you call an overloaded method like calc(5), Java resolves the best match in this strict sequence:
calc(int))int widens to long -> float -> double)int boxes to Integer)calc(int... args) - lowest priority!)
Parent class has: protected void compute() {}
Child class attempts: void compute() {} (package-private).
Does this code compile? Explain why or why not.
protected is more accessible than package-private (default). To override correctly, the child method must be marked either protected or public.
Parent has: public void readData() throws IOException {}
Child has: @Override public void readData() throws Exception {}
Does this compile? Explain the rule.
Exception is the superclass of IOException (broader exception). An overridden method in a subclass CANNOT declare broader checked exceptions than the superclass method! It can only throw the same exception, a subtype exception (e.g. FileNotFoundException), or no checked exception at all.
Parent: public Animal getPet() { return new Animal(); }
Child: @Override public Dog getPet() { return new Dog(); } (where Dog extends Animal).
Is this valid Java code?
Dog is a subtype of Animal, returning a more specific subtype in the overriding method is 100% legal and recommended.