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 abstraction bugs come from a few patterns: a broken promise (a missing or non-public method), new on something abstract, two default bodies that clash, and a dummy body where an abstract method belongs. Knowing them by sight makes every design easier.

Mistakes and fixes at a glance

javac lists every error with its line number, so read them from the top. This phone bill breaks three rules at once: a missing method, a non-public interface method and new on an abstract class.

Example 1 · Reading several errors at once

ManyErrorsDemo.javaJava
interface Payable {    int amountDue();}
abstract class Bill implements Payable {    abstract String label();}
class PhoneBill extends Bill {    int amountDue() {        return 299;    }}
public class ManyErrorsDemo {    public static void main(String[] args) {        Bill b = new Bill();        System.out.println(b.amountDue());    }}
Sample compiler output
ManyErrorsDemo.java:9: error: PhoneBill is not abstract and does not override abstract method label() in Bill
class PhoneBill extends Bill {
^
ManyErrorsDemo.java:10: error: amountDue() in PhoneBill cannot implement amountDue() in Payable
    int amountDue() {
        ^
  attempting to assign weaker access privileges; was public
ManyErrorsDemo.java:17: error: Bill is abstract; cannot be instantiated
        Bill b = new Bill();
                 ^
3 errors

What this code does

  • javac lists every error with its line number. Fix them from the top: here PhoneBill misses label(), amountDue needs public, and new Bill() is not allowed.
  • After each fix, compile again: sometimes one fix removes or reveals other errors.

Review: Interfaces: a contract (opens in a new tab). The other common errors, each with the fix:

Mistakes that compile

The worst abstraction bug gives no error at all: a dummy body in the parent, like int charge(int kg) { return 0; }. A child that forgets to override it silently ships for free.

Example 2 · A design mistake that compiles: the dummy body

DummyShippingDemo.javaJava
abstract class Shipping {    int charge(int kg) {        return 0;    }}
class Standard extends Shipping {    @Override    int charge(int kg) {        return 40 + kg * 10;    }}
class Express extends Shipping {    // forgot charge()}
public class DummyShippingDemo {    public static void main(String[] args) {        Shipping[] options = { new Standard(), new Express() };        for (Shipping s : options) {            System.out.println("Charge for 3 kg: Rs. " + s.charge(3));        }    }}
Output
Charge for 3 kg: Rs. 70
Charge for 3 kg: Rs. 0

What this code does

  • Shipping is abstract, but charge has a dummy body. Express forgets to override it and ships for free.
  • There is no error. Making charge abstract would turn this silent bug into a compile error.

Example 3 · The fix: an abstract method

FixedShippingDemo.javaJava
abstract class Shipping {    abstract int charge(int kg);}
class Standard extends Shipping {    @Override    int charge(int kg) {        return 40 + kg * 10;    }}
class Express extends Shipping {    @Override    int charge(int kg) {        return 100 + kg * 15;    }}
public class FixedShippingDemo {    public static void main(String[] args) {        Shipping[] options = { new Standard(), new Express() };        for (Shipping s : options) {            System.out.println("Charge for 3 kg: Rs. " + s.charge(3));        }    }}
Output
Charge for 3 kg: Rs. 70
Charge for 3 kg: Rs. 145

What this code does

  • With abstract int charge(int kg);, javac forces every concrete child to write its own charge.
  • The loop does not change at all; only the parent's promise got stronger.

Two more slips compile silently. An abstract class used for an ability (abstract class Chargeable) blocks every class that already has a parent; abilities are interfaces (Abstract class or interface? (opens in a new tab)). A template method that is not final lets a child override it and skip a step, like the payment check.

Spot the bug

Each snippet below looks almost right. Read what it should do, tap the wrong line, then check the fix. Ask three questions: who must write each method, is it public where the contract says so, and is anything abstract being created with new?

Find the bug

Find the abstraction bug

Select the line that is wrong.

QrScanner should follow the CodeScanner contract.

Round 1 of 5

When in doubt, trace the call

A template method that takes a lambda can feel like magic. Trace it: the abstract parent's constructor runs first, then the final method runs its fixed steps, calling the lambda for one step and the child's method for another.

Trace the code

Trace a template method with a lambda

interface Offer {
int apply(int amount);
}
abstract class Order {
int amount;
Order(int amount) {
this.amount = amount;
}
final int pay(Offer o) {
int after = o.apply(amount);
return after + fee(after);
}
abstract int fee(int after);
}
class Food extends Order {
Food(int amount) { super(amount); }
int fee(int after) { return after >= 300 ? 0 : 40; }
}
public class PayTrace {
public static void main(String[] args) {
Order f = new Food(350);
System.out.println(f.pay(a -> a - 100));
}
}

Variables

amount
350

Output

(nothing printed yet)

Line 16new Food(350) runs Food's constructor, which first calls super(amount) in the abstract parent.

Step 1 of 7

Finish up

Mark complete & continue

Next: Must-do problems: Abstraction and Interfaces