Tag: Interface Segregation Principle (ISP)


  • Dependency Inversion Principle (DIP)

    High-level modules should not depend on low-level modules. Both should depend on abstractions. Example: Consider a Light class that directly controls a Switch. // Violation of DIP public class LightSwitch { private Light light; public LightSwitch(Light light) { this.light = light; } public void turnOn() { light.turnOn(); } public void turnOff() { light.turnOff(); } }…

  • Interface Segregation Principle (ISP)

    Clients should not be obligated to rely on interfaces they don’t utilize. Example: Consider an interface that imposes irrelevant methods on implementing classes. // Violation of ISP public interface Worker { void work(); void eat(); } public class HumanWorker implements Worker { public void work() { System.out.println(“Human working”); } public void eat() { System.out.println(“Human eating”);…

  • Liskov Substitution Principle (LSP)

    Subtypes must be substitutable for their base types. Example: Consider a scenario with a base class Bird and a derived class Penguin. // Violation of LSP public class Bird { public void fly() { System.out.println(“Flying”); } } public class Penguin extends Bird { @Override public void fly() { throw new UnsupportedOperationException(“Penguins can’t fly”); } }…