{"id":479,"date":"2016-01-03T06:22:02","date_gmt":"2016-01-03T06:22:02","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=479"},"modified":"2018-06-05T06:32:50","modified_gmt":"2018-06-05T06:32:50","slug":"inheritance","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/inheritance","title":{"rendered":"Inheritance Tutorial With Example In JAVA"},"content":{"rendered":"<p>Inheritance is a mechanism that allows the class to use the states and behavior of another class. In simple words a class derive field and methods from another class. The derived class in inheritance is called sub class (also called derived class or extended class, or child class) and the class from which it is derived is called super class (also called base class or a parent class).<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> A super class can have any number of sub class in inheritance but sub class can only extend one super class. Multiple inheritance is not supported in JAVA.<\/p>\n<p>Inheritance is one of the important concept of object oriented programming. The simple definition says inheritance provides mechanism that allows a class to inherit properties of another class.<\/p>\n<p><span style=\"color: #008000;\"><strong><u>Inheritance Concept<\/u><\/strong><strong>:<\/strong><\/span> Inheritance means inherit the properties and behavior of existing object in a new object.<\/p>\n<p><span style=\"color: #008000;\"><strong><u>Way To Achieve Inheritance<\/u><\/strong><strong>: <\/strong><\/span>It is achieved by deriving a new class from existing class using extends keyword. Example, <code>Class Child extends Base { }<\/code>.<\/p>\n<hr \/>\n<h4><strong>Explanation With Example:<\/strong><\/h4>\n<ul>\n<li>In Inheritance by default all data members and member functions of a parent class are available to child class if they are not private<\/li>\n<li>Inheritance defines <strong>is-a <\/strong>relationship between a super class(parent) and its sub class(child).<\/li>\n<li><strong>extends <\/strong>keyword is used to show inheritance in java.<\/li>\n<\/ul>\n<p>For example :<\/p>\n<p>Suppose a class name Base.java<\/p>\n<pre>class Base\r\n{\r\n\/\/Code of Base class\r\n}<\/pre>\n<p>Another class Child.java use Inheritance to extends properties from Base class.<\/p>\n<pre>Class Child extends Base\r\n\r\n{\r\n\/\/extends the properties of base class\r\n}<\/pre>\n<p>In the above example, Child class will inherit\u00a0field and methods of Base class.<\/p>\n<hr \/>\n<h4><strong>Types Of Inheritance<\/strong><\/h4>\n<p>There are 3 types of inheritance:<\/p>\n<ol>\n<li>Single Inheritance<\/li>\n<li>Multilevel Inheritance<\/li>\n<li>Hierarchical Inheritance<\/li>\n<\/ol>\n<p><span style=\"color: #008000;\"><strong>1. Single Inheritance:<\/strong><\/span><\/p>\n<p>In Single inheritance one class is derived from one parent class. The below diagram shows single inheritance where Class B inherits from Class A.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-504\" src=\"\/java\/wp-content\/uploads\/2016\/01\/Single-Inheritance-in-JAVA.jpg\" alt=\"Single Inheritance in JAVA\" width=\"219\" height=\"229\" \/><\/center><strong><span style=\"color: #008000;\">2. Multilevel Inheritance:<\/span><\/strong><\/p>\n<p>In multilevel inheritance there is a concept of grand parent class as shown in below diagram class C inherits class B and class B inherits class A.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-505\" src=\"\/java\/wp-content\/uploads\/2016\/01\/Multilevel-Inheritance-in-JAVA.jpg\" alt=\"Multilevel Inheritance in JAVA\" width=\"296\" height=\"293\" \/><\/center><span style=\"color: #008000;\"><strong>3. Hierarchical Inheritance:<\/strong><\/span><\/p>\n<p>In Hierarchical inheritance more than one sub classes is derived from a single parent class as shown in below diagram class B and C both are derived from single parent class A.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-506\" src=\"\/java\/wp-content\/uploads\/2016\/01\/Hierarchical-Inheritance-in-JAVA.jpg\" alt=\"Hierarchical Inheritance in JAVA\" width=\"353\" height=\"205\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/01\/Hierarchical-Inheritance-in-JAVA.jpg 353w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/01\/Hierarchical-Inheritance-in-JAVA-300x174.jpg 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/center><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>Java does not support multiple Inheritance .<\/p>\n<p><span style=\"color: #008000;\"><strong>Why multiple inheritance is not supported in java:<\/strong><\/span><\/p>\n<p><strong>To remove ambiguity<\/strong>:<\/p>\n<p>Multiple inheritance is not supported in java as it causes ambiguity in few scenarios. The most common scenario is <strong>Diamond problem.<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-507\" src=\"\/java\/wp-content\/uploads\/2016\/01\/ambiguity-problem-in-JAVA.jpg\" alt=\"ambiguity problem in JAVA\" width=\"453\" height=\"312\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/01\/ambiguity-problem-in-JAVA.jpg 453w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/01\/ambiguity-problem-in-JAVA-300x207.jpg 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/center>Consider the above diagram which shows multiple inheritance. In this class D extends both class B &amp; C.Here class B &amp; C inherit the same method of class A. Now the problem comes when class D is extending both class B &amp; C and if class D wants to use same method then which method would be called? That&#8217;s why multiple inheritance is not supported in java as to remove ambiguity.<\/p>\n<hr \/>\n<h4><strong>Inheritance Example:<\/strong><\/h4>\n<p>Below is the program to show you the use of inheritance in java. For coding this we have used eclipse IDE.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Let&#8217;s inherit some fields and methods in Child class from Base class.<\/p>\n<p>Base class is having 2 fields and 1 method:<\/p>\n<pre>class Base\r\n{\r\n\tint x=50;\r\n\tint y = 60;\r\n\r\n\/\/Addition method return integer value of x+y\r\n\tpublic int addition(){\r\n\t\treturn x+y; \/\/return 110\r\n\r\n\t}\r\n}<\/pre>\n<p>Child class inherit addition method and x field from Base class:<\/p>\n<pre>public class Child extends Base\r\n{\r\n\r\n\tint z;\r\n\tpublic void subtraction(){\r\n\t\t\r\n\t\t\/\/addition method and x filed is inherited from Base Class\r\n\t\tz = addition() - x;\r\n\t\tSystem.out.println(z);\r\n\t}\r\n\r\n}<\/pre>\n<p>In the Main class we create Child object and calls subtraction method on it.<\/p>\n<pre>public class InheritanceAbhiandroid {\r\n\r\npublic static void main(String[] args) {\r\n\r\nChild child = new Child(); \/\/Child object\r\nchild.subtraction();\/\/Subtraction method called on child object\r\n\r\n}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>60<\/pre>\n<p><span style=\"color: #008000;\"><strong>Example 2:<\/strong><\/span><\/p>\n<p><strong>Base.java:<\/strong><\/p>\n<pre>class Base\r\n{\r\nint x=50;\r\n}<\/pre>\n<p><strong>Child.java:<\/strong><\/p>\n<pre>public class Child extends Base\r\n{\r\n int x=20;\r\n void show()\r\n {\r\n  System.out.println(x);\r\n }\r\n}<\/pre>\n<p><strong>InheritanceAbhiandroid.java<\/strong><\/p>\n<pre>public class InheritanceAbhiandroid {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tChild c = new Child();\r\n\t\tc.show();\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p><strong>OUTPUT:<\/strong><\/p>\n<pre>20<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> The above program prints value of x=20 because priority always goes to local variables. So in show() method of child class,we can access member of parent class i.e base class using <strong>super <\/strong>keyword. It means if we want to print value of x=50 also from the same above program then by using super keyword.<\/p>\n<p><span style=\"color: #008000;\"><strong>Example 3 of Inheritance using super keyword:<\/strong><\/span><\/p>\n<p><strong>Base.java:<\/strong><\/p>\n<pre>class Base\r\n{\r\nint x=50;\r\n}<\/pre>\n<p><strong>Child.java:<\/strong><\/p>\n<pre>class Child extends Base\r\n{\r\nint x=20;\r\nvoid show()\r\n{\r\nSystem.out.println(x);\r\nSystem.out.println(super.x);\r\n}\r\n}<\/pre>\n<pre>public class InheritanceAbhiAndroid{\r\n\r\npublic static void main(String[] args)\r\n{\r\nChild c=new Child();\r\nc.show();\r\n}\r\n}<\/pre>\n<p><strong>OUTPUT:<\/strong><\/p>\n<pre>20\r\n50<\/pre>\n<hr \/>\n<h4><strong>Importance Of Inheritance:<\/strong><\/h4>\n<ul>\n<li><strong>Reusuability of code<\/strong>: It is one of the important feature of inheritance. It is a good way to reuse the already existing code rather than creating the same code again and again. This feature not only saves time and money as we are reusing the properties but it also increase reliability of code.<\/li>\n<li><strong>Method Overriding<\/strong>: With the help of inheritance, it is possible to override the methods of base class so that base class method is easily used in derived class.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Inheritance Important Points To Remember:<\/strong><\/h4>\n<ul>\n<li>Whenever a parent class and a child class both are having same data members then this concept is known as <strong>data hiding<\/strong>.<\/li>\n<li>Whenever a parent class and a child class both are having ditto same functions then this concept is known as <strong><a href=\"\/java\/method-overriding\">method overriding<\/a>.<\/strong><\/li>\n<li>Whenever a parent class and a child class both are having same static functions then this concept is known as <strong>function hiding.<\/strong><\/li>\n<li>We cannot print <strong>super<\/strong>, there is a syntax error. Always data members of parent class is inherited by <strong>super<\/strong><\/li>\n<li>If you make any non static function of a class as <strong>final<\/strong> then it cannot be overridden by the child class that means to stop method overriding makes a function <strong>final<\/strong>.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Inheritance Quick Revision:<\/strong><\/h4>\n<ul>\n<li>Inheritance allows the class to use the states and behavior of another class using extends keyword<\/li>\n<li>Inheritance <strong>is-a <\/strong>relationship between a Base class and its child class.<\/li>\n<li>Multiple inheritance is not supported in JAVA.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is a mechanism that allows the class to use the states and behavior of another class. In simple words a class derive field and methods from another class. The derived class in inheritance is called sub class (also called derived class or extended class, or child class) and the class from which it is &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/inheritance\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Inheritance Tutorial With Example In JAVA<\/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-479","page","type-page","status-publish","hentry"],"psp_head":"<title>Inheritance Tutorial With Example In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial on Inheritance with lots of examples in JAVA. Also find details about types of Inheritance (single, multilevel and hierarchical), importance and why multiple inheritance is not supported in JAVA.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/inheritance\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/479","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=479"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/479\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/479\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}