Alex Murphy

Jun 20, 2020

2 min read

Interface Segregation Principle

In this article, I am going to discuss the Interface Segregation Principle with a real-time example. The letter I in the SOLID Design Principle stands for Interface Segregation Principle which is also known as ISP.

Here is the list of the blogs in this series:

  1. Understand SOLID Principle
  2. Single Responsibility Principle
  3. Open-Closed Principle
  4. Liskov Substitution Principle
  5. Dependency Inversion Principle

What is the Interface Segregation Principle?

The Interface Segregation Principle states that “Clients should not be forced to implement any methods they don’t use. Rather than one fat interface, numerous little interfaces are preferred based on groups of methods with each interface serving one submodule”.

Let us break down the above definition into two parts.

  1. First, no class should be forced to implement any method(s) of an interface they don’t use.
  2. Secondly, instead of creating a large or you can say fat interfaces, create multiple smaller interfaces with the aim that the clients should only think about the methods that are of interest to them.

As per the Single Responsibility Principle of SOLID, like classes, interfaces also should have a single responsibility. That means we shouldn’t force any class to implement any method(s) which they don’t require.

Let us understand the Interface Segregation Principle with an example.

Imagine that you want to extract Bird behaviours for an interface.

interface Bird {    public void putEggs();
public void peck();
public void fly();
public void land();
}

Now if we try above interface in Falcon Bird it will work, because Falcon bird have all of the four behaviour.

class Falcon implements Bird
{
public void putEggs() { // it can put Eggs }
public void peck() { // it can peck }
public void fly() { // it can fly }
public void land() { // it can land }
}

Now what if we try to implement this Bird behaviour in penguins, chickens or other birds that does not fly?

class Penguins implements Bird {

public void fly() { //throws Exception; }
// Above method will not work because Penguins can not fly, but //still we need to handle this method
}

To solve this we can segregate the Bird interfaces in more specialized ones:

interface Bird
{
public void putEggs();
public void peck();
}

interface Flying
{
public void fly();
public void land();
}

class Penguin implements Bird
{
//...
}

class Chicken implements Bird
{
//...
}

class Falcon implements Bird, Flying
{
//...
}

As you can see in the above code, now we have split that big interface into two small interfaces. Each interface now having some specific purpose. Now if any class wants all the behaviours then that class needs to implement all the two interfaces as shown in above example like Falcon is implementing two interfaces.

In the next article, I am going to discuss the Dependency Inversion principle with a real-time example. I hope you understood the need and use of the Interface Segregation Principle.

Thanks for reading this article ❤

If I got something wrong? Let me in the comments. I would love to improve.

Clap 👏 If this article helps you.

Follow me on Twitter.