7.1 Two Flavours of Polymorphism
| Kind | Mechanism | Resolved | Also called |
|---|---|---|---|
| Compile-time | Method overloading | By the compiler from argument types | Static binding, early binding |
| Runtime | Method overriding | By the JVM from the actual object | Dynamic dispatch, late binding |
7.2 Upcasting & Downcasting
Upcasting (child → parent reference) is implicit and always safe: Animal a = new Dog();. Through a you can call only members declared in Animal — the reference type defines the visible interface, the object type defines the behaviour.
Downcasting (parent reference → child type) needs an explicit cast and is checked at runtime:
Animal a = new Dog();
Dog d = (Dog) a; // OK: the object really is a Dog
Animal x = new Cat();
Dog bad = (Dog) x; // compiles, but ClassCastException at runtime!
if (x instanceof Dog dd) // Java 16 pattern matching: test + cast + bind
dd.bark();
7.3 Dynamic Method Dispatch — How the JVM Chooses
For a.speak(), the compiler only verifies that the reference type declares speak(). At runtime, the JVM follows the object header to its class metadata and looks up speak in that class's virtual method table (vtable) — each subclass's vtable slot holds a pointer to its own override. This is why the same call site prints different things for Dog and Cat objects.
class Animal { String kind = "Animal"; void speak() { System.out.println("..."); } }
class Dog extends Animal { String kind = "Dog"; void speak() { System.out.println("Woof"); } }
Animal a = new Dog();
a.speak(); // Woof -> method: object type (runtime)
System.out.println(a.kind); // Animal -> FIELD: reference type (compile time)
Trap of the decade: methods are polymorphic, fields are not. Also, private, static, and final methods use static binding — they never dispatch dynamically.
7.4 Abstract Classes — Partial Blueprints
An abstract class cannot be instantiated and may contain both abstract methods (signature only) and concrete methods, fields, constructors, and static members.
abstract class Shape {
private final String name;
Shape(String name) { this.name = name; } // ctor runs via super()
abstract double area(); // contract for children
void describe() { // shared concrete logic
System.out.printf("%s area = %.2f%n", name, area()); // template pattern
}
}
class Circle extends Shape {
private final double r;
Circle(double r) { super("Circle"); this.r = r; }
@Override double area() { return Math.PI * r * r; }
}
Rules: a class with even one abstract method must be declared abstract; a subclass must implement all inherited abstract methods or itself be abstract; abstract classes can have constructors (invoked through super() during child construction) but cannot be final (final forbids the extension that abstract requires — contradictory modifiers).
describe() above is the Template Method pattern: the parent fixes the algorithm skeleton, children plug in the varying step — the standard "real use of abstraction" exam answer.
7.5 Abstract Class vs Interface — When to Use Which
| Criterion | Abstract class | Interface |
|---|---|---|
| State (instance fields) | Yes | No (only public static final constants) |
| Constructors | Yes | No |
| Method bodies | Any | default, static, private only |
| Multiple inheritance | No (single extends) | Yes (implement many) |
| Access modifiers on members | All four | Methods implicitly public |
| Relationship modelled | IS-A with shared code/state | CAN-DO capability |
Rule of thumb: share code and state among closely related classes → abstract class; define a capability for unrelated classes (Comparable, Runnable, Serializable) → interface.
🎯 Exam Focus
- Define runtime polymorphism. Explain dynamic method dispatch with a program and describe how the JVM resolves the call (vtable idea).
- Differentiate upcasting and downcasting. When does ClassCastException occur, and how does
instanceof(including pattern matching) prevent it? - Why are instance fields not polymorphic? Predict the output of a program where both a field and a method are redefined in the subclass.
- What is an abstract class? State four rules governing abstract classes and methods. Can an abstract class have a constructor? Justify.
- Compare abstract classes and interfaces (any six points). When would you choose each?
- Write a program with abstract class Shape (abstract
area(), concretedescribe()) and subclasses Circle, Rectangle and Triangle, driven through aShape[]array.