{"id":76897,"date":"2020-02-25T13:53:15","date_gmt":"2020-02-25T08:23:15","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=76897"},"modified":"2020-02-25T13:53:15","modified_gmt":"2020-02-25T08:23:15","slug":"java-inheritance","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/","title":{"rendered":"Java Inheritance &#8211; Types &amp; Importance of Inheritance with Real-life Examples!"},"content":{"rendered":"<p>Inheritance is one of the most important concepts of Object-Oriented Programming. Inheritance is the capability of one class to inherit capabilities or properties from another class in Java. For instance, we are humans.<\/p>\n<p>We inherit certain properties from the class \u2018Human\u2019 such as the ability to speak, breathe, eat, drink, etc.We can also take the example of cars. The class \u2018Car\u2019 inherits its properties from the class \u2018Automobiles\u2019 which inherits some of its properties from another class \u2018Vehicles\u2019.<\/p>\n<p>The object-oriented languages express this inheritance relationship by allowing one class to inherit from another. Thus a model of these languages is much closer to the real-world.<\/p>\n<p>The principle behind this kind of division is that each subclass (child-class) shares common characteristics with the class from which it is derived.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77004\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/inheritance-in-java.jpg\" alt=\"Java Inheritance\" width=\"653\" height=\"399\" \/><\/a><\/p>\n<p>The above figure illustrates:<\/p>\n<ul>\n<li><strong>Automobiles<\/strong> and <strong>Pulled Vehicles<\/strong> are subclasses of <strong>Vehicles<\/strong>.<\/li>\n<li><strong>Vehicles<\/strong> are the base class or superclass of <strong>Automobiles<\/strong> and <strong>pulled Vehicles<\/strong>.<\/li>\n<li><strong>Car<\/strong> and <strong>Bus<\/strong> are sub-classes or derived classes of <strong>Automobiles<\/strong>.<\/li>\n<li><strong>Automobiles<\/strong> are the base class or superclass of <strong>Car<\/strong> and <strong>Bus.<\/strong><\/li>\n<\/ul>\n<h3>Why Java Inheritance?<\/h3>\n<p>There are several reasons why inheritance was introduced into Object-oriented languages. We will discuss some major reasons behind the introduction of inheritance.<\/p>\n<ul>\n<li>The capability to express inheritance relationships ensures the closeness with the <strong>real-world models.<\/strong><\/li>\n<li>Another reason is the idea of <strong>reusability.<\/strong> One can derive a new class (sub-class) from an existing class and add new features to it without modifying its parent class. There is no need to rewrite the parent class in order to inherit it.<\/li>\n<li>One reason is the <strong>transitive nature.<\/strong> If class <strong>A<\/strong> inherits properties from another class <strong>B<\/strong>, then all subclasses of <strong>A<\/strong> will automatically inherit properties from <strong>B<\/strong>. This property is called the transitive nature of inheritance.<\/li>\n<\/ul>\n<p><em><strong>Note:<\/strong> A subclass defines only those features that are unique to it. <\/em><\/p>\n<p>For example, the class<strong> Student<\/strong> inherits from the class <strong>Person<\/strong>. Then although Student is a person, the reverse is not true. A Person need not be a Student. The class Student has properties that it does not share with class Person.<\/p>\n<p>For instance, the <strong>Student<\/strong> has a marks-percentage, but the <strong>Person<\/strong> does not have.<\/p>\n<h3>Important Terms in Java Inheritance<\/h3>\n<p><strong>1. Class:<\/strong> Class is a user-defined datatype in Java that is basically a group of objects. It is a blueprint or template from which we create objects.<\/p>\n<p><strong>2. Super Class:<\/strong> The class whose features and functionalities are being inherited or used is known as the superclass or a base class or a parent class.<\/p>\n<p><strong>3. Sub Class:<\/strong> The class that inherits the properties and features from another class is known as a subclass or a derived class or extended class or child class. The subclass can add its own features and functions in addition to the fields and methods of its superclass or the parent class.<\/p>\n<p><strong>4. The extends keyword:<\/strong> The keyword extends is used by child class while inheriting the parent class.<\/p>\n<p><strong>5. The super keyword:<\/strong> The super keyword is similar to this keyword. The following are some cases where we use super keyword :<\/p>\n<ul>\n<li>There are some situations where the members of the superclass and the subclass have the same names, then the super keyword is used to differentiate the members of the superclass from the members of the subclass.<\/li>\n<li>To invoke the superclass constructor from the subclass.<\/li>\n<\/ul>\n<p><strong>Syntax of using Inheritance in Java:<\/strong><\/p>\n<p>We already know that to inherit a class, we use the extends keyword. The syntax of using inheritance in Java is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class BaseClass\n{\n \t\/\/methods and fields\n}\nclass DerivedClass extends BaseClass\n{\n \t\/\/methods and fields\n}<\/pre>\n<p><strong>Code to explain Java Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\/\/Base class\nclass Person\n{\n  String name = \"John\";\n  int age =17;\n  String city = \"Delhi\";\n  public void show()\n  {\n    System.out.println(\"Student inheriting properties from Person:\\n\");\n  }\n}\n\/\/child class\nclass Student extends Person\n{\n  \/\/ defining additional properties to child class\n  int marks = 78;\n  String tutorial = \"TechVidvan Tutorial of Java\";\n\n  public static void main(String args[])\n  {\n    Student obj = new Student();\n    obj.show();\n\n    System.out.println(\"Name of the student is: \" + obj.name);\n    System.out.println(\"Age of the student is: \" + obj.age);\n    System.out.println(\"Student lives in: \" + obj.city);\n    System.out.println(\"Student learns from: \" + obj.tutorial);\n    System.out.println(\"Marks obtained by the student is: \" + obj.marks);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Student inheriting properties from Person:<br \/>\nName of the student is: John<br \/>\nAge of the student is: 17<br \/>\nStudent lives in: Delhi<br \/>\nStudent learns from: TechVidvan Tutorial of Java<br \/>\nMarks obtained by the student is: 78<\/div>\n<p>From the above program, we can say that <strong>Student IS-A Person.<\/strong> This means that a derived class has an <strong>IS-A<\/strong> relationship with the base class. This inheritance is called <strong>IS-A<\/strong> relationship between the child and parent class.<\/p>\n<p>In the above code, when an object of Student class is created, a copy of all the methods and fields of the superclass acquire memory in this object. Therefore, we are able to access the members of the superclass by using the object of the subclass.<\/p>\n<p>Please note that during inheritance, we create the object of only the subclass, not the superclass.<\/p>\n<h3>Types of Java Inheritance<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/types-of-java-inheritance.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77018\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/types-of-java-inheritance.jpg\" alt=\"Java Inheritance types\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<p>From the above diagram, we can see that there are five types of inheritance in Java. They are classified on the basis of the number of super and subclasses.<\/p>\n<p>There is an exception that \u2018multiple inheritance\u2019 is not directly supported by classes in Java. Rather we use interfaces to implement multiple inheritances in Java.<\/p>\n<p>Now, we will discuss each type of inheritance with examples and programs.<\/p>\n<h4>1. Single Inheritance in Java<\/h4>\n<p>In single inheritance, there is a single child class that inherits properties from one parent class.<\/p>\n<p>In the following diagram, class A is a base class that is derived from class B. It is also known as single-level inheritance.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/single-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77005\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/single-inheritance-in-java.jpg\" alt=\"single inheritance\" width=\"346\" height=\"328\" \/><\/a><\/p>\n<p><strong>Syntax of single Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class A\n{\n  \/\/methods and fields\n}\nClass B extends A\n{\n  \/\/methods and fields<\/pre>\n<p><strong>Code to illustrate Single Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\/\/Base class\nclass Person\n{\n  String name=\"John\";\n  public void show()\n  {\n    System.out.println(\"Student inheriting properties from Person\");\n  }\n}\n\/\/child class\nclass Student extends Person\n{\n  \/\/ defining additional properties to child class\n  String course = \"Techvidvan's Java Course\";\n  public void show1()\n  {\n    System.out.println(\"I am a Student who belongs to Person class\");\n  }\n  public static void main(String args[])\n  {\n    Student obj = new Student();\n    obj.show();\n    obj.show1();\n    System.out.println(\"Name of student: \" +obj.name);\n    System.out.println(\"Course opted by the student: \" +obj.course);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Student inheriting properties from Person<br \/>\nI am a Student who belongs to Person class<br \/>\nName of student: John<br \/>\nCourse opted by the student: Techvidvan&#8217;s Java Course<\/div>\n<h4>2. Multilevel Inheritance in Java<\/h4>\n<p>In this type of inheritance, the child or derived class inherits the features of the superclass and simultaneously this child class acts as a superclass for another derived class.<\/p>\n<p>In the following diagram, class A is a base class that is derived from class B, which in turn, acts as a base class for a derived class C.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/multilevel-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77006\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/multilevel-inheritance-in-java.jpg\" alt=\"multilevel inheritance\" width=\"400\" height=\"404\" \/><\/a><\/p>\n<p><strong>Code to illustrate Multilevel Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\/\/Base class\nclass Person\n{\n  public void show()\n  {\n    System.out.println(\"Student inheriting properties from Person\");\n  }\n}\nclass Student extends Person\n{\n  public void show1()\n  {\n      System.out.println(\"I am a Student who belongs to Person class\");\n  }\n}\n\/\/child class\nclass EngineeringStudent extends Student\n{\n  \/\/ defining additional properties to the child class\n  public void show2()\n  {\n    System.out.println(\"Engineering Student inheriting properties from Student\");\n  }\n}\npublic class MultilevelDemo\n{\n  public static void main(String args[])\n  {\n    EngineeringStudent obj = new EngineeringStudent();\n    obj.show();\n    obj.show1();\n    obj.show2();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Student inheriting properties from Person<br \/>\nI am a Student who belongs to Person class<br \/>\nEngineering Student inheriting properties from Student<\/div>\n<h4>3. Hierarchical Inheritance in Java<\/h4>\n<p>In Hierarchical Inheritance, one class acts as a superclass (base class) for more than one subclass. More than one subclass can inherit the features of a base class.<\/p>\n<p>In the following diagram, class A is a base class for the derived classes B, C, and D.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/hierarchial-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77007\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/hierarchial-inheritance-in-java.jpg\" alt=\"Hierarchial Inheritance\" width=\"442\" height=\"374\" \/><\/a><\/p>\n<p><strong>Code to illustrate Hierarchical Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\n\/\/Base class\nclass Person\n{\n  public void show()\n  {\n  System.out.println(\"I am a Person\");\n  }\n}\n\n\/\/child class1\nclass Student extends Person\n{\n  public void show1()\n  {\n  System.out.println(\"I am a Student who is Person \");\n  }\n}\n\n\/\/child class2\nclass Teacher extends Person\n{\n  \/\/ defining additional properties to the child class\n\n  public void show2()\n  {\n    System.out.println(\"I am a Teacher who is a Person\");\n  }\n}\n\/\/child class3\nclass Doctor extends Person\n{\n  \/\/ defining additional properties to the child class\n\n  public void show3()\n  {\n    System.out.println(\"I am a Doctor who is a Person\");\n  }\n}\n\npublic class HierarchicalInheritance\n{\n  public static void main(String args[])\n  {\n    Teacher teacher = new Teacher();\n    Student student = new Student();\n    Doctor doctor = new Doctor();\n    student.show();\n    student.show1();\n    teacher.show2();\n    doctor.show3();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am a Person<br \/>\nI am a Student who is Person<br \/>\nI am a Teacher who is a Person<br \/>\nI am a Doctor who is a Person<\/div>\n<h4>4. Multiple Inheritance in Java<\/h4>\n<p>In Multiple Inheritance, one child or subclass class can have more than one base class or superclass and inherit features from every parent class which it inherits.<\/p>\n<p>We have already discussed that Java does not support multiple inheritances with classes. We can achieve multiple inheritances only with the help of Interfaces.<\/p>\n<p>In the following diagram, Class C inherits from interfaces A and B.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/multiple-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77008\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/multiple-inheritance-in-java.jpg\" alt=\"Multiple Inheritance\" width=\"376\" height=\"344\" \/><\/a><\/p>\n<p><strong>Code to illustrate Multiple Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\n\/\/base interface1\ninterface Moveable\n{\n  public void run();\n}\n\n\/\/base interface2\ninterface Speakable\n{\n  public void speak();\n}\n\n\/\/child interface inheriting two base interfaces\ninterface Ability extends Moveable, Speakable\n{\n  public void show();\n}\n\nclass Person implements Ability\n{\n  @Override\n  public void run()\n  {\n    System.out.println(\"I can run !!\");\n  }\n  @Override\n  public void speak()\n  {\n    System.out.println(\"I can speak !!\");\n  }\n  @Override\n  public void show() \n  {\n    System.out.println(\"I am a person, I can speak and run !!\");\n  }\n}\n\npublic class MultipleInheritance\n{\n  public static void main(String[] args)\n  {\n    Person obj = new Person();\n    obj.run();\n    obj.speak();\n    obj.show();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I can run !!<br \/>\nI can speak !!<br \/>\nI am a person, I can speak and run !!<\/div>\n<h4>5. Hybrid Inheritance in Java<\/h4>\n<p>It is a combination of two or more types of inheritance. The hybrid inheritance is also not possible with classes because Java doesn\u2019t support multiple inheritance with classes. We can achieve hybrid inheritance only through Interfaces.<\/p>\n<p>In the following diagram, class A is the base class for subclasses B and C. And, class D inherits both the classes B and C.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/hybrid-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77009\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/hybrid-inheritance-in-java.jpg\" alt=\"Hybrid Inheritance\" width=\"386\" height=\"378\" \/><\/a><\/p>\n<p><strong>Code to illustrate hybrid Inheritance:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.inheritance;\n\/\/base class 1\nclass Ability\n{\n  public void show()\n  {\n    System.out.println(\"I am a person, I can speak and run !!\");\n  }\n}\n\n\/\/child interface 1\ninterface Moveable\n{\n  public void run();\n}\n\n\/\/child interface2\ninterface Speakable\n{\n  public void speak();\n}\n\n\/\/child class inheriting two base interfaces\nclass Person extends Ability implements Moveable, Speakable\n{\n  @Override\n  public void run()\n  {\n    System.out.println(\"I can run !!\");\n  }\n  @Override\n  public void speak()\n  {\n    System.out.println(\"I can speak !!\");\n  }\n}\n\npublic class HybridInheritance\n{\n  public static void main(String[] args)\n  {\n    Person obj = new Person();\n    obj.run();\n    obj.speak();\n    obj.show();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I can run !!<br \/>\nI can speak !!<br \/>\nI am a person, I can speak and run !!<\/div>\n<h3>Inheritance in OOP with Real-time Example<\/h3>\n<p>Consider an application Polygon that represents different types of Shapes.<\/p>\n<p>We are supposed to create two different types of Polygons, one will be Rectangle and the other will be Triangle.<\/p>\n<p>Let&#8217;s compare and study the two different approaches of coding with a structured and object-oriented programming perspective.<\/p>\n<p><strong>Structural approach:<\/strong><\/p>\n<p>Using a structured programming approach, we will create two functions:<\/p>\n<ul>\n<li>One to get the Number of sides of a polygon.<\/li>\n<li>And the other to calculate the area.<\/li>\n<\/ul>\n<p>The working of these functions remains the same across two different shapes.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/strucutral-approach.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77011\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/strucutral-approach.jpg\" alt=\"Structural Approach in Java\" width=\"395\" height=\"372\" \/><\/a><\/p>\n<p><strong>OOP&#8217;s approach:<\/strong><\/p>\n<p>Using the OOPs programming approach, we would create two different classes.<\/p>\n<ul>\n<li>Each having implementation of thegetNumberOfSides() and getArea() functions.<\/li>\n<li>This will reduce extra work.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/OOPs-Approach.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77012\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/OOPs-Approach.jpg\" alt=\"OOP's Approach\" width=\"504\" height=\"276\" \/><\/a><\/p>\n<h4>Change Request in Software<\/h4>\n<p>Suppose there is a change in the functionality of the software. You are supposed to add the functionality of finding the area of a Square.<\/p>\n<p>Let\u2019s see how to deal with this problem with both approaches.<\/p>\n<p><strong>Structural approach:<\/strong><\/p>\n<p>If we want to add a new feature using a functional or traditional methodology, we will need to modify the getArea() function which is already tested and baselined. If we add new functionality of finding the area of a Square then our code will look like:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/structural-approach-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77013\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/structural-approach-1.jpg\" alt=\"Structural Approach 1\" width=\"607\" height=\"400\" \/><\/a><\/p>\n<p><strong>OOP&#8217;s approach:<\/strong><\/p>\n<p>Using the Object-Oriented approach, you just require to add a new class Square which will have the unique functionality of finding the area of Square. There is no need to change the piece of code that is already tested by using this approach.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/oop-approach.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77015\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/oop-approach.jpg\" alt=\"The OOP Approach\" width=\"691\" height=\"328\" \/><\/a><\/p>\n<h4>Another Change Request<\/h4>\n<p>Suppose if there are some more changes required in the software. For example, if you want to add a Shape Parallelogram with its own unique requirements.<\/p>\n<p><strong>Structural approach:<\/strong><\/p>\n<p>If we want to add the Parallelogram Shape in the existing class using the structural approach, then we will definitely need to make changes in the existing code.<\/p>\n<p><strong>OOP&#8217;s approach:<\/strong><\/p>\n<p>If we want to add another shape in the existing class using the object-oriented approach, we will just need to create a new class Parallelogram with its unique methods. The diagram below illustrates the same &#8211;<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/OOPs-Approach-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77014\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/OOPs-Approach-1.jpg\" alt=\"OOP's Approach 1\" width=\"550\" height=\"374\" \/><\/a><\/p>\n<p>So, even though at the initial stage, the structural programming was appearing to be an easy approach, but as the complexity of the code increases and there are more changes in the requirements then this approach fails.<\/p>\n<p>Eventually, the Object-Oriented approach wins in the long term.<\/p>\n<p>But you might be thinking that across all the classes, we have to write a repeated piece of code for each class.<\/p>\n<p>To overcome this problem of repetition, we can create a parent class called \u201cShape\u201d and implement the same function of getNumberOfSides and getArea. Then we will create the child classes that will inherit this parent class Shape.<\/p>\n<p>So that they will have access to getNumberOfSides and getArea functions in the Shape class.<\/p>\n<p>There is no need to declare these functions in each class. This concept is called Inheritance in java.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/repeated-piece-of-cod-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77016\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/repeated-piece-of-cod-1.jpg\" alt=\"piece of code\" width=\"750\" height=\"208\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/use-of-inheritance-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77017\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/02\/use-of-inheritance-in-java.jpg\" alt=\"java inheritance use\" width=\"720\" height=\"576\" \/><\/a><\/p>\n<p>So you can clearly see that with the help of the Inheritance approach of OOP we can easily update our code without disturbing the code which is already tested.<\/p>\n<h3>Summary<\/h3>\n<p>Inheritance is the most essential feature of Object-oriented programming. It helps in reducing the complexity of writing very large codes as it provides the code reuse feature.<\/p>\n<p>Coming to the end of this article, we have learned the basics of inheritance in Java, the importance of inheritance as well as various types of inheritances in Java with coding examples and diagram illustrations so that you can understand the concept easily.<\/p>\n<p>We also discussed the importance of inheritance with some real-world examples that can be further helpful for you in programming in the real world.<\/p>\n<p>Thank you for reading our article. If you have any queries or suggestions related to Java Inheritance, let us know by leaving a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is one of the most important concepts of Object-Oriented Programming. Inheritance is the capability of one class to inherit capabilities or properties from another class in Java. For instance, we are humans. We&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":77018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[1851,1852,1853,1854,1855,1856,1857,1858,1859,1860,1861],"class_list":["post-76897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-example-of-java-inheritance","tag-hierarchical-inheritance-in-java","tag-hybrid-inheritance-in-java","tag-inheritance-in-java","tag-java-inheritance","tag-java-inheritance-inheritance","tag-java-inheritance-types","tag-multilevel-inheritance-in-java","tag-multiple-inheritance-in-java","tag-single-inheritance-in-java","tag-why-java-inheritance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Inheritance - Types &amp; Importance of Inheritance with Real-life Examples! - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java Inheritance- Learn Inheritance in Java along with its importance, types explained with real-life examples, codings and diagram for easy understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Inheritance - Types &amp; Importance of Inheritance with Real-life Examples! - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java Inheritance- Learn Inheritance in Java along with its importance, types explained with real-life examples, codings and diagram for easy understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-25T08:23:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Inheritance - Types &amp; Importance of Inheritance with Real-life Examples! - TechVidvan","description":"Java Inheritance- Learn Inheritance in Java along with its importance, types explained with real-life examples, codings and diagram for easy understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/","og_locale":"en_US","og_type":"article","og_title":"Java Inheritance - Types &amp; Importance of Inheritance with Real-life Examples! - TechVidvan","og_description":"Java Inheritance- Learn Inheritance in Java along with its importance, types explained with real-life examples, codings and diagram for easy understanding.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-02-25T08:23:15+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Inheritance &#8211; Types &amp; Importance of Inheritance with Real-life Examples!","datePublished":"2020-02-25T08:23:15+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/"},"wordCount":1859,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg","keywords":["example of Java inheritance","Hierarchical Inheritance in Java","Hybrid Inheritance in Java","Inheritance in Java","java inheritance","Java Inheritance Inheritance","Java Inheritance Types","Multilevel Inheritance in Java","Multiple Inheritance in Java","Single Inheritance in Java","Why Java Inheritance"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/","url":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/","name":"Java Inheritance - Types &amp; Importance of Inheritance with Real-life Examples! - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg","datePublished":"2020-02-25T08:23:15+00:00","description":"Java Inheritance- Learn Inheritance in Java along with its importance, types explained with real-life examples, codings and diagram for easy understanding.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-inheritance\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/02\/types-of-java-inheritance.jpg","width":802,"height":420,"caption":"Java Inheritance types"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-inheritance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Inheritance &#8211; Types &amp; Importance of Inheritance with Real-life Examples!"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=76897"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76897\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/77018"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=76897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=76897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=76897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}