Overriding and dynamic dispatch
What you will learn
Polymorphism means that a common type can refer to objects with different implementations. Java resolves the applicable method signature using compile-time information, then ordinary overridable instance calls select the implementation for the actual object at runtime.
After this lesson you can:
- Predict the result of an overridden method call through a superclass reference.
- Distinguish overriding from overloading and static hiding.
Use a separate Main.java for each example. Compile with javac Main.java and run with java Main. The code is compatible with Java 21; Java 25 execution is not yet verified.
class Notice { public String message() { return "General notice"; } public String label(int n) { return "Number " + n; }}class Delay extends Notice { @Override public String message() { return "Delayed"; } public String label(String s) { return "Text " + s; } public int minutes() { return 15; }}public class Main { public static void main(String[] args) { Notice notice = new Delay(); System.out.println(notice.message()); System.out.println(notice.label(4)); if (notice instanceof Delay delay) { System.out.println(delay.minutes()); System.out.println(delay.label("A")); } }}Expected output:
DelayedNumber 415Text AThe override is selected for message(); the inherited int overload remains available. The pattern permits use of Delay-specific methods.
Separate declared type from runtime class
In Notice notice = new Delay();, the variable is declared as Notice, but its object is a Delay. The compiler checks the call against Notice. For message(), the runtime then uses the Delay override. The extra minutes() method is not accessible through notice without a suitable type check or conversion. The pattern if (notice instanceof Delay delay) safely tests the type and introduces a Delay variable inside the true branch; null does not match. This pattern form has been stable since Java 16.
Overloading uses distinct parameter signatures and is resolved at compile time. Overriding replaces the behavior of an inherited instance method with a compatible signature. Use @Override to catch spelling and parameter errors. In these non-generic examples, a reference return type may narrow to a subtype (covariant return); primitive return types must match. An override cannot reduce accessibility or widen checked exceptions beyond the inherited contract. Checked exceptions are taught next chapter. Static methods are hidden rather than dynamically overridden; qualify them with the class name. Fields also do not participate in method dispatch. Private methods are not overridden, and final methods cannot be overridden.
A superclass constructor that invokes an overridable method can reach subclass code before subclass initialization finishes. The final example deliberately demonstrates that pitfall with a harmless printed null. Avoid such calls in constructors; use private or final helpers for initialization, or move the work after construction.
class Document {}class Receipt extends Document { public String number() { return "R7"; }}class Printer { public Document create() { return new Document(); } public static String kind() { return "Printer"; }}class ReceiptPrinter extends Printer { @Override public Receipt create() { return new Receipt(); } public static String kind() { return "Receipt printer"; }}public class Main { public static void main(String[] args) { Printer printer = new ReceiptPrinter(); Document document = printer.create(); System.out.println(document instanceof Receipt); Receipt receipt = new ReceiptPrinter().create(); System.out.println(receipt.number()); System.out.println(Printer.kind()); System.out.println(ReceiptPrinter.kind()); }}Expected output:
trueR7PrinterReceipt printerThe runtime object is a Receipt, but the Printer declaration promises Document. Static calls explicitly name the class.
Check a boundary or variation
class Parent { Parent() { System.out.println(label()); } String label() { return "Parent"; }}class Child extends Parent { private String name = "Ready"; @Override String label() { return name; }}public class Main { public static void main(String[] args) { new Child(); System.out.println(new Child().label()); }}Expected output:
nullnullReadyEach Parent constructor dispatches to Child.label before Child.name is initialized; after the second construction finishes, the field is Ready. This is intentionally poor construction design, not a recommended pattern.
Common mistakes
Do not cast a superclass reference just to call every method available on a subclass. Prefer a shared operation. If a subtype-specific operation is needed, use a checked pattern rather than assuming the object has that type. Do not rely on overridable methods during construction.
Practice
Create Meter.charge(int units) returning 4 times units and DiscountMeter overriding it to return 3 times units. Inputs are 0 to 100. Write a total for a Meter[] containing either subtype, using only the shared charge operation. Verify a mixed array, zero units, and an empty array.
Mark the subclass method with @Override. Use Meter as the loop variable so runtime dispatch selects charge().
Next: Interface defaults and conflict rules.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Overriding and dynamic dispatch — knowledge check
Apply the stated Java 21-compatible contracts and predict observable behavior.
Notice n = new Delay(); What selects the implementation of n.message()?
What chooses between label(int) and label(String)?
What can a valid covariant reference return do?
What does instanceof Delay return for a null reference?
Why avoid calling overridable methods in a superclass constructor?