by BehindJava

What is Interface-Segregation Principle in SOLID Design Principles of Object Oriented Programming

Home » java » What is Interface-Segregation Principle in SOLID Design Principles of Object Oriented Programming

Interface-Segregation Principle

  • Client should not be forced to implement a method they do not need.
  • It is okay to have multiple small interfaces which does specific work.
  • A silent benefit of having small interfaces is, you start developing the habit of favouring the composition over inheritance.

Example: If you a Vehicle interface which has a methods like start(), stop(), checkEngine(), checkRCValidity(), checkInsurance(). These methods are relevant to the vehicle but not to the client who is calling this interface.

Interface Vehicle{
    void start();
    void stop();
    void checkEngine();
    void checkRCValidity();
    void checkInsurance();
}

Lets say we have two classes MechanicInsepction and PolicInspection, Here MechanicInsepction doesn’t bother or need to implement the checkRCValidity() and checkInsurance() where a mechanics job is to check weather the vehicle is starting or stopping or engine is working fine and PolicInspection is concerned about the checkRCValidity() and checkInsurance().

Now the problem here is when MechanicInsepction implements the Vehicle interface. it should provide the default implementations for checkRCValidity() and checkInsurance() methods which is not necessary and vice versa for the PolicInspection.

Interface Vehicle{
    void start();
    void stop();
    void checkEngine();
}

class MechanicInsepction extends Vehicle
{}
Interface Vehicle
{
    void checkRCValidity();
    void checkInsurance();
}

class PolicInspection extends Vehicle
{}

Now we can break the Vehicle interface into two by implementing the interface-segregation principle as shown below.