Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Liskov substitution principle
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Violation== Liskov substitution principle explains a property, ''"If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T,"''.<ref>{{cite book|last=Liskov|first=Barbara|title=Data Abstraction and Hierarchy|date=May 1988|publisher=SIGPLAN Notices|url=https://klevas.mif.vu.lt/~plukas/resources/OODPrinciples/Liskov1987.pdf |archive-url=https://web.archive.org/web/20200621040810/https://klevas.mif.vu.lt/~plukas/resources/OODPrinciples/Liskov1987.pdf |archive-date=Jun 21, 2020 |url-status=unfit }}</ref> Here is perhaps an example of violation of LSP: <syntaxhighlight lang=cpp> class Rectangle { public: void SetWidth(double w) { itsWidth = w; } void SetHeight(double h) { itsHeight = h; } double GetHeight() const { return itsHeight; } double GetWidth() const { return itsWidth; } double GetArea() const { return GetHeight() * GetWidth(); } private: double itsWidth; double itsHeight; }; </syntaxhighlight> From a programming point of view, the Square class may be implemented by inheriting from the Rectangle class. <syntaxhighlight lang=cpp> public class Square : Rectangle { public: virtual void SetWidth(double w); virtual void SetHeight(double h); }; void Square::SetWidth(double w) { Rectangle::SetWidth(w); Rectangle::SetHeight(w); } void Square::SetHeight(double h) { Rectangle::SetHeight(h); Rectangle::SetWidth(h); } </syntaxhighlight> However, this violates LSP even though the [[is-a]] relationship holds between Rectangle and Square Consider the following example, where function g does not work if a Square is passed in, and so the open-closed principle might be considered to have been violated. <syntaxhighlight lang=cpp> void g(Rectangle& r) { r.SetWidth(5); r.SetHeight(4); assert(r.GetArea()) == 20); // assertion will fail } </syntaxhighlight> Conversely, if one considers that the type of a shape should only be a constraint on the relationship of its dimensions, then the assumption in g() that SetHeight will change height, and area, but not width is invalid. This assumption is invalid not only for squares, but even potentially for other rectangles that might be coded to preserve area or aspect ratio when height changes.<ref>{{cite web|title=The Liskov Substitution Principle |url=http://www.objectmentor.com/resources/articles/lsp.pdf |publisher=Robert C. Martin, 1996 |access-date=2 October 2012 |url-status=dead |archive-url=https://web.archive.org/web/20150905081111/http://www.objectmentor.com/resources/articles/lsp.pdf |archive-date= 5 September 2015 }}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)