DEV Community

Adam Roynon
Adam Roynon

Posted on

Interface Segregation Principle

The Interface Segregation Principle is a component of The SOLID Principles. The SOLID principles are useful tips and rules to follow when writing software and code to create a maintainable and easily extensible codebase. The interface segregation principle states that any object or class should not be forced to override or implement any method that it does not use or any method that doesn't make sense for its context and purpose.

The below code snippet shows a simple interface called Bird. This interface has two abstract methods, 'walk', and 'fly'. When we implement, or inherit, from this interface we will be forced to override and provide a body to both of these methods. According to the interface segregation principle, we shouldn't implement this interface if it doesn't make sense for the object to override both of these methods.

public interface Bird {

  void walk();
  void fly();

}
Enter fullscreen mode Exit fullscreen mode

Now let's create a subclass from our Bird class. The below class called Pigeon inherits from the Bird class. This means we need to override and implement both the 'walk' and 'fly' methods. This is fine and this makes sense, pigeons can both walk and fly. This doesn't break the interface segregation principle as it makes sense for a pigeon class to implement these two methods, we haven't forced any unexpected or unneeded functionality.

public Pigeon implements Bird {

  @Override
  public void walk(){
    //TODO
  }

  @Override
  public void fly(){
    //TODO
  }

}
Enter fullscreen mode Exit fullscreen mode

Let's look at another example. The below class creates a Dodo class that inherits from the Bird class. This is where we are breaking the interface segregation principle. If you didn't know, a Dodo bird cannot fly. This means, by inheriting from the bird class, we are forcing our Dodo class to implement a method that is doesn't need and that it doesn't make sense for it to have. Often times in large codebases inheritance is used to avoid duplicating code and when the interface segregation principle is broken the method is simply given a '//noop' comment. This stands for 'no operation'. Never use a '//noop' comment.

public Dodo implements Bird {

  @Override
  public void walk(){
    //TODO
  }

  @Override
  public void fly(){
    // noop
  }

}
Enter fullscreen mode Exit fullscreen mode

Instead of using a bird class we can create an interface called 'CanFly', as shown below. This will allow us to inherit this interface on only the classes where it makes sense, and we would also be able to use it on classes that aren't types of birds without confusing our inheritance tree. This is a more abstract way of thinking about objects and classes as we are not assigning names to nouns that exist in the real world (e.g. bird, dodo, pigeon). This is often the reason why beginner developers break the interface segregation principle.

public interface CanFly {

  void fly();

}
Enter fullscreen mode Exit fullscreen mode

This post was originally published on https://acroynon.com

Top comments (0)