What are the Liskov Substitution and Interface Segregation principles? - Part 4
Liskov Substitution and Interface Segregation are explained with open-ended funds and fixed maturity plans, including the warning signs of a broken hierarchy and how to fix it.
What are the Liskov Substitution and Interface Segregation principles? - Part 4
Inheritance and interfaces are the most used features of an object oriented language, and also the most misused. A subclass which throws an exception for a method it inherited, or an interface with twenty methods where every class implements only five of them, looks harmless at the time of writing. But it makes every caller to be careful about which object it received, and that kills the purpose of polymorphism. In the previous article, we discussed the Single Responsibility Principle and the Open Closed Principle. In this article, we are going to see the next two SOLID principles, the Liskov Substitution Principle and the Interface Segregation Principle, with mutual fund examples.
Maintainable Code series:
- Why does code become hard to maintain? - Part 1
- How to keep the code simple and the objects in charge? - Part 2
- What are the Single Responsibility and Open Closed principles? - Part 3
- What are the Liskov Substitution and Interface Segregation principles? - Part 4 (this article)
- How to use Dependency Inversion to loosen the coupling? - Part 5
TL;DR Summary
- Liskov Substitution Principle (LSP) - Child classes should never break the behavior of the parent class. A child must be substitutable for its parent.
- “Is a” is not enough, the child must also behave as the parent promises. Think “is substitutable by”.
- Warning signs of LSP violation - empty methods, hardened conditions, partial implementation and changing the state of the parent.
- Interface Segregation Principle (ISP) - No client should be forced to depend on methods it does not use.
- Prefer small and cohesive interfaces. A fat interface forces the implementers to write methods which they can’t support.
- ISP enforces SRP and LSP, and both give less coupling and easy testing.
Liskov Substitution Principle
What is it?
The Liskov Substitution Principle was introduced by Barbara Liskov. It says, the objects of a parent class should be replaceable by the objects of its child classes without breaking the program. In simple terms, if a method works with a Fund, it should work with every kind of Fund, without checking which kind it received.
It is the subclass polymorphism we discussed in Part 1, but with a condition. In the real world a closed-ended fund “is a” fund, but in the code the child must also keep the promise of the parent. So the question is not only “is a”, it is “is substitutable by”.
Example
Let us start with an innocent looking hierarchy,
public abstract class Fund
{
public abstract void Purchase(decimal amount);
public abstract void Redeem(decimal units);
}
public class OpenEndedFund : Fund
{
public override void Purchase(decimal amount) { /* ... */ }
public override void Redeem(decimal units) { /* ... */ }
}
public class FixedMaturityPlan : Fund
{
public override void Purchase(decimal amount) { /* ... */ }
public override void Redeem(decimal units)
{
throw new NotSupportedException("Units can't be redeemed before maturity");
}
}
A Fixed Maturity Plan (FMP) is a closed-ended fund, the investor can’t redeem the units with the fund house before the maturity date. So the class inherits Redeem() but it can’t honour it. Now see what happens in the caller,
public void RedeemAll(IEnumerable<Fund> funds)
{
foreach (var fund in funds)
fund.Redeem(fund.Units); // blows up for the FMP
}
The caller was written against the Fund, and it expects Redeem() to work. To protect itself, it must check the type, if (fund is FixedMaturityPlan), and now the caller knows about every child. It breaks the Open Closed Principle also, because every new fund type means a new check.
The following diagram shows the problem and the fix.

Variations of the violation
Find below the common warning signs,
- Empty methods - The child overrides a method with nothing inside, or just throws NotImplementedException or NotSupportedException.
- Hardened conditions - The child adds stricter rules than the parent, for example the parent accepts any positive amount and the child accepts only multiples of 1000 (the precondition is strengthened).
- Partial implementation - The child implements only some of the behavior the parent promised.
- Changing the state of the parent - The child changes the data of the parent in a way the parent doesn’t expect.
How to fix it?
- Fix the relationship - If the child can’t do what the parent does, it is not a real subtype. Move the behavior out of the parent, as we did in the example.
- Use Tell, Don’t Ask - If the caller has to ask “what are you?” before calling, the behavior should be moved inside the object.
- Use pure abstraction - Keep the base type with only the behavior which every child can honour, and put the optional behavior in a separate interface.
The fixed version,
public abstract class Fund
{
public abstract void Purchase(decimal amount);
}
public interface IRedeemable
{
void Redeem(decimal units);
}
public class OpenEndedFund : Fund, IRedeemable
{
public override void Purchase(decimal amount) { /* ... */ }
public void Redeem(decimal units) { /* ... */ }
}
public class FixedMaturityPlan : Fund
{
public override void Purchase(decimal amount) { /* ... */ }
}
public void RedeemAll(IEnumerable<IRedeemable> funds)
{
foreach (var fund in funds)
fund.Redeem(/* units */);
}
The code which needs Redeem() asks for IRedeemable, so it can never receive a fund which can’t redeem. The compiler now helps us, instead of the exception at runtime.
Benefits
- Less coupling and high cohesion.
- Reusability, the callers work with any new child without changes.
- Easy testing, you can test the callers with any child (or a fake).
Interface Segregation Principle
What is it?
The Interface Segregation Principle says, no client should be forced to depend on methods it does not use. In simple terms, prefer many small and cohesive interfaces to one big interface. When an interface is fat, the implementers must write the methods which they can’t support (the unimplemented interface methods), and the clients get a dependency on the methods they never call.
Example
Let us extend our fund example with one interface for everything,
public interface IFundOperations
{
void Purchase(decimal amount);
void Redeem(decimal units);
void Switch(Fund target, decimal units);
void StartSip(decimal monthlyAmount);
void PayDividend();
}
The FixedMaturityPlan can accept the money in the new fund offer period, and it can pay the dividend, but it can’t redeem, switch or run a Systematic Investment Plan (SIP). It must still implement all five methods, so three of them will be empty or will throw an exception. We have come back to the LSP problem again, this time through the interface.
The same interface hurts the clients also. A web page which only shows the “Invest now” button depends on the Redeem and Switch methods, so a change to the signature of Switch forces this page to recompile and to be retested.

Let us segregate it by the role,
public interface IPurchasable { void Purchase(decimal amount); }
public interface IRedeemable { void Redeem(decimal units); }
public interface ISwitchable { void Switch(Fund target, decimal units); }
public interface ISipEnabled { void StartSip(decimal monthlyAmount); }
public class OpenEndedFund : Fund, IPurchasable, IRedeemable, ISwitchable, ISipEnabled { /* ... */ }
public class FixedMaturityPlan : Fund, IPurchasable { /* ... */ }
Each class implements only what it can honour, and each client depends only on the interface it uses. The “Invest now” page takes an IPurchasable, and it doesn’t care about the others.
Benefits
- Less coupling and high cohesion.
- Enforces the use of SRP and LSP, because small interfaces have one reason to change and fewer chances to be broken.
- Reusability and easy testing, a fake for one small interface has only one method.
Must Consider: Don’t go to the other extreme and create one interface for each method. Group the methods which are always used together by the same client. A small interface has a clear role, not a single method.
Wrap up
In this article, we have looked at the Liskov Substitution Principle, how to find its violations and fix them, and the Interface Segregation Principle with a mutual fund example. I appreciate your time and efforts to read this article. In the next article, I will walk you through the last SOLID principle, the Dependency Inversion Principle, and how it loosens the coupling between the layers.
Related Posts
How to keep the code simple and the objects in charge? - Part 2
KISS, DRY and YAGNI keep the code simple, while Tell Don't Ask and the Law of Demeter keep the logic near the data, explained with mutual fund redemption and NAV examples.
How to use Dependency Inversion to loosen the coupling? - Part 5
Dependency Inversion is explained with layered and multi-implementation examples, dependency injection, IoC containers, testing benefits and a summary of all the principles.
What are the Single Responsibility and Open Closed principles? - Part 3
The Single Responsibility and Open Closed principles are explained with a mutual fund service and exit load rules, with the benefits, techniques and trade-offs of each.