{"id":7035,"date":"2014-05-17T00:23:41","date_gmt":"2014-05-17T00:23:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/whats-wrong-with-the-square-and-rectangle-inheritance-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:23:41","modified_gmt":"2014-05-17T00:23:41","slug":"whats-wrong-with-the-square-and-rectangle-inheritance-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/whats-wrong-with-the-square-and-rectangle-inheritance-collection-of-common-programming-errors\/","title":{"rendered":"What&#39;s wrong with the Square and Rectangle inheritance?-Collection of common programming errors"},"content":{"rendered":"<p>Consider the abstract base class or interface (whether something is an interface or abstract class is an implementation detail rather irrelevant to the LSP) <code>ReadableRectangle<\/code>; it has read-only properties <code>Width<\/code> and <code>Height<\/code>. It would be possible to derive from that a type <code>ReadableSquare<\/code>, which has the same properties but contractually guarantees that <code>Width<\/code> and <code>Height<\/code> will always be equal.<\/p>\n<p>From <code>ReadableRectangle<\/code>, one could define concrete type <code>ImmutableRectangle<\/code> (which takes a height and width in its constructor, and guarantees that the <code>Height<\/code> and <code>Width<\/code> properties will always return the same values), and <code>MutableRectangle<\/code>. One could also define concrete type <code>MutableRectangle<\/code>, which allows the height and width to be set at any time.<\/p>\n<p>On the &#8220;square&#8221; side of things, an <code>ImmutableSquare<\/code> should be substitutable for both an <code>ImmutableRectangle<\/code> and a <code>ReadableSquare<\/code>. A <code>MutableSquare<\/code>, however, is only substitutable for a <code>ReadableSquare<\/code> [which is in turn substitutable for a <code>ReadableRectangle<\/code>.] Further, while the behavior of an <code>ImmutableSquare<\/code> is substitutable for an <code>ImmutableRectangle<\/code>, the value gained by inheriting a concrete <code>ImmutableRectangle<\/code> type would be limited. If <code>ImmutableRectangle<\/code> were an abstract type or interface, the <code>ImmutableSquare<\/code> class would only need to use one field rather than two to hold its dimensions (for a class with two fields, saving one is no big deal, but it&#8217;s not hard to imagine classes with a lot more fields, where the savings could be significant). If, however, <code>ImmutableRectangle<\/code> is a concrete type, then any derived type would have to have all the fields of its base.<\/p>\n<p>Some types of square are substitutable for the corresponding types of rectangles, but a mutable square is not substitutable for a mutable rectangle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the abstract base class or interface (whether something is an interface or abstract class is an implementation detail rather irrelevant to the LSP) ReadableRectangle; it has read-only properties Width and Height. It would be possible to derive from that a type ReadableSquare, which has the same properties but contractually guarantees that Width and Height [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7035","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7035"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7035\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}