Sub topic 2
How Java Picks the Method at Runtime
Dynamic Dispatch
When you call a method on an object, Java waits until the program runs to choose which version to use. This is called dynamic dispatch. Java looks at the object's real class, which is its runtime type. It does not look at the variable's declared type.
class Account {
String describe() {
return "Account";
}
}
class SavingsAccount extends Account {
@Override
String describe() {
return super.describe() + " (savings)";
}
}
public class Main {
public static void main(String[] args) {
Account a = new SavingsAccount();
System.out.println(a.describe()); // Account (savings)
}
}The variable a is declared as Account, but the object is a SavingsAccount. Java therefore runs the SavingsAccount version.
Extending Behavior with super
The keyword super refers to the parent class. Inside an override, super.describe() runs the parent's version. You can reuse that result and add to it, as the example above does.
The Rules of Overriding
The compiler checks every override against these rules:
| Rule | Allowed | Not allowed |
|---|---|---|
| Method name and parameters | Exactly the same | Different parameters |
| Return type | Same type, or a subtype | An unrelated type |
| Access | Same, or more open (`protected` to `public`) | More closed (`public` to `private`) |
| Checked exceptions | Same, narrower, or none | New or broader ones |
Overloading Is Not Overriding
Overloading means writing two methods with the same name but different parameters. The compiler picks an overload before the program runs, using the declared types of the arguments. After that, Java picks the override at runtime.
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
}
}The variable o holds a String, but its declared type is Object. So the compiler chooses print(Object), and then Java runs the FancyPrinter version of it.
What Cannot Be Overridden: static, private, and final
- A static method belongs to the class, not to an object. If a subclass writes a static method with the same name, it hides the parent's method. Java chooses it from the declared type.
- A private method is invisible to subclasses. A method with the same name in a subclass is a new, separate method.
- A final method is locked. Trying to override it is a compile error.
Exercise
A variable is declared as Printer p = new FancyPrinter();. Which method runs for p.print("hi"), and why?
Show answer
Printer.print(String) runs. The compiler picks the print(String) overload because the argument's declared type is String. FancyPrinter does not override print(String), so the Printer version runs.
Exercise
Can a subclass override a public method and make it protected?
Show answer
No. An override can keep the same access or make it more open, but it cannot make it more restricted.