{"id":665,"date":"2016-03-04T10:58:48","date_gmt":"2016-03-04T10:58:48","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=665"},"modified":"2016-03-04T10:58:48","modified_gmt":"2016-03-04T10:58:48","slug":"composition","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/composition","title":{"rendered":"Composition In Java With Example"},"content":{"rendered":"<p>We have discussed the concept of inheritance which is a powerful mechanism of reusing code, minimizing data redundancy, and improving the organization of object oriented system. Inheritance is suitable only when classes are in a relationship in which child class <strong>is a<\/strong> parent class. For example: A <strong>Car<\/strong> is a <strong>Vehicle,<\/strong> so the class <strong>Car<\/strong> has all the features or properties of class <strong>Vehicle <\/strong>and in addition to its own features. However, we cannot always have <strong>is a <\/strong>relationship between objects of different classes. Let us say with example: A car is not a kind of engine. To represent such a relationship, we have an alternative to inheritance known as composition. It is applied when classes are in a relationship in which child class <strong>has<\/strong> <strong>a <\/strong>parent class.<\/p>\n<p>Unlike Inheritance in which a subclass extends the functionality of a superclass, in composition, a class reuses the functionality by creating a reference to the object of the class it wants to reuse. For example: A car has a engine, a window has a button, a zoo has a tiger.<\/p>\n<p>Composition is a special case of aggregation. In other words, a restricted aggregation is called composition. When an object contains the other object and the contained object cannot exist without the other object, then it is called composition.<\/p>\n<h4><strong>Program Example of Composition:<\/strong><\/h4>\n<p>Let us consider the following program that demonstrates the concept of composition.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1: <\/strong><\/span>First we create a class <strong>Bike<\/strong> in which we declare and define data members and methods:<\/p>\n<pre>class\u00a0Bike\r\n{\r\n\r\n  \/\/\u00a0declaring data members and methods\r\n\r\n  private\u00a0String\u00a0color;\r\n\r\n  private\u00a0int\u00a0wheels;\r\n\r\n  public\u00a0void\u00a0bikeFeatures()\r\n\r\n  {\r\n\r\n    System.out.println(\"Bike\u00a0Color=\u00a0\"+color\u00a0+\u00a0\"\u00a0wheels=\u00a0\"\u00a0+\u00a0wheels);\r\n\r\n  }\r\n\r\n  public\u00a0void\u00a0setColor(String\u00a0color)\r\n\r\n  {\r\n\r\n    this.color\u00a0=\u00a0color;\r\n\r\n  }\r\n\r\n  public\u00a0void\u00a0setwheels(int\u00a0wheels)\r\n\r\n  {\r\n\r\n    this.wheels\u00a0=\u00a0wheels;\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2: <\/strong><\/span>Second we create a class <strong>Honda <\/strong>which extends the above class <strong>Bike<\/strong>. Here <strong>Honda<\/strong> class uses <strong>HondaEngine<\/strong> class object <strong>start()<\/strong> method via composition. Now we can say that <strong>Honda<\/strong> class HAS-A <strong>HondaEngine<\/strong>:<\/p>\n<pre>class\u00a0Honda\u00a0extends\u00a0Bike\r\n\r\n{\r\n\r\n  \/\/inherits all properties of bike class\r\n\r\n  public\u00a0void\u00a0setStart()\r\n\r\n  {\r\n\r\n    HondaEngine\u00a0e\u00a0=\u00a0new\u00a0HondaEngine();\r\n\r\n    e.start();\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3: <\/strong><\/span>Third we create a class <strong>HondaEngine <\/strong>through which we uses this class object in above class <strong>Honda:<\/strong><\/p>\n<pre>class\u00a0HondaEngine\r\n\r\n{\r\n\r\n  public\u00a0void\u00a0start()\r\n\r\n  {\r\n\r\n    System.out.println(\"Engine has been\u00a0started.\");\r\n\r\n  }\r\n\r\n  public\u00a0void\u00a0stop()\r\n\r\n  {\r\n\r\n    System.out.println(\"Engine\u00a0has been stopped.\");\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Fourth we create a class <strong>CompositionDemo<\/strong> in which we make an object of <strong>Honda<\/strong> class and initialized it:<\/p>\n<pre>class\u00a0CompositionDemo\r\n\r\n{\r\n\r\n  public\u00a0static\u00a0void\u00a0main(String[]\u00a0args)\r\n\r\n  {\r\n\r\n    Honda\u00a0h\u00a0=\u00a0new\u00a0Honda();\r\n\r\n    h.setColor(\"Black\");\r\n\r\n    h.setwheels(2);\r\n\r\n    h.bikeFeatures();\r\n\r\n    h.setStart();\r\n\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Bike color= Black \u00a0\u00a0wheels= 2\r\nEngine has been started\r\n<\/pre>\n<hr \/>\n<h4><strong>Importance of Composition:<\/strong><\/h4>\n<ul>\n<li>In composition we can control the visibility of other object to client classes and reuse only what we need.<\/li>\n<li>Composition allows creation of back-end class when it is needed.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Comparing Composition and Inheritance:<\/strong><\/h4>\n<ul>\n<li>It is easier to change the class which is implementing composition than inheritance.<\/li>\n<li>In inheritance, you cannot add method to a child class with the same method name but a different return type as that method inherited from a parent class. On the other hand, composition allows us to change the interface of a front-end class without affecting back-end classes.<\/li>\n<li>Composition is done at run time i.e. dynamic binding while Inheritance is done at compile time i.e. static binding.<\/li>\n<li>If you want to reuse code and there is no <strong>is-a<\/strong> relationship, then use composition. You don\u2019t need to use inheritance for code reuse.<\/li>\n<li>If you want polymorphism, but there is no <strong>is-a<\/strong> relationship, then use composition with interfaces. You don\u2019t need to use inheritance to get polymorphism.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We have discussed the concept of inheritance which is a powerful mechanism of reusing code, minimizing data redundancy, and improving the organization of object oriented system. Inheritance is suitable only when classes are in a relationship in which child class is a parent class. For example: A Car is a Vehicle, so the class Car &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/composition\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Composition In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-665","page","type-page","status-publish","hentry"],"psp_head":"<title>Composition In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn how Composition is applied JAVA when classes are in a relationship in which child class has a parent class. Unlike Inheritance in which a subclass extends the functionality of a superclass, in composition, a class reuses the functionality by creating a reference to the object of the class it wants to reuse.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/composition\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=665"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/665\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}