inheritance - Liskov substitution principle -
inheritance - Liskov substitution principle -
i able general thought of lsp , violations. say, square extending rectangle having setwidth() , setheight() functions subtle violation of lsp because there requirement. greenduck extending duck class function something(grass g) must not have clause requiring grass green.
for square , rectangle scenario, think right implement polygon interface , rectangle , square class implementing polygon in own ways. greenduck scenario, greenduck using composition have duck 1 of fields might idea.
q1) understanding right of above 2 scenarios?
q2) also, unable model scenarios lsp allow 'extends' brings me question. can delve lsp , provide specific examples extends appropriate , bonus if composition wouldn't good.
edit: clarity
in context of liskov, it's of import distinguish between inspection interfaces , modification interfaces. square "is-a" rectangle in sense that, rectangle, can height, , width. different rectangle in can't set height , width separately. if "it right implement polygon interface" mean rectangle
should have getpolygonvertices
function, sure, that's fine. if mean should implement interface has setpolygonvertices
that's wrong, same reason square
can't have setheightandwidth
rectangle
.
as greenduck
having duck
member.... eeeh... kind of weird. ducks have other ducks within them?
inheritance design-patterns lsp
Comments
Post a Comment