Skip to lesson

Common mistakes and practice

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most polymorphism bugs are not compile errors: the program runs, but the wrong version runs. Knowing the few patterns behind them makes every override and every trace quicker.

Mistakes and fixes at a glance

The most common slip: a child method that was meant to override, but its parameters differ. It quietly becomes an overload, and the parent version keeps running.

Example 1 · Bug: an overload instead of an override

OverloadBugDemo.javaJava
class Alert {    void send(String msg) {        System.out.println("Notice board: " + msg);    }}
class SmsAlert extends Alert {    void send(String msg, boolean urgent) {        System.out.println("SMS: " + msg);    }}
public class OverloadBugDemo {    public static void main(String[] args) {        Alert a = new SmsAlert();        a.send("Exam postponed");    }}
Output
Notice board: Exam postponed

What this code does

  • send(String, boolean) has an extra parameter, so it overloads instead of replacing send(String).
  • The call matches send(String), which SmsAlert never overrode. Write @Override void send(String msg) in the child and the SMS line prints.

Review: The @Override annotation (opens in a new tab). The other common mistakes, each with what goes wrong and the fix:

Mistakes that compile

Two more bugs give no error at all. A child "overrides" a private parent method, so the parent's code still calls its own copy; and a child repeats a field, which parent code never sees.

Example 2 · Two silent bugs

SilentBugsDemo.javaJava
class Account {    int rate = 4;
    private String type() {        return "Account";    }
    void show() {        System.out.println(type() + " at " + rate + "%");    }}
class FixedDeposit extends Account {    int rate = 7;
    String type() {        return "Fixed deposit";    }}
public class SilentBugsDemo {    public static void main(String[] args) {        Account a = new FixedDeposit();        a.show();    }}
Output
Account at 4%

What this code does

  • type() in Account is private, so the child's type() is a separate method and show() calls the parent's own copy.
  • The child's rate is a second field. show() lives in Account, so it reads Account's rate.

Example 3 · The fixed version

FixedBugsDemo.javaJava
class Account {    String type() {        return "Account";    }
    int rate() {        return 4;    }
    void show() {        System.out.println(type() + " at " + rate() + "%");    }}
class FixedDeposit extends Account {    @Override    String type() {        return "Fixed deposit";    }
    @Override    int rate() {        return 7;    }}
public class FixedBugsDemo {    public static void main(String[] args) {        Account a = new FixedDeposit();        a.show();    }}
Output
Fixed deposit at 7%

What this code does

  • Both values now come from overridable instance methods, so show() gets the object's versions.
  • @Override on both child methods proves to javac that they really override.

Both look like overriding but are not: private methods are not inherited and fields are not dispatched. The fix is the same each time: one overridable instance method in the parent, with @Override on the child's version so javac confirms it.

Spot the bug

Each snippet below looks almost right. Read what it should do, tap the wrong line, then check the fix. Ask: does the signature match exactly, is it an instance method, and which type does the compiler see?

Find the bug

Find the polymorphism bug

Select the line that is wrong.

SmsAlert should replace Alert's void send(String msg).

Round 1 of 5

Trace before you guess

When the output surprises you, trace it. Check the reference type first, then start at the object's class and walk up. A super. call always starts one level above the current class.

Trace the code

Trace dispatch and super

class Ticket {
int fare(int km) {
return km * 2;
}
void print(int km) {
System.out.println("Fare: " + fare(km));
}
}
class AcTicket extends Ticket {
@Override
int fare(int km) {
return super.fare(km) + 50;
}
}
public class FareTrace {
public static void main(String[] args) {
Ticket t = new Ticket();
t.print(100);
t = new AcTicket();
t.print(100);
}
}

Variables

t
Ticket object

Output

(nothing printed yet)

Line 17t points to a plain Ticket.

Step 1 of 10

Finish up

Mark complete & continue

Next: Must-do problems: Polymorphism