java - What can I do if Liskov Substitution Principle is violated? -
liskov substitution principle (lsp) states if object o1 type of s , can substituted object o2 type of t without violating original behavior(s) of users, s subtype of t.
the common example used show lsp violation rectangle , derivative type, square. argument although intuitively square seems subtype of rectangle, there behavior of square different rectangle. conclusion square cannot subtype of rectangle lsp.
all explanations discovered end there , find not helpful. want know should if have problem? create s not subtype of t, , what? solutions have solve it?
can please enlighten me answer overhanging question?
edit: rather elaborating example here, refer article.
you can use 'has a' or 'uses' relationship if can not establish 'is a' relationship.
this means instead of having class b inherit class a, can have class b contain instance of class a. coding practice avoid tight coupling between class , class b.