{"id":1202,"date":"2022-02-25T16:45:51","date_gmt":"2022-02-25T16:45:51","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1202"},"modified":"2022-02-25T16:45:51","modified_gmt":"2022-02-25T16:45:51","slug":"java-inheritance-2","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-inheritance-2\/","title":{"rendered":"Java Inheritance"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java inheritance and its types with the help of example.<\/p>\n\n\n\n<p id=\"introduction\">Inheritance is one of the key features of OOP that allows us to create a new class from an existing class.<\/p>\n\n\n\n<p>The new class that is created is known as&nbsp;<strong>subclass<\/strong>&nbsp;(child or derived class) and the existing class from where the child class is derived is known as&nbsp;<strong>superclass<\/strong>&nbsp;(parent or base class).<\/p>\n\n\n\n<p>The&nbsp;<code>extends<\/code>&nbsp;keyword is used to perform inheritance in Java. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  \/\/ methods and fields\n}\n\n\/\/ use of extends keyword\n\/\/ to perform inheritance\nclass Dog extends Animal {\n\n  \/\/ methods and fields of Animal\n  \/\/ methods and fields of Dog\n}<\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<var>Dog<\/var>&nbsp;class is created by inheriting the methods and fields from the&nbsp;<var>Animal<\/var>&nbsp;class.<\/p>\n\n\n\n<p>Here,&nbsp;<var>Dog<\/var>&nbsp;is the subclass and&nbsp;<var>Animal<\/var>&nbsp;is the superclass.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example 1: Java Inheritance<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ field and method of the parent class\n  String name;\n  public void eat() {\n    System.out.println(\"I can eat\");\n  }\n}\n\n\/\/ inherit from Animal\nclass Dog extends Animal {\n\n  \/\/ new method in subclass\n  public void display() {\n    System.out.println(\"My name is \" + name);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the subclass\n    Dog labrador = new Dog();\n\n    \/\/ access field of superclass\n    labrador.name = \"Rohu\";\n    labrador.display();\n\n    \/\/ call method of superclass\n    \/\/ using object of subclass\n    labrador.eat();\n\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>My name is Rohu\nI can eat<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have derived a subclass&nbsp;<var>Dog<\/var>&nbsp;from superclass&nbsp;<var>Animal<\/var>. Notice the statements,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>labrador.name = \"Rohu\";\n\nlabrador.eat();<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>labrador<\/var>&nbsp;is an object of&nbsp;<var>Dog<\/var>. However,&nbsp;<var>name<\/var>&nbsp;and&nbsp;<code>eat()<\/code>&nbsp;are the members of the&nbsp;<var>Animal<\/var>&nbsp;class.<\/p>\n\n\n\n<p>Since&nbsp;<var>Dog<\/var>&nbsp;inherits the field and method from&nbsp;<var>Animal<\/var>, we are able to access the field and method using the object of the&nbsp;<var>Dog<\/var>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-inheritance-implementation.png\" alt=\"Subclass Dog can access the field and method of the superclass Animal.\" title=\"Java Inheritance Implementation\"\/><figcaption>Java Inheritance Implementation<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"is-a\">is-a relationship<\/h2>\n\n\n\n<p>In Java, inheritance is an&nbsp;<strong>is-a<\/strong>&nbsp;relationship. That is, we use inheritance only if there exists an is-a relationship between two classes. For example,<\/p>\n\n\n\n<ul><li><strong>Car<\/strong>&nbsp;is a&nbsp;<strong>Vehicle<\/strong><\/li><li><strong>Orange<\/strong>&nbsp;is a&nbsp;<strong>Fruit<\/strong><\/li><li><strong>Surgeon<\/strong>&nbsp;is a&nbsp;<strong>Doctor<\/strong><\/li><li><strong>Dog<\/strong>&nbsp;is an&nbsp;<strong>Animal<\/strong><\/li><\/ul>\n\n\n\n<p>Here,&nbsp;<strong>Car<\/strong>&nbsp;can inherit from&nbsp;<strong>Vehicle<\/strong>,&nbsp;<strong>Orange<\/strong>&nbsp;can inherit from&nbsp;<strong>Fruit<\/strong>, and so on.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"method-overriding\">Method Overriding in Java Inheritance<\/h2>\n\n\n\n<p>In&nbsp;<strong>Example 1<\/strong>, we see the object of the subclass can access the method of the superclass.<\/p>\n\n\n\n<p><strong>However, if the same method is present in both the superclass and subclass, what will happen?<\/strong><\/p>\n\n\n\n<p>In this case, the method in the subclass overrides the method in the superclass. This concept is known as method overriding in Java.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Method overriding in Java Inheritance<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ method in the superclass\n  public void eat() {\n    System.out.println(\"I can eat\");\n  }\n}\n\n\/\/ Dog inherits Animal\nclass Dog extends Animal {\n\n  \/\/ overriding the eat() method\n  @Override\n  public void eat() {\n    System.out.println(\"I eat dog food\");\n  }\n\n  \/\/ new method in subclass\n  public void bark() {\n    System.out.println(\"I can bark\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the subclass\n    Dog labrador = new Dog();\n\n    \/\/ call the eat() method\n    labrador.eat();\n    labrador.bark();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I eat dog food\nI can bark<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<code>eat()<\/code>&nbsp;method is present in both the superclass&nbsp;<var>Animal<\/var>&nbsp;and the subclass&nbsp;<var>Dog<\/var>.<\/p>\n\n\n\n<p>Here, we have created an object&nbsp;<var>labrador<\/var>&nbsp;of&nbsp;<var>Dog<\/var>.<\/p>\n\n\n\n<p>Now when we call&nbsp;<code>eat()<\/code>&nbsp;using the object&nbsp;<var>labrador<\/var>, the method inside&nbsp;<var>Dog<\/var>&nbsp;is called. This is because the method inside the derived class overrides the method inside the base class.<\/p>\n\n\n\n<p>This is called method overriding.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We have used the\u00a0<code>@Override<\/code>\u00a0annotation to tell the compiler that we are overriding a method. However, the annotation is not mandatory.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"super\">super Keyword in Java Inheritance<\/h2>\n\n\n\n<p>Previously we saw that the same method in the subclass overrides the method in superclass.<\/p>\n\n\n\n<p>In such a situation, the&nbsp;<code>super<\/code>&nbsp;keyword is used to call the method of the parent class from the method of the child class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: super Keyword in Inheritance<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n  \/\/ method in the superclass\n  public void eat() {\n    System.out.println(\"I can eat\");\n  }\n}\n\n\/\/ Dog inherits Animal\nclass Dog extends Animal {\n\n  \/\/ overriding the eat() method\n  @Override\n  public void eat() {\n\n    \/\/ call method of superclass\n    super.eat();\n    System.out.println(\"I eat dog food\");\n  }\n\n  \/\/ new method in subclass\n  public void bark() {\n    System.out.println(\"I can bark\");\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the subclass\n    Dog labrador = new Dog();\n\n    \/\/ call the eat() method\n    labrador.eat();\n    labrador.bark();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I can eat\nI eat dog food\nI can bark <\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, the&nbsp;<code>eat()<\/code>&nbsp;method is present in both the base class&nbsp;<var>Animal<\/var>&nbsp;and the derived class&nbsp;<var>Dog<\/var>. Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>super.eat();<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>super<\/code>&nbsp;keyword is used to call the&nbsp;<code>eat()<\/code>&nbsp;method present in the superclass.<\/p>\n\n\n\n<p>We can also use the\u00a0<code>super<\/code>\u00a0keyword to call the constructor of the superclass from the constructor of the subclass. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"protected-keyword\">protected Members in Inheritance<\/h2>\n\n\n\n<p>In Java, if a class includes&nbsp;<code>protected<\/code>&nbsp;fields and methods, then these fields and methods are accessible from the subclass of the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: protected Members in Inheritance<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  protected String name;\n\n  protected void display() {\n    System.out.println(\"I am an animal.\");\n  }\n}\n\nclass Dog extends Animal {\n\n  public void getInfo() {\n    System.out.println(\"My name is \" + name);\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the subclass\n    Dog labrador = new Dog();\n\n    \/\/ access protected field and method\n    \/\/ using the object of subclass\n    labrador.name = \"Rocky\";\n    labrador.display();\n\n    labrador.getInfo();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>I am an animal.\nMy name is Rocky<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a class named Animal. The class includes a protected field:&nbsp;<var>name<\/var>&nbsp;and a method:&nbsp;<code>display()<\/code>.<\/p>\n\n\n\n<p>We have inherited the&nbsp;<var>Dog<\/var>&nbsp;class inherits&nbsp;<var>Animal<\/var>. Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>labrador.name = \"Rocky\";\nlabrador.display();<\/code><\/pre>\n\n\n\n<p>Here, we are able to access the protected field and method of the superclass using the&nbsp;<var>labrador<\/var>&nbsp;object of the subclass.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantage\">Why use inheritance?<\/h2>\n\n\n\n<ul><li>The most important use of inheritance in Java is code reusability. The code that is present in the parent class can be directly used by the child class.<\/li><li>Method overriding is also known as runtime polymorphism. Hence, we can achieve Polymorphism in Java with the help of inheritance.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types\">Types of inheritance<\/h2>\n\n\n\n<p>There are five types of inheritance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Single Inheritance<\/h3>\n\n\n\n<p>In single inheritance, a single subclass extends from a single superclass. For example,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-single-inheritance.png\" alt=\"Class A inherits from class B.\" title=\"Java Single Inheritance\"\/><figcaption>Java Single Inheritance<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Multilevel Inheritance<\/h3>\n\n\n\n<p>In multilevel inheritance, a subclass extends from a superclass and then the same subclass acts as a superclass for another class. For example,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-multilevel-inheritance.png\" alt=\"Class B inherits from class A and class C inherits from class B.\" title=\"Java Multilevel Inheritance\"\/><figcaption>Java Multilevel Inheritance<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Hierarchical Inheritance<\/h3>\n\n\n\n<p>In hierarchical inheritance, multiple subclasses extend from a single superclass. For example,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-hierarchical-inheritance.png\" alt=\"Both classes B and C inherit from the single class A.\" title=\"Java Hierarchical Inheritance\"\/><figcaption>Java Hierarchical Inheritance<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Multiple Inheritance<\/h3>\n\n\n\n<p>In multiple inheritance, a single subclass extends from multiple superclasses. For example,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-multiple-inheritance.png\" alt=\"Class C inherits from both classes A and B.\" title=\"Java Multiple Inheritance\"\/><figcaption>Java Multiple Inheritance<\/figcaption><\/figure>\n\n\n\n<p><strong>Note<\/strong>: Java doesn&#8217;t support multiple inheritance. However, we can achieve multiple inheritance using interfaces. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Hybrid Inheritance<\/h3>\n\n\n\n<p>Hybrid inheritance is a combination of two or more types of inheritance. For example,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-hybrid-inheritance.png\" alt=\"Class B and C inherit from a single class A and class D inherits from both the class B and C.\" title=\"Java Hybrid Inheritance\"\/><figcaption>Java Hybrid Inheritance<\/figcaption><\/figure>\n\n\n\n<p>Here, we have combined hierarchical and multiple inheritance to form a hybrid inheritance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java inheritance and its types with the help of example. Inheritance is one of the key features of OOP that allows us to create a new class from an existing class. The new class that is created is known as&nbsp;subclass&nbsp;(child or derived class) and the existing class from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[531],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1202"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}