What does the Liskov substitution principle require of a subclass, and how do you recognise a violation?
It says that a child class must be usable everywhere the parent class is used, and that it must keep every promise that the parent made.
public class Main {
public static void main(String[] args) {
java.util.List<Account> accounts = new java.util.ArrayList<>();
accounts.add(new SavingsAccount(100.0));
accounts.add(new FixedAccount(100.0));
double total = 0;
for (Account account : accounts) {
total = total + account.withdraw(10.0);
}
System.out.println(total);
}
}
And here is the parent and the two children.
public abstract class Account {
protected double balance;
public Account(double openingBalance) {
this.balance = openingBalance;
}
public abstract double withdraw(double amount);
public double balance() {
return balance;
}
}
public class SavingsAccount extends Account {
public SavingsAccount(double openingBalance) {
super(openingBalance);
}
@Override
public double withdraw(double amount) {
if (amount > balance) {
throw new IllegalStateException("not enough money");
}
balance = balance - amount;
return balance;
}
}
public class FixedAccount extends Account {
public FixedAccount(double openingBalance) {
super(openingBalance);
}
@Override
public double withdraw(double amount) {
// It refuses half of what the parent allows.
if (amount > 0) {
throw new UnsupportedOperationException("a fixed account cannot be withdrawn from");
}
return balance;
}
}
And how do you recognise a violation? There are four signs, and they all come from the child doing something the parent did not allow.
the sign what it means
--------------------------------------- --------------------------------------------
it throws where the parent worked the child refuses part of the
contract
it accepts less than the parent the parameter type is narrower
it gives back less than the parent the return type is narrower
it changes a rule the parent depended on the state behaves differently
And the first one is the commonest. The parent says the method works for any positive amount, and the child throws for the positive ones. So the code that holds the child as the parent tries it and it fails at run time.
And the second one is worth spelling out, because it is a compile error in Java and a silent bug in other languages. So the parent takes an Object and the child takes a String. Then a caller that has a parent reference and passes a string works, but a caller that passes something else gets an error that was not in the contract.
And the third one is the same idea on the way back. So the parent gives back an Object and the child gives back a String. That is allowed, because a String is an Object. But the reverse is not, so a child cannot narrow what the parent promised to give back.
And the fourth one is the one that has no compiler check at all. The child inherits the state and the rules that go with it, and then it changes the rules, so the code that relied on them breaks.
And there is one more thing that is worth knowing about, and it is the practical test. You write the code that uses the parent and you make sure it never asks what the child really is. If you need to, then the child is not substitutable and the hierarchy is wrong.
// This code is only safe if the substitutability holds.
for (Account account : accounts) {
total = total + account.withdraw(10.0);
}
// This code admits that the promise is broken.
for (Account account : accounts) {
if (account instanceof FixedAccount) {
continue;
}
total = total + account.withdraw(10.0);
}
So the rule to remember is this. The child must accept everything the parent accepts and must not refuse anything the parent allows.
The last question in this topic is about checking the contract before it ships.