

This sort of LSP interchangeability explained above is fulfilled by the type-system already, because every subtype is also all its supertypes. Just because Rectangle and Square have one common supertype Shape, which according to LSP they are each individually interchangeable with, does not (necessarily) mean they are interchangeable with each other. LSP still applies to each supertype- subtype relationship individually: IDraw draw = new Square() // ok Expecting interchangeability here is "begging the question".


Square respectively, which means LSP isn't nullified here, it simply doesn't apply. What you are likely referring to as a sign of non-interchangeability of Rectangle and Square is that you cannot do this: IDraw draw = new Rectangle() // nopeīut there's no supertype- subtype relationship between IDraw and Rectangle / IMove and Shape shape = new Square() // still OK to treat a square like a shape So given Square : Shape, IDraw and Rectangle : Shape, IMove the above is still valid: Shape shape = new Rectangle() // still OK to treat a rectangle like a shape In a sense, you could say that Rectangle and Square are interchangeable here, both being possible substitutions for Shape, but this is merely a result of LSP relationships of Rectangle and Square to their superclass Shape respectively.Įvery type has an individual LSP relationship to each of its supertypes. Shape shape = new Square() // should be OK to treat a square like a shape
#Interface segregation code
LSP in code is basically this: Shape shape = new Rectangle() // should be OK to treat a rectangle like a shape It's concerned about interchangeability of a supertype and one of its subtype. LSP is not concerned about two sibling types Rectangle and Square being interchangeable with each other. If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program Public abstract class Shape : IDraw, IMoveĪlternatively, if I put interfaces "halfway" in the class heirachy, LSP is nullified but now ISP is preserved, for example: // In this example the classes Rectangle and Square are no longer interchangeable, so LSP is broken.Īlternatively, if I put interfaces "halfway" in the class hierarchy, LSP is nullified classes Rectangle and Square are no longer interchangeable "A client should never be forced to implement an interface that it doesn't use or clients shouldn't be forced to depend on methods they do not use." // In this example all clients **must** implement IDraw() and IMove() Therefore, doesn't that make the concept of ISP nullified since it states that: How can a class that implements interfaces also guarenttee that it also fits the liksov subsitution?įor example, in this code, if a client to make a new shape class they must still implement IDraw and IMove. It seem as though they conflict each other's definitions. I am confused by the two principles of SOLID, liskovs substitution principle and interface segregation principle.
