Sub topic 8
Overloading Is Not Overriding
Java has two features with similar names: overloading and overriding. They look alike in code, but Java handles them at different times. If you mix them up, you will guess the wrong output.
Two Different Ideas
Overloading means one class has several methods with the same name but different parameter lists. A parameter list is the types and number of values a method takes. For example, print(Object o) and print(String s) are overloads.
Overriding means a subclass replaces an inherited method that has the exact same name and parameter list. You met this in earlier chapters.
| Feature | Same name? | Same parameters? | Decided when? | Decided by |
|---|---|---|---|---|
| Overloading | Yes | No | Compile time | Declared types of the arguments |
| Overriding | Yes | Yes | Runtime | Runtime type of the object |
Compile time is when javac turns your code into a program. Runtime is when the program actually runs.
A Worked Example
This program uses both features at once.
class Printer {
void print(Object o) {
System.out.println("Printer: Object");
}
void print(String s) {
System.out.println("Printer: String");
}
}
class FancyPrinter extends Printer {
@Override
void print(Object o) {
System.out.println("FancyPrinter: Object");
}
}
public class Main {
public static void main(String[] args) {
Printer p = new FancyPrinter();
Object o = "hello";
p.print(o); // FancyPrinter: Object
p.print("hi"); // Printer: String
}
}Printer has two overloads of print. FancyPrinter overrides only one of them, print(Object).
Two Steps, Two Decisions
Java picks the method in two steps.
- The compiler picks the overload. It looks only at the declared types. In
p.print(o), the variableois declared asObject. So the compiler picksprint(Object). It does not care thatoreally holds aString. - The JVM picks the override. The JVM is the program that runs Java code. At runtime it looks at the real object in
p, which is aFancyPrinter. It runsFancyPrinter's version ofprint(Object).
For p.print("hi"), the argument "hi" is declared as a String. The compiler picks print(String). FancyPrinter did not override that one, so the JVM runs the version in Printer.
A Common Mistake
Suppose you meant to override print(Object) but typed print(Integer) in the subclass. That creates a new overload, not an override. Your method would never run through a Printer reference holding an Object. Putting @Override on it makes the compiler report an error, so you catch the mistake early.
Exercise
In the example, suppose main also runs String s = "yo"; p.print(s);. What does it print, and why?
Show answer
It prints "Printer: String". The variable s is declared as String, so the compiler picks print(String). FancyPrinter does not override print(String), so the JVM runs the Printer version.
Exercise
Is void print(Integer i) in FancyPrinter an override or an overload of Printer's methods?
Show answer
An overload. Its parameter type, Integer, does not match either inherited method's parameters, so it is a new method with the same name. Adding @Override would cause a compile error.