Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 9

Overloading vs Overriding

The comparison that settles half of all OOP exam questions

20m55 XP
→→→

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
1

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

AspectOverloadingOverriding
Purposesame operation, different inputssame operation, different behaviour per type
Whereusually one class (may also be inherited)always across parent → child
Needs inheritance?noyes (class or interface)
Method namesamesame
Parameter listmust differmust be identical
Return typefree (not part of the signature)same or covariant subtype
Access levelfreesame or wider, never narrower
Decidedat compile time by argument typesat run time by the object's class
Binding namestatic / early bindingdynamic / late binding
Polymorphism kindcompile-time polymorphismruntime polymorphism
@Override applies?no — it would be a compile erroryes — always write it
static methodscan be overloadedcannot be overridden (only hidden)
private / final methodscan be overloadedcannot 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

Main.java
1class Printer {
2 void print(String text) { }
3 void print(String text, int copies) { }
4}
  • Two distinct methods coexist
  • The compiler picks one from the arguments
  • No inheritance involved

Overriding

identical signature, child class

Main.java
1class ColorPrinter extends Printer {
2 @Override
3 void print(String text) { }
4}
  • One method, two implementations
  • The object picks one at run time
  • Inheritance is mandatory
Both at once — read the output carefully

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.

Output
Press Run to execute this example.

How to answer any 'which method runs?' question

  1. Look at the arguments and pick the matching signature — this is the compiler's job, using declared types only.
  2. Then look at the object (what new created) and take that class's version of the chosen signature — this is the JVM's job.
  3. If the class does not define it, walk up the inheritance chain until you find it.
  4. 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

WrongCalling a subclass method with extra parameters 'an override'.
WhyDifferent parameters → overload. The inherited method still exists untouched.
FixMatch the signature exactly and annotate with `@Override`.
WrongExpecting a `static` method to be overridden polymorphically.
WhyStatic methods are bound at compile time by the reference type — redeclaring one *hides* it.
FixUse instance methods for behaviour that must vary by type.
WrongSaying 'polymorphism' when you mean overloading in an interview.
WhyOverloading is only the compile-time flavour; interviewers are usually asking about dynamic dispatch.
FixName the kind explicitly: compile-time (overloading) vs runtime (overriding).

Lesson playground

Free practice

`report()` uses both mechanisms: the overload `fee(int)` and the override of `fee()`. Add a `StudentAccount` with a 2.0 fee and report it.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

Derived overrides only one of the two overloads. What is printed?

Main.java
1class Base {
2 void show(int value) {
3 System.out.println("Base int: " + value);
4 }
5
6 void show(String value) {
7 System.out.println("Base String: " + value);
8 }
9}
10
11class Derived extends Base {
12 @Override
13 void show(String value) {
14 System.out.println("Derived String: " + value);
15 }
16}
17
18public class Main {
19 public static void main(String[] args) {
20 Base b = new Derived();
21 b.show(5);
22 b.show("hello");
23 }
24}

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.