<br />
<b>Warning</b>:  Declaration of PageLinesBanners::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/banners/section.php</b> on line <b>205</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesFeatures::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/features/section.php</b> on line <b>254</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesFeatures::section_head($clone_id) should be compatible with PageLinesSection::section_head() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/features/section.php</b> on line <b>61</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesHighlight::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/highlight/section.php</b> on line <b>94</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesCarousel::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/carousel/section.php</b> on line <b>168</b><br />
<br />
<b>Warning</b>:  Declaration of PLheroUnit::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/hero/section.php</b> on line <b>140</b><br />
<br />
<b>Warning</b>:  Declaration of PLNavBar::section_template($clone_id, $location = '') should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/navbar/section.php</b> on line <b>199</b><br />
<br />
<b>Warning</b>:  Declaration of PLMasthead::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/masthead/section.php</b> on line <b>178</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesQuickSlider::section_template($clone_id) should be compatible with PageLinesSection::section_template() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/quickslider/section.php</b> on line <b>59</b><br />
<br />
<b>Warning</b>:  Declaration of PageLinesQuickSlider::section_head($clone_id) should be compatible with PageLinesSection::section_head() in <b>/home/c0nannet/public_html/blog/wp-content/themes/pagelines/sections/quickslider/section.php</b> on line <b>29</b><br />
{"id":268,"date":"2018-01-11T00:06:48","date_gmt":"2018-01-11T00:06:48","guid":{"rendered":"http:\/\/c0nan.net\/blog\/?p=268"},"modified":"2018-01-11T00:06:48","modified_gmt":"2018-01-11T00:06:48","slug":"inheritance-vs-composition-wars","status":"publish","type":"post","link":"https:\/\/c0nan.net\/blog\/inheritance-vs-composition-wars\/","title":{"rendered":"Inheritance vs Composition Wars"},"content":{"rendered":"<p>One of the fundamental activities of an object-oriented design is establishing relationships between classes. Two fundamental ways to relate classes are\u00a0<em>inheritance<\/em>\u00a0and\u00a0<em>composition<\/em>. Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition. This article will compare these two approaches to relating classes and will provide guidelines on their use. <a href=\"https:\/\/www.javaworld.com\/article\/2076814\/core-java\/inheritance-versus-composition--which-one-should-you-choose-.html\">Source<\/a><\/p>\n<p>First, some background on the meaning of inheritance and composition.<\/p>\n<p><strong>About inheritance<\/strong>\u00a0In this article, I&#8217;ll be talking about single inheritance through class extension, as in:<\/p>\n<pre><code>class Fruit {\r\n \/\/...}\r\n\r\nclass Apple extends Fruit {\r\n \/\/... }<\/code><\/pre>\n<p>In this simple example, class\u00a0<code>Apple<\/code>\u00a0is related to class\u00a0<code>Fruit<\/code>\u00a0by inheritance, because\u00a0<code>Apple<\/code>\u00a0extends\u00a0<code>Fruit<\/code>. In this example,\u00a0<code>Fruit<\/code>\u00a0is the\u00a0<em>superclass<\/em>\u00a0and\u00a0<code>Apple<\/code>is the\u00a0<em>subclass<\/em>.<\/p>\n<aside id=\"\" class=\"nativo-promo nativo-promo-1 smartphone\"><\/aside>\n<p>I won&#8217;t be talking about multiple inheritance of interfaces through interface extension. That topic I&#8217;ll save for next month&#8217;s\u00a0<strong>Design Techniques<\/strong>\u00a0article, which will be focused on designing with interfaces.<\/p>\n<p>Here&#8217;s a UML diagram showing the inheritance relationship between\u00a0<code>Apple<\/code>\u00a0and\u00a0<code>Fruit<\/code>:<\/p>\n<figure class=\"center\"><img loading=\"lazy\" src=\"https:\/\/images.techhive.com\/images\/idge\/imported\/article\/jvw\/1998\/11\/techniquesfig1-100158308-orig.gif\" width=\"113\" height=\"130\" \/><figcaption>Figure 1. The inheritance relationship<\/figcaption><\/figure>\n<p><strong>About composition<\/strong>\u00a0By composition, I simply mean using instance variables that are references to other objects. For example:<\/p>\n<pre><code>class Fruit {\r\n\/\/... }\r\nclass Apple {\r\nprivate Fruit fruit = new Fruit(); \/\/... }<\/code><\/pre>\n<p>In the example above, class\u00a0<code>Apple<\/code>\u00a0is related to class\u00a0<code>Fruit<\/code>\u00a0by composition, because\u00a0<code>Apple<\/code>\u00a0has an instance variable that holds a reference to a\u00a0<code>Fruit<\/code>\u00a0object. In this example,\u00a0<code>Apple<\/code>\u00a0is what I will call the\u00a0<em>front-end class<\/em>\u00a0and\u00a0<code>Fruit<\/code>\u00a0is what I will call the\u00a0<em>back-end class<\/em>. In a composition relationship, the front-end class holds a reference in one of its instance variables to a back-end class.<\/p>\n<p>The UML diagram showing the composition relationship has a darkened diamond, as in:<\/p>\n<figure class=\"center\"><img loading=\"lazy\" src=\"https:\/\/images.techhive.com\/images\/idge\/imported\/article\/jvw\/1998\/11\/techniquesfig2-100158309-orig.gif\" width=\"254\" height=\"104\" \/><figcaption>Figure 2. The composition relationship<\/figcaption><\/figure>\n<h2>Dynamic binding, polymorphism, and change<\/h2>\n<p>When you establish an inheritance relationship between two classes, you get to take advantage of\u00a0<em>dynamic binding<\/em>\u00a0and\u00a0<em>polymorphism.<\/em>\u00a0Dynamic binding means the JVM will decide at runtime which method implementation to invoke based on the class of the object. Polymorphism means you can use a variable of a superclass type to hold a reference to an object whose class is the superclass or any of its subclasses.<\/p>\n<p>One of the prime benefits of dynamic binding and polymorphism is that they can help make code easier to change. If you have a fragment of code that uses a variable of a superclass type, such as\u00a0<code>Fruit<\/code>, you could later create a brand new subclass, such as\u00a0<code>Banana<\/code>, and the old code fragment will work without change with instances of the new subclass. If\u00a0<code>Banana<\/code>\u00a0overrides any of\u00a0<code>Fruit<\/code>&#8216;s methods that are invoked by the code fragment, dynamic binding will ensure that\u00a0<code>Banana<\/code>&#8216;s implementation of those methods gets executed. This will be true even though class\u00a0<code>Banana<\/code>\u00a0didn&#8217;t exist when the code fragment was written and compiled.<\/p>\n<p>Thus, inheritance helps make code easier to change if the needed change involves adding a new subclass. This, however, is not the only kind of change you may need to make.<\/p>\n<h2>Changing the superclass interface<\/h2>\n<p>In an inheritance relationship, superclasses are often said to be &#8220;fragile,&#8221; because one little change to a superclass can ripple out and require changes in many other places in the application&#8217;s code. To be more specific, what is actually fragile about a superclass is its interface. If the superclass is well-designed, with a clean separation of interface and implementation in the object-oriented style, any changes to the superclass&#8217;s implementation shouldn&#8217;t ripple at all. Changes to the superclass&#8217;s interface, however, can ripple out and break any code that uses the superclass or any of its subclasses. What&#8217;s more, a change in the superclass interface can break the code that defines any of its subclasses.<\/p>\n<p>For example, if you change the return type of a public method in class\u00a0<code>Fruit<\/code>\u00a0(a part of\u00a0<code>Fruit<\/code>&#8216;s interface), you can break the code that invokes that method on any reference of type\u00a0<code>Fruit<\/code>\u00a0or any subclass of\u00a0<code>Fruit<\/code>. In addition, you break the code that defines any subclass of\u00a0<code>Fruit<\/code>\u00a0that overrides the method. Such subclasses won&#8217;t compile until you go and change the return value of the overridden method to match the changed method in superclass\u00a0<code>Fruit<\/code>.<\/p>\n<p>Inheritance is also sometimes said to provide &#8220;weak encapsulation,&#8221; because if you have code that directly uses a subclass, such as\u00a0<code>Apple<\/code>, that code can be broken by changes to a superclass, such as\u00a0<code>Fruit<\/code>. One of the ways to look at inheritance is that it allows subclass code to\u00a0<em>reuse<\/em>\u00a0superclass code. For example, if\u00a0<code>Apple<\/code>\u00a0doesn&#8217;t override a method defined in its superclass\u00a0<code>Fruit<\/code>,\u00a0<code>Apple<\/code>\u00a0is in a sense reusing\u00a0<code>Fruit<\/code>&#8216;s implementation of the method. But\u00a0<code>Apple<\/code>only &#8220;weakly encapsulates&#8221; the\u00a0<code>Fruit<\/code>\u00a0code it is reusing, because changes to\u00a0<code>Fruit<\/code>&#8216;s interface can break code that directly uses\u00a0<code>Apple<\/code>.<\/p>\n<h2>The composition alternative<\/h2>\n<p>Given that the inheritance relationship makes it hard to change the interface of a superclass, it is worth looking at an alternative approach provided by composition. It turns out that when your goal is code reuse, composition provides an approach that yields easier-to-change code.<\/p>\n<aside id=\"\" class=\"nativo-promo nativo-promo-2 tablet desktop smartphone\"><\/aside>\n<p><strong>Code reuse via inheritance<\/strong>\u00a0For an illustration of how inheritance compares to composition in the code reuse department, consider this very simple example:<\/p>\n<pre><span class=\"kwd\">class<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"com\">\/\/ Return int number of pieces of peel that<\/span> <span class=\"com\">\/\/ resulted from the peeling activity.<\/span> <span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">System<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">out<\/span><span class=\"pun\">.<\/span><span class=\"pln\">println<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Peeling is appealing.\"<\/span><span class=\"pun\">);<\/span> <span class=\"kwd\">return<\/span> <span class=\"lit\">1<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Apple<\/span> <span class=\"kwd\">extends<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Example1<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">static<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> main<\/span><span class=\"pun\">(<\/span><span class=\"typ\">String<\/span><span class=\"pun\">[]<\/span><span class=\"pln\"> args<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">Apple<\/span><span class=\"pln\"> apple <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Apple<\/span><span class=\"pun\">();<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> pieces <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> apple<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span><\/pre>\n<p>When you run the\u00a0<code>Example1<\/code>\u00a0application, it will print out\u00a0<code>\"Peeling is appealing.\"<\/code>, because\u00a0<code>Apple<\/code>\u00a0inherits (reuses)\u00a0<code>Fruit<\/code>&#8216;s implementation of\u00a0<code>peel()<\/code>. If at some point in the future, however, you wish to change the return value of\u00a0<code>peel()<\/code>\u00a0to type\u00a0<code>Peel<\/code>, you will break the code for\u00a0<code>Example1<\/code>. Your change to\u00a0<code>Fruit<\/code>breaks\u00a0<code>Example1<\/code>&#8216;s code even though\u00a0<code>Example1<\/code>\u00a0uses\u00a0<code>Apple<\/code>\u00a0directly and never explicitly mentions\u00a0<code>Fruit<\/code>.<\/p>\n<p>Here&#8217;s what that would look like:<\/p>\n<pre><span class=\"kwd\">class<\/span> <span class=\"typ\">Peel<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">private<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"typ\">Peel<\/span><span class=\"pun\">(<\/span><span class=\"kwd\">int<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span> <span class=\"kwd\">this<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peelCount <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> getPeelCount<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">return<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span> <span class=\"com\">\/\/...<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"com\">\/\/ Return a Peel object that<\/span> <span class=\"com\">\/\/ results from the peeling activity.<\/span> <span class=\"kwd\">public<\/span> <span class=\"typ\">Peel<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">System<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">out<\/span><span class=\"pun\">.<\/span><span class=\"pln\">println<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Peeling is appealing.\"<\/span><span class=\"pun\">);<\/span> <span class=\"kwd\">return<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Peel<\/span><span class=\"pun\">(<\/span><span class=\"lit\">1<\/span><span class=\"pun\">);<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"com\">\/\/ Apple still compiles and works fine<\/span> <span class=\"kwd\">class<\/span> <span class=\"typ\">Apple<\/span> <span class=\"kwd\">extends<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"com\">\/\/ This old implementation of Example1<\/span> <span class=\"com\">\/\/ is broken and won't compile.<\/span> <span class=\"kwd\">class<\/span> <span class=\"typ\">Example1<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">static<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> main<\/span><span class=\"pun\">(<\/span><span class=\"typ\">String<\/span><span class=\"pun\">[]<\/span><span class=\"pln\"> args<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">Apple<\/span><span class=\"pln\"> apple <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Apple<\/span><span class=\"pun\">();<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> pieces <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> apple<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span><\/pre>\n<p><strong>Code reuse via composition<\/strong>\u00a0Composition provides an alternative way for\u00a0<code>Apple<\/code>to reuse\u00a0<code>Fruit<\/code>&#8216;s implementation of\u00a0<code>peel()<\/code>. Instead of extending\u00a0<code>Fruit<\/code>,\u00a0<code>Apple<\/code>can hold a reference to a\u00a0<code>Fruit<\/code>\u00a0instance and define its own\u00a0<code>peel()<\/code>\u00a0method that simply invokes\u00a0<code>peel()<\/code>\u00a0on the\u00a0<code>Fruit<\/code>. Here&#8217;s the code:<\/p>\n<pre><span class=\"kwd\">class<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"com\">\/\/ Return int number of pieces of peel that<\/span> <span class=\"com\">\/\/ resulted from the peeling activity.<\/span> <span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">System<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">out<\/span><span class=\"pun\">.<\/span><span class=\"pln\">println<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Peeling is appealing.\"<\/span><span class=\"pun\">);<\/span> <span class=\"kwd\">return<\/span> <span class=\"lit\">1<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Apple<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">private<\/span> <span class=\"typ\">Fruit<\/span><span class=\"pln\"> fruit <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Fruit<\/span><span class=\"pun\">();<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span> <span class=\"kwd\">return<\/span><span class=\"pln\"> fruit<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Example2<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">static<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> main<\/span><span class=\"pun\">(<\/span><span class=\"typ\">String<\/span><span class=\"pun\">[]<\/span><span class=\"pln\"> args<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">Apple<\/span><span class=\"pln\"> apple <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Apple<\/span><span class=\"pun\">();<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> pieces <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> apple<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span><\/pre>\n<p>In the composition approach, the subclass becomes the &#8220;front-end class,&#8221; and the superclass becomes the &#8220;back-end class.&#8221; With inheritance, a subclass automatically inherits an implemenation of any non-private superclass method that it doesn&#8217;t override. With composition, by contrast, the front-end class must explicitly invoke a corresponding method in the back-end class from its own implementation of the method. This explicit call is sometimes called &#8220;forwarding&#8221; or &#8220;delegating&#8221; the method invocation to the back-end object.<\/p>\n<p>The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn&#8217;t break any code that relies only on the front-end class. For example, changing the return type of\u00a0<code>Fruit<\/code>&#8216;s\u00a0<code>peel()<\/code>\u00a0method from the previous example doesn&#8217;t force a change in\u00a0<code>Apple<\/code>&#8216;s interface and therefore needn&#8217;t break\u00a0<code>Example2<\/code>&#8216;s code.<\/p>\n<p>Here&#8217;s how the changed code would look:<\/p>\n<pre><span class=\"kwd\">class<\/span> <span class=\"typ\">Peel<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">private<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"typ\">Peel<\/span><span class=\"pun\">(<\/span><span class=\"kwd\">int<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span> <span class=\"kwd\">this<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peelCount <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> getPeelCount<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">return<\/span><span class=\"pln\"> peelCount<\/span><span class=\"pun\">;<\/span> <span class=\"pun\">}<\/span> <span class=\"com\">\/\/...<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"kwd\">class<\/span> <span class=\"typ\">Fruit<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"com\">\/\/ Return int number of pieces of peel that<\/span> <span class=\"com\">\/\/ resulted from the peeling activity.<\/span> <span class=\"kwd\">public<\/span> <span class=\"typ\">Peel<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">System<\/span><span class=\"pun\">.<\/span><span class=\"kwd\">out<\/span><span class=\"pun\">.<\/span><span class=\"pln\">println<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Peeling is appealing.\"<\/span><span class=\"pun\">);<\/span> <span class=\"kwd\">return<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Peel<\/span><span class=\"pun\">(<\/span><span class=\"lit\">1<\/span><span class=\"pun\">);<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"com\">\/\/ Apple must be changed to accomodate<\/span> <span class=\"com\">\/\/ the change to Fruit<\/span> <span class=\"kwd\">class<\/span> <span class=\"typ\">Apple<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">private<\/span> <span class=\"typ\">Fruit<\/span><span class=\"pln\"> fruit <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Fruit<\/span><span class=\"pun\">();<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">()<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">Peel<\/span><span class=\"pln\"> peel <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> fruit<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"kwd\">return<\/span><span class=\"pln\"> peel<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getPeelCount<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span>\r\n\r\n<span class=\"com\">\/\/ This old implementation of Example2<\/span> <span class=\"com\">\/\/ still works fine.<\/span> <span class=\"kwd\">class<\/span> <span class=\"typ\">Example1<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"kwd\">public<\/span> <span class=\"kwd\">static<\/span> <span class=\"kwd\">void<\/span><span class=\"pln\"> main<\/span><span class=\"pun\">(<\/span><span class=\"typ\">String<\/span><span class=\"pun\">[]<\/span><span class=\"pln\"> args<\/span><span class=\"pun\">)<\/span> <span class=\"pun\">{<\/span>\r\n\r\n<span class=\"typ\">Apple<\/span><span class=\"pln\"> apple <\/span><span class=\"pun\">=<\/span> <span class=\"kwd\">new<\/span> <span class=\"typ\">Apple<\/span><span class=\"pun\">();<\/span> <span class=\"kwd\">int<\/span><span class=\"pln\"> pieces <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> apple<\/span><span class=\"pun\">.<\/span><span class=\"pln\">peel<\/span><span class=\"pun\">();<\/span> <span class=\"pun\">}<\/span> <span class=\"pun\">}<\/span><\/pre>\n<p>This example illustrates that the ripple effect caused by changing a back-end class stops (or at least can stop) at the front-end class. Although\u00a0<code>Apple<\/code>&#8216;s\u00a0<code>peel()<\/code>method had to be updated to accommodate the change to\u00a0<code>Fruit<\/code>,\u00a0<code>Example2<\/code>required no changes.<\/p>\n<h2>Comparing composition and inheritance<\/h2>\n<p>So how exactly do composition and inheritance compare? Here are several points of comparison:<\/p>\n<ul>\n<li>It is easier to change the interface of a back-end class (composition) than a superclass (inheritance). As the previous example illustrated, a change to the interface of a back-end class necessitates a change to the front-end class implementation, but not necessarily the front-end interface. Code that depends only on the front-end interface still works, so long as the front-end interface remains the same. By contrast, a change to a superclass&#8217;s interface can not only ripple down the inheritance hierarchy to subclasses, but can also ripple out to code that uses just the subclass&#8217;s interface.<\/li>\n<li>It is easier to change the interface of a front-end class (composition) than a subclass (inheritance). Just as superclasses can be fragile, subclasses can be rigid. You can&#8217;t just change a subclass&#8217;s interface without making sure the subclass&#8217;s new interface is compatible with that of its supertypes. For example, you can&#8217;t add to a subclass a method with the same signature but a different return type as a method inherited from a superclass. Composition, on the other hand, allows you to change the interface of a front-end class without affecting back-end classes.<\/li>\n<li>Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass.<\/li>\n<li>It is easier to add new subclasses (inheritance) than it is to add new front-end classes (composition), because inheritance comes with polymorphism. If you have a bit of code that relies only on a superclass interface, that code can work with a new subclass without change. This is not true of composition, unless you use composition with interfaces. Used together, composition and interfaces make a very powerful design tool. I&#8217;ll talk about this approach in next month&#8217;s\u00a0<strong>Design Techniques<\/strong>\u00a0article.<\/li>\n<li>The explicit method-invocation forwarding (or delegation) approach of composition will often have a performance cost as compared to inheritance&#8217;s single invocation of an inherited superclass method implementation. I say &#8220;often&#8221; here because the performance really depends on many factors, including how the JVM optimizes the program as it executes it.<\/li>\n<li>With both composition and inheritance, changing the implementation (not the interface) of any class is easy. The ripple effect of implementation changes remain inside the same class.<\/li>\n<\/ul>\n<h2>Choosing between composition and inheritance<\/h2>\n<p>So how do all these comparisons between composition and inheritance help you in your designs? Here are a few guidelines that reflect how I tend to select between composition and inheritance.<\/p>\n<p><strong>Make sure inheritance models the\u00a0<em>is-a<\/em>\u00a0relationship<\/strong>\u00a0My main guiding philosophy is that inheritance should be used only when a subclass\u00a0<em>is-a<\/em>\u00a0superclass. In the example above, an\u00a0<code>Apple<\/code>\u00a0likely is-a\u00a0<code>Fruit<\/code>, so I would be inclined to use inheritance.<\/p>\n<p>An important question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application and, with luck, the lifecycle of the code. For example, you might think that an\u00a0<code>Employee<\/code>\u00a0is-a\u00a0<code>Person<\/code>, when really\u00a0<code>Employee<\/code>\u00a0represents a role that a\u00a0<code>Person<\/code>\u00a0plays part of the time. What if the person becomes unemployed? What if the person is both an\u00a0<code>Employee<\/code>\u00a0and a\u00a0<code>Supervisor<\/code>? Such impermanent is-a relationships should usually be modelled with composition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the fundamental activities of an object-oriented design is establishing relationships between classes. Two fundamental ways to relate classes are\u00a0inheritance\u00a0and\u00a0composition. Although the compiler and Java virtual machine (JVM) will do a lot of work for you when you use inheritance, you can also get at the functionality of inheritance when you use composition. This [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/posts\/268"}],"collection":[{"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":1,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/posts\/268\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c0nan.net\/blog\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}