Java OOP · Module 9
Overloading vs Overriding
The comparison that settles half of all OOP exam questions
What you'll be able to do
- Contrast overloading and overriding on every axis that matters
- Predict output when both mechanisms appear in one program
- Detect an overload that was intended to be an override
- Say which decision the compiler makes and which the JVM makes
Overloading vs Overriding
Two mechanisms that only sound alike
Both reuse a method name. That is the only thing they have in common — everything else is different.
Overloading vs Overriding — full comparison
| Aspect | Overloading | Overriding |
|---|---|---|
| Purpose | same operation, different inputs | same operation, different behaviour per type |
| Where | usually one class (may also be inherited) | always across parent → child |
| Needs inheritance? | no | yes (class or interface) |
| Method name | same | same |
| Parameter list | must differ | must be identical |
| Return type | free (not part of the signature) | same or covariant subtype |
| Access level | free | same or wider, never narrower |
| Decided | at compile time by argument types | at run time by the object's class |
| Binding name | static / early binding | dynamic / late binding |
| Polymorphism kind | compile-time polymorphism | runtime polymorphism |
@Override applies? | no — it would be a compile error | yes — always write it |
| static methods | can be overloaded | cannot be overridden (only hidden) |
| private / final methods | can be overloaded | cannot be overridden |
If you remember only one row, remember the parameter list: overloading *requires* a difference, overriding *forbids* one.
Side by side
Overloading
different parameters, one class
- Two distinct methods coexist
- The compiler picks one from the arguments
- No inheritance involved
Overriding
identical signature, child class
- One method, two implementations
- The object picks one at run time
- Inheritance is mandatory
Why does the *inherited* two-argument method print the ColorPrinter text? Because inside it, `print(text)` is a call on `this`, and `this` is a ColorPrinter.
How to answer any 'which method runs?' question
- Look at the arguments and pick the matching signature — this is the compiler's job, using declared types only.
- Then look at the object (what
newcreated) and take that class's version of the chosen signature — this is the JVM's job. - If the class does not define it, walk up the inheritance chain until you find it.
- Remember: step 1 chooses the *signature*, step 2 chooses the *body*.
The trap: an overload hiding in a subclass
``java
class Animal { void sound() { } }
class Dog extends Animal {
void sound(int volume) { } // overload, NOT override
}
`
A Dog now has **two** methods: the inherited sound() and its own sound(int). dog.sound() runs the parent's version, which is almost never what the author intended. @Override` would have caught it instantly.
Prove it — coding challenges
Common mistakes
Calling a subclass method with extra parameters 'an override'.Expecting a `static` method to be overridden polymorphically.Saying 'polymorphism' when you mean overloading in an interview.Lesson playground
`report()` uses both mechanisms: the overload `fee(int)` and the override of `fee()`. Add a `StudentAccount` with a 2.0 fee and report it.
Check understanding
Quiz
Question 1 of 5
Derived overrides only one of the two overloads. What is printed?
Summary
- Overloading: same name, different parameters, chosen by the compiler.
- Overriding: same signature in a subclass, chosen by the object at run time.
- The compiler picks the signature; the JVM picks the body — in that order.
- `@Override` is your protection against writing an accidental overload.