According to Liskov Substitution Principle, Subtypes must be substitutable for super type i.e. methods or functions which uses super class type must be able to work with object of sub class without any issue”. LSP is closely related to Single responsibility principle and Interface Segregation Principle. If a class has more functionality than subclass might not support some of the functionality and does violated LSP. In order to follow LSP design principle, derived class or sub class must enhance functionality not reducing it. (See Reference)
How can we identify LSP violation?
Derived class may require less functionalities than the Base class, so some methods would be redundant. We might be using IS-A to check for Super-Sub relationships, but LSP doesn’t use only IS-A, but it also requires that the Sub types must be substitutable for the Super class. And one cannot decide the substitutability of sub class in isolation. One has to consider how the clients of the class hierarchy are going to use it. (See Reference)
Lets take look at the code below which violates LSP by binding swim and fly functionality to Base class Bird and use this functionalities by sub classes.
When we run above code, boom!
penguin sounds penguin swims Exception in thread "main" java.lang.UnsupportedOperationException: penguin can not fly at LSPViolation$Penguin.fly(LSPViolation.java:40) at LSPViolation.main(LSPViolation.java:54)
So lets fix it by seperating responsibilities as SwimmableBird and FlyableBird: