04 · Java language in depth / LESSON 11 OF 102
Inheritance, overriding, and polymorphism
Polymorphism lets callers use a contract while runtime dispatch selects behavior.
Before this lesson: Packages, imports, and access modifiers
Understand the topic
An overriding instance method is selected from the runtime object. An overloaded method is selected at compile time using the declared argument types. Keep overrides compatible with the parent contract; a subtype that throws for valid parent inputs breaks substitutability.
Use inheritance when the relationship is genuinely substitutable. Prefer composition for reusable behavior that does not need a type hierarchy. final methods cannot be overridden; static methods are hidden rather than dynamically dispatched.
EXAMPLE · JAVA
Main.java
public class Main {
interface Formatter { String format(String title); }
static class Upper implements Formatter {
@Override public String format(String title) {
return title.toUpperCase(java.util.Locale.ROOT);
}
}
public static void main(String[] args) {
Formatter formatter = new Upper();
System.out.println(formatter.format("task"));
}
}What to expect
Prints TASK although the variable is declared as Formatter.
YOUR TURN
Put it into practice
- 01Add a second Formatter that trims whitespace.
- 02Pass both implementations to the same rendering method without instanceof branches.
- 03Verify: Both implementations satisfy the same null and whitespace policy documented by the interface.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
Polymorphism lets callers use a contract while runtime dispatch selects behavior.
- 01An overriding instance method is selected from the runtime object. An overloaded method is selected at compile time using the declared argument types. Keep overrides compatible with the parent contract; a subtype that throws for valid parent inputs breaks substitutability.
- 02Use inheritance when the relationship is genuinely substitutable. Prefer composition for reusable behavior that does not need a type hierarchy. final methods cannot be overridden; static methods are hidden rather than dynamically dispatched.
- 03Trace the example: Prints TASK although the variable is declared as Formatter.
A mistake worth avoiding
A supposed override is never called.
DEBUGGING ROOM
Something broke. Where do you look?
A supposed override is never called.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
What chooses an overridden instance method at runtime?
BUILD IT YOURSELF
Add a second Formatter that trims whitespace.
- 1.Add a second Formatter that trims whitespace.
- 2.Pass both implementations to the same rendering method without instanceof branches.
- 3.Reproduce this failure, then repair it: A supposed override is never called.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
What chooses an overridden instance method at runtime?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
Both implementations satisfy the same null and whitespace policy documented by the interface.
Reading is separate from passing a knowledge check or completing a lab.