{"id":1219,"date":"2014-04-04T13:41:08","date_gmt":"2014-04-04T13:41:08","guid":{"rendered":"https:\/\/www.whizlabs.com\/blog\/?p=1219"},"modified":"2020-09-01T07:27:16","modified_gmt":"2020-09-01T07:27:16","slug":"what-is-inheritance","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/what-is-inheritance\/","title":{"rendered":"What is Inheritance"},"content":{"rendered":"<p>In our day to day life, we use a word <i>inherit<\/i> which simply means-\u2018to receive from an ancestor by legal succession or will\u2019. We use this term generally in case where a child inherits his\/her parents\u2019 properties or where a parent-child relationship exists. This process of inheriting the properties is referred as inheritance. Similarly, in java the term inheritance, is also used where a parent child relationship exists, but instead of human beings here parent as well as child is a class.<\/p>\n<p><b>Inheritance <\/b>is a concept using which a class can inherit the properties of another class. Inheritance allows the creation of hierarchal classifications and therefore is considered as one of the cornerstones of object-oriented programming. Basically, we use inheritance to achieve <i>Dynamic Binding (discussed later) and Code Reusability.<\/i><b> Code Reusability<\/b> means that once we have created a general class which defines traits common to a set of related items, this class can be inherited by other classes, each adding those things that are unique to it. Reusing existing code saves time, money, and efforts and increases a program\u2019s reliability.<\/p>\n<p>In the terminology of java, the class that is inherited is referred as a <b><i>base class <\/i><\/b>or<b><i> a super-class <\/i><\/b>or <b><i>a parent clas s<\/i><\/b>on the other hand the class which is inheriting is known as a <b><i>child class <\/i><\/b>or <b><i>a sub-class.<\/i><\/b> The child class inherits the proper the properties of the parent class by using the <b><i>extends <\/i><\/b>keyword.<\/p>\n<p><b><span style=\"text-decoration: underline\">Different Types of Inheritances in Java:<\/span><br \/>\n<\/b><\/p>\n<p><b>1) Single Inheritance: &#8211; <\/b>When one sub-class inherits only one super-class, it is known as single inheritance. For example;<br \/>\nclass Base<br \/>\n{<br \/>\nvoid show()<br \/>\n{<br \/>\nSystem.out.println(\u201cBase class\u201d);<br \/>\n}<br \/>\n}<br \/>\nclass Child extends Base<br \/>\n{<br \/>\nvoid display()<br \/>\n{<br \/>\nSystem.out.prinln(\u201cChild class\u201d);<br \/>\n}<br \/>\npublic static void main(String a[])<br \/>\n{<br \/>\nChild c=new Child();<br \/>\nc.display();<br \/>\n}<br \/>\n}<\/p>\n<p><b>2) Multilevel Inheritance: &#8211; <\/b>A multilevel inheritance is achieved when a super-class is inherited by one sub-class and again this sub-class is inherited by another sub-class and so on. For example;<br \/>\nclass Base<br \/>\n{<br \/>\nvoid show()<br \/>\n{<br \/>\nSystem.out.println(\u201cBase\u201d);<br \/>\n}<br \/>\n}<br \/>\nclass Child1 extends Base<br \/>\n{<br \/>\nvoid show1()<br \/>\n{<br \/>\nSystem.out.println(\u201cChild1\u201d);<br \/>\n}<br \/>\n}<br \/>\nclass Child2 extends Child1<br \/>\n{<br \/>\nvoid display()<br \/>\n{<br \/>\nSystem.out.println(\u201cChild2\u201d);<br \/>\n}<br \/>\npublic static void main(String a[])<br \/>\n{<br \/>\nChild c2=new Child();<br \/>\nc2.display();<br \/>\nBase b=new Base();<br \/>\nb.show();<br \/>\n}<br \/>\n}<\/p>\n<p><b>3) Hierarchal Inheritance: &#8211; <\/b>In hierarchal inheritance, we have only one super class which is inherited by more than one sub-classes.<br \/>\nclass Base<br \/>\n{<br \/>\nvoid show()<br \/>\n{<br \/>\nSystem.out.println(\u201cBase\u201d);<br \/>\n}<br \/>\n}<br \/>\nclass Child1 extends Base<br \/>\n{<br \/>\nvoid show1()<br \/>\n{<br \/>\nSystem.out.println(\u201cChild1\u201d);<br \/>\n}<br \/>\n}<br \/>\nclass Child2 extends Base<br \/>\n{<br \/>\nvoid show2()<br \/>\n{<br \/>\nSystem.out.println(\u201cChild2\u201d);<br \/>\n}<br \/>\npublic static void main(String a[])<br \/>\n{<br \/>\nChild2 c2=new Child2();<br \/>\nc2.show1();<br \/>\nBase b=new Base();<br \/>\nb.show();<br \/>\n}<br \/>\n}<\/p>\n<p><b>NOTE: &#8211; <\/b>All the data members and member functions of the parent class are available to the child class, unless they are declared private within the parent class.<\/p>\n<p>If a data member is declared in a parent class as well as in the child class, both having the same name and if we simply use it further in our program, then by default the value of child class\u2019 data member is used and we say that child class hides data of the parent class.<\/p>\n<p>For example;<br \/>\nclass Base<br \/>\n{<br \/>\nint x=10;<br \/>\n}<br \/>\nclass Child extends Base<br \/>\n{<br \/>\nint x=20;<br \/>\nvoid show()<br \/>\n{<br \/>\nSystem.out.println(\u201cx\u201d);<br \/>\n}<br \/>\npublic static void main(String a[])<br \/>\n{<br \/>\nChild c=new child();<br \/>\nc1.show();<br \/>\n}<br \/>\n}<br \/>\nOutput- 20<\/p>\n<p>To overcome this problem, we use the keyword \u2018<b>super<\/b>\u2019. Using super, we refer to the parent class\u2019 data member.<br \/>\nWe write (super.x) in line number 10 instead of x to get output as 10, i.e. the value of x in parent class.<\/p>\n<p><span>Understand more about Inheritance\u00a0<\/span><span>in <\/span><a href=\"https:\/\/www.whizlabs.com\/oracle-certified-professional-java-se-6-programmer\/\">Whizlabs OCPJP 6 Training Course.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our day to day life, we use a word inherit which simply means-\u2018to receive from an ancestor by legal succession or will\u2019. We use this term generally in case where a child inherits his\/her parents\u2019 properties or where a parent-child relationship exists. This process of inheriting the properties is referred as inheritance. Similarly, in java the term inheritance, is also used where a parent child relationship exists, but instead of human beings here parent as well as child is a class. Inheritance is a concept using which a class can inherit the properties of another class. Inheritance allows the [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[991],"class_list":["post-1219","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-inheritance"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"In our day to day life, we use a word inherit which simply means-\u2018to receive from an ancestor by legal succession or will\u2019. We use this term generally in case where a child inherits his\/her parents\u2019 properties or where a parent-child relationship exists. This process of inheriting the properties is referred as inheritance. Similarly, in&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/1219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=1219"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/1219\/revisions"}],"predecessor-version":[{"id":71623,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/1219\/revisions\/71623"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}