{"id":246,"date":"2014-03-30T19:30:22","date_gmt":"2014-03-30T14:00:22","guid":{"rendered":"http:\/\/saurabhjain1537.wordpress.com\/?p=246"},"modified":"2025-02-27T07:46:43","modified_gmt":"2025-02-27T07:46:43","slug":"oop-fundamentals-2-of-3","status":"publish","type":"post","link":"https:\/\/saurabhjain.dev\/?p=246","title":{"rendered":"OOP Fundamentals (2 of 3)"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>Object Oriented Programming Fundamentals<\/strong><\/p>\n\n\n\n<p>An important aspect of OOP is reuse of classes using composition and inheritance.<\/p>\n\n\n\n<p><strong>Composition <\/strong>is using objects of existing classes into a new class. It represents &#8216;<em>has-a<\/em>&#8216; relationship. e.g. car <em>has-a<\/em> engine.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/03\/composition.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"440\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/03\/composition.jpg?w=300\" alt=\"composition\" class=\"wp-image-248\" \/><\/a><figcaption>Composition (has-a)<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Inheritance <\/strong>is creating new classes as type of existing classes. It represents &#8216;<em>is-a<\/em>&#8216; relationship. In Java code, after specifying the name of\u00a0derived (new) class use the keyword <strong>extends<\/strong> followed by name of base (existing) class .<\/p>\n\n\n\n<p>With inheritance derived class gets all the public &amp; protected attributes and methods of base class. There are two more access specifier (other than private and public) &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Protected &#8211; Like private, protected&nbsp; entity is accessible to only the Class which defines it. Unlike private, protected entity of base class is available in the derived class.<\/li><li>Package &#8211; This is the default access where entity is accessible within the package but is private out of package.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/03\/inheritance2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"591\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/03\/inheritance2.jpg?w=300\" alt=\"inheritance\" class=\"wp-image-255\" \/><\/a><figcaption>Inheritance (is-a)<\/figcaption><\/figure><\/div>\n\n\n\n<p>The derived class may add more attribute &amp; methods to the base class. Internally derived class has a object of base class but externally it exposes an extended interface of base class. So,&nbsp; derived class is a wrapper over base class with some more specific functionality.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Initialization<\/strong><\/p>\n\n\n\n<p>Since base class is wrapped inside derived class, it becomes responsibility of derived class to do initialization of base class. Java helps here by calling the constructor of base class from the constructor of derived class, but this automatic help is available only in case of default constructors. <\/p>\n\n\n\n<p>When dealing with constructors having arguments, the derived-class constructor needs to explicitly invoke the base-class constructor using <strong>super<\/strong> keyword. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Furniture {\n\n  Furniture(int i) {\n\n    print(\u201cCreating Furniture\u201d);\n\n  }\n\n}\n\nClass Table extends Furniture {\n\n  Table(int i) {\n\n    super (i);\n\n    print(\u201cCreating Table\u201d);\n\n  }\n\n}<\/code><\/pre>\n\n\n\n<p>In the above example, constructor <em>Table(int)<\/em> invokes <em>super(int)<\/em> which is effectively <em>Furniture(int)<\/em>. A derived class constructor must initialize the base class before doing anything else because initialization of derived may depend on the base class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Programming Fundamentals An important aspect of OOP is reuse of classes using composition and inheritance. Composition is using objects of existing classes into a new class. It represents &#8216;has-a&#8216; relationship. e.g. car has-a engine. Inheritance is creating new classes as type of existing classes. It represents &#8216;is-a&#8216; relationship. In Java code, after specifying &hellip; <a href=\"https:\/\/saurabhjain.dev\/?p=246\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">OOP Fundamentals (2 of 3)<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,30,42,43,44],"class_list":["post-246","post","type-post","status-publish","format-standard","hentry","category-java","tag-composition","tag-inheritance","tag-object-oriented-programming","tag-oop","tag-oops"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=246"}],"version-history":[{"count":1,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":788,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions\/788"}],"wp:attachment":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}