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
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()); }}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 errorsWhat this code does
- javac lists every error with its line number. Fix them from the top: here
PhoneBillmisseslabel(),amountDueneedspublic, andnew 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:
abstract methods cannot have a body/missing method body, or declare abstract→ fix the braces.illegal combination of modifiers→ an abstract method is neverprivate,staticorfinal. Review: Abstract classes and abstract methods (opens in a new tab)constructor X in class X cannot be applied to given types→ callsuper(...)with values;overridden method is final→ fill in the abstract step instead. Review: Constructors, fields and normal methods (opens in a new tab)'{' expectedatextends A, Borimplements ... extends→ one class first, then interfaces. Review: Implement several interfaces (opens in a new tab)inherits unrelated defaults→ override the method and pick withX.super.m();illegal static interface method call→ call it asInterface.m(). Review: default, static and private methods (opens in a new tab)is not a functional interface→ a lambda needs exactly one abstract method. Review: Functional interfaces and lambdas (opens in a new tab)
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
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)); } }}Charge for 3 kg: Rs. 70 Charge for 3 kg: Rs. 0
What this code does
Shippingis abstract, butchargehas a dummy body.Expressforgets to override it and ships for free.- There is no error. Making
chargeabstract would turn this silent bug into a compile error.
Example 3 · The fix: an abstract method
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)); } }}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 owncharge. - 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 5When 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
Variables
- amount
- 350
Output
(nothing printed yet)Line 16new Food(350) runs Food's constructor, which first calls super(amount) in the abstract parent.
Finish up
Mark complete & continue
Next: Must-do problems: Abstraction and Interfaces