CodeBlanks Logo Icon CodeBlanks

Dynamic binding

Learn which method is called at runtime.

How dynamic dispatch works

Dynamic dispatch means the method is selected at runtime, not at compile time. The compiler only checks that the reference type declares the method; the JVM picks the actual code to run.

When you collect different types in a list under their superclass, each element calls its own area. A loop for (Shape s : shapes) s.area() gives the right result for each; that is the power of polymorphism.

Override rules

For a subclass method to override the parent's, the signatures must match exactly: same name, same parameter list, compatible return type. The @Override annotation is optional but catches errors at compile time.

The access level cannot be narrowed: a protected parent method can be public in the child, but not private. A final method cannot be overridden.

Practice Dynamic dispatch in a Shape list

Add to the Shape and Square definitions a class Triangle extends Shape with int base, int height, a constructor and an area method returning base * height / 2. Then in main create a List<Shape> shapes = new ArrayList<>();, add a new Square(4) and a new Triangle(6, 4), and print each area on its own line with a for-each loop.

Starter code

Main.java
import java.util.List;
import java.util.ArrayList;

// Shape and Square as above

// Add the Triangle class: base, height, area = base * height / 2

// main: fill the shapes list and print area via for-each

Sample solution

Main.java
class Shape {
    int area() { return 0; }
}

class Square extends Shape {
    int side;
    Square(int side) { this.side = side; }
    int area() { return side * side; }
}

class Triangle extends Shape {
    int base;
    int height;
    Triangle(int b, int h) { this.base = b; this.height = h; }
    int area() { return base * height / 2; }
}

public class Main {
    public static void main(String[] args) {
        List<Shape> shapes = new ArrayList<>();
        shapes.add(new Square(4));
        shapes.add(new Triangle(6, 4));
        for (Shape s : shapes) {
            System.out.println(s.area());
        }
    }
}

Expected output

16
12

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the loop header

Question
for (Shape s : ____) {
    System.out.println(s.area());
}
Answer
for (Shape s : shapes) {
    System.out.println(s.area());
}

Explanation: The for-each loop iterates over each Shape in shapes.

2 Which is an override rule?

  • The return type can be anything
  • The signature (name and parameters) must match exactly Correct answer
  • The access level may be narrowed
  • Final methods can be overridden

Explanation: Override requires the signature to match exactly.

3 Match the override rules

Term Meaning
@Override Compile-time check annotation
final Blocks override
protected Can become public in subclass
private Not visible for override

All lessons in Intermediate