Part 08 / Topic 9 of 12
Interfaces and default methods
An interface is a pure contract: a set of method signatures any implementing class promises to provide, declared with implements instead of extends. Since Java 8, an interface method can also carry a default body — behaviour every implementer gets for free, which any implementer may still override. A class can implement as many interfaces as it needs (unlike extends, which allows only one superclass), because a contract is just a promise, not a slice of an object's actual shape.
Think of "must be able to produce a taxable amount" as a form every interest-paying product has to fill in, while "how to turn that amount into tax" is a calculation the bank is happy to do the same way for most products, and differently for a few.
Real-world example: a Taxable interface with a default TDS (tax deducted at source) calculation.
interface Taxable {
double taxableAmount(); // every implementer must supply this
default double tdsAmount() { // a ready-made method built on taxableAmount()
return Math.round(taxableAmount() * 0.10 * 100) / 100.0; // 10% TDS, a flat example rate
}
}
static class SavingsAccount implements Taxable {
private final String accountNumber;
private final double interestEarned;
SavingsAccount(String accountNumber, double interestEarned) {
this.accountNumber = accountNumber;
this.interestEarned = interestEarned;
}
@Override public double taxableAmount() { return interestEarned; }
@Override public String toString() { return accountNumber + ": interest ₹" + interestEarned; }
}
static class FixedDeposit implements Taxable {
private final String depositId;
private final double maturityInterest;
FixedDeposit(String depositId, double maturityInterest) {
this.depositId = depositId;
this.maturityInterest = maturityInterest;
}
@Override public double taxableAmount() { return maturityInterest; }
@Override public double tdsAmount() { // FixedDeposit overrides the default: higher rate above a threshold
double rate = taxableAmount() > 40000.0 ? 0.20 : 0.10;
return Math.round(taxableAmount() * rate * 100) / 100.0;
}
@Override public String toString() { return depositId + ": maturity interest ₹" + maturityInterest; }
}
SBIN0001234: interest ₹3200.0 -> TDS ₹320.0
FD-9001: maturity interest ₹52000.0 -> TDS ₹10400.0
SavingsAccount never writes a tdsAmount() method at all — it gets the default one for free, as long as it supplies taxableAmount(). FixedDeposit supplies its own tdsAmount(), which overrides the default; the same override rules as section 7 apply to interface methods too. Picking abstract class versus interface: reach for an abstract class when subclasses share real state and a constructor (section 8's BankAccount holds balance); reach for an interface when unrelated classes — accounts, deposits, maybe one day bonds — just need to promise the same capability, with no shared state at all. A class is free to do both: extends one abstract class and implements several interfaces.
Full program: examples/InterfacesDemo.java.
Done here? The practice set for Part 8 — including the solutions — is on the part page.