{"id":1125,"date":"2022-02-24T16:40:23","date_gmt":"2022-02-24T16:40:23","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1125"},"modified":"2022-02-24T16:40:23","modified_gmt":"2022-02-24T16:40:23","slug":"java-inheritance","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-inheritance\/","title":{"rendered":"Java &#8211; Inheritance"},"content":{"rendered":"\n<p>The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">extends Keyword<\/h2>\n\n\n\n<p><strong>extends<\/strong>&nbsp;is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Super {\n   .....\n   .....\n}\nclass Sub extends Super {\n   .....\n   .....\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Code<\/h2>\n\n\n\n<p>Following is an example demonstrating Java inheritance. In this example, you can observe two classes namely Calculation and My_Calculation.<\/p>\n\n\n\n<p>Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of Calculation class.<\/p>\n\n\n\n<p>Copy and paste the following program in a file with name My_Calculation.java<\/p>\n\n\n\n<p><strong>Example<\/strong><a href=\"http:\/\/tpcg.io\/ZAlGXn\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculation {\n   int z;\n\t\n   public void addition(int x, int y) {\n      z = x + y;\n      System.out.println(\"The sum of the given numbers:\"+z);\n   }\n\t\n   public void Subtraction(int x, int y) {\n      z = x - y;\n      System.out.println(\"The difference between the given numbers:\"+z);\n   }\n}\n\npublic class My_Calculation extends Calculation {\n   public void multiplication(int x, int y) {\n      z = x * y;\n      System.out.println(\"The product of the given numbers:\"+z);\n   }\n\t\n   public static void main(String args&#91;]) {\n      int a = 20, b = 10;\n      My_Calculation demo = new My_Calculation();\n      demo.addition(a, b);\n      demo.Subtraction(a, b);\n      demo.multiplication(a, b);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Compile and execute the above code as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>javac My_Calculation.java\njava My_Calculation\n<\/code><\/pre>\n\n\n\n<p>After executing the program, it will produce the following result \u2212<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The sum of the given numbers:30\nThe difference between the given numbers:10\nThe product of the given numbers:200\n<\/code><\/pre>\n\n\n\n<p>In the given program, when an object to&nbsp;<strong>My_Calculation<\/strong>&nbsp;class is created, a copy of the contents of the superclass is made within it. That is why, using the object of the subclass you can access the members of a superclass.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/java\/images\/inheritance.jpg\" alt=\"Inheritance\"\/><\/figure>\n\n\n\n<p>The Superclass reference variable can hold the subclass object, but using that variable you can access only the members of the superclass, so to access the members of both classes it is recommended to always create reference variable to the subclass.<\/p>\n\n\n\n<p>If you consider the above program, you can instantiate the class as given below. But using the superclass reference variable (&nbsp;<strong>cal<\/strong>&nbsp;in this case) you cannot call the method&nbsp;<strong>multiplication()<\/strong>, which belongs to the subclass My_Calculation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Calculation demo = new My_Calculation();\ndemo.addition(a, b);\ndemo.Subtraction(a, b);<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>&nbsp;\u2212 A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The super keyword<\/h2>\n\n\n\n<p>The&nbsp;<strong>super<\/strong>&nbsp;keyword is similar to&nbsp;<strong>this<\/strong>&nbsp;keyword. Following are the scenarios where the super keyword is used.<\/p>\n\n\n\n<ul><li>It is used to&nbsp;<strong>differentiate the members<\/strong>&nbsp;of superclass from the members of subclass, if they have same names.<\/li><li>It is used to&nbsp;<strong>invoke the superclass<\/strong>&nbsp;constructor from subclass.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Differentiating the Members<\/h3>\n\n\n\n<p>If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>super.variable\nsuper.method();\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code<\/h3>\n\n\n\n<p>This section provides you a program that demonstrates the usage of the&nbsp;<strong>super<\/strong>&nbsp;keyword.<\/p>\n\n\n\n<p>In the given program, you have two classes namely&nbsp;<em>Sub_class<\/em>&nbsp;and&nbsp;<em>Super_class<\/em>, both have a method named display() with different implementations, and a variable named num with different values. We are invoking display() method of both classes and printing the value of the variable num of both classes. Here you can observe that we have used super keyword to differentiate the members of superclass from subclass.<\/p>\n\n\n\n<p>Copy and paste the program in a file with name Sub_class.java.<\/p>\n\n\n\n<p><strong>Example<\/strong><a href=\"http:\/\/tpcg.io\/C04irr\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Super_class {\n   int num = 20;\n\n   \/\/ display method of superclass\n   public void display() {\n      System.out.println(\"This is the display method of superclass\");\n   }\n}\n\npublic class Sub_class extends Super_class {\n   int num = 10;\n\n   \/\/ display method of sub class\n   public void display() {\n      System.out.println(\"This is the display method of subclass\");\n   }\n\n   public void my_method() {\n      \/\/ Instantiating subclass\n      Sub_class sub = new Sub_class();\n\n      \/\/ Invoking the display() method of sub class\n      sub.display();\n\n      \/\/ Invoking the display() method of superclass\n      super.display();\n\n      \/\/ printing the value of variable num of subclass\n      System.out.println(\"value of the variable named num in sub class:\"+ sub.num);\n\n      \/\/ printing the value of variable num of superclass\n      System.out.println(\"value of the variable named num in super class:\"+ super.num);\n   }\n\n   public static void main(String args&#91;]) {\n      Sub_class obj = new Sub_class();\n      obj.my_method();\n   }\n}<\/code><\/pre>\n\n\n\n<p>Compile and execute the above code using the following syntax.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>javac Super_Demo\njava Super\n<\/code><\/pre>\n\n\n\n<p>On executing the program, you will get the following result \u2212<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the display method of subclass\nThis is the display method of superclass\nvalue of the variable named num in sub class:10\nvalue of the variable named num in super class:20\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Invoking Superclass Constructor<\/h2>\n\n\n\n<p>If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>super(values);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code<\/h3>\n\n\n\n<p>The program given in this section demonstrates how to use the super keyword to invoke the parametrized constructor of the superclass. This program contains a superclass and a subclass, where the superclass contains a parameterized constructor which accepts a integer value, and we used the super keyword to invoke the parameterized constructor of the superclass.<\/p>\n\n\n\n<p>Copy and paste the following program in a file with the name Subclass.java<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>Live Demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Superclass {\n   int age;\n\n   Superclass(int age) {\n      this.age = age; \t\t \n   }\n\n   public void getAge() {\n      System.out.println(\"The value of the variable named age in super class is: \" +age);\n   }\n}\n\npublic class Subclass extends Superclass {\n   Subclass(int age) {\n      super(age);\n   }\n\n   public static void main(String args&#91;]) {\n      Subclass s = new Subclass(24);\n      s.getAge();\n   }\n}<\/code><\/pre>\n\n\n\n<p>Compile and execute the above code using the following syntax.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>javac Subclass\njava Subclass\n<\/code><\/pre>\n\n\n\n<p>On executing the program, you will get the following result \u2212<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The value of the variable named age in super class is: 24\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">IS-A Relationship<\/h2>\n\n\n\n<p>IS-A is a way of saying: This object is a type of that object. Let us see how the&nbsp;<strong>extends<\/strong>&nbsp;keyword is used to achieve inheritance.<\/p>\n\n\n\n<pre id=\"animal\" class=\"wp-block-code\"><code>public class Animal {\n}\n\npublic class Mammal extends Animal {\n}\n\npublic class Reptile extends Animal {\n}\n\npublic class Dog extends Mammal {\n}<\/code><\/pre>\n\n\n\n<p>Now, based on the above example, in Object-Oriented terms, the following are true \u2212<\/p>\n\n\n\n<ul><li>Animal is the superclass of Mammal class.<\/li><li>Animal is the superclass of Reptile class.<\/li><li>Mammal and Reptile are subclasses of Animal class.<\/li><li>Dog is the subclass of both Mammal and Animal classes.<\/li><\/ul>\n\n\n\n<p>Now, if we consider the IS-A relationship, we can say \u2212<\/p>\n\n\n\n<ul><li>Mammal IS-A Animal<\/li><li>Reptile IS-A Animal<\/li><li>Dog IS-A Mammal<\/li><li>Hence: Dog IS-A Animal as well<\/li><\/ul>\n\n\n\n<p>With the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.<\/p>\n\n\n\n<p>We can assure that Mammal is actually an Animal with the use of the instance operator.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>Live Demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n}\n\nclass Mammal extends Animal {\n}\n\nclass Reptile extends Animal {\n}\n\npublic class Dog extends Mammal {\n\n   public static void main(String args&#91;]) {\n      Animal a = new Animal();\n      Mammal m = new Mammal();\n      Dog d = new Dog();\n\n      System.out.println(m instanceof Animal);\n      System.out.println(d instanceof Mammal);\n      System.out.println(d instanceof Animal);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\ntrue\ntrue\n<\/code><\/pre>\n\n\n\n<p>Since we have a good understanding of the&nbsp;<strong>extends<\/strong>&nbsp;keyword, let us look into how the&nbsp;<strong>implements<\/strong>&nbsp;keyword is used to get the IS-A relationship.<\/p>\n\n\n\n<p>Generally, the&nbsp;<strong>implements<\/strong>&nbsp;keyword is used with classes to inherit the properties of an interface. Interfaces can never be extended by a class.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface Animal {\n}\n\npublic class Mammal implements Animal {\n}\n\npublic class Dog extends Mammal {\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The instanceof Keyword<\/h2>\n\n\n\n<p>Let us use the&nbsp;<strong>instanceof<\/strong>&nbsp;operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>Live Demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Animal{}\nclass Mammal implements Animal{}\n\npublic class Dog extends Mammal {\n\n   public static void main(String args&#91;]) {\n      Mammal m = new Mammal();\n      Dog d = new Dog();\n\n      System.out.println(m instanceof Animal);\n      System.out.println(d instanceof Mammal);\n      System.out.println(d instanceof Animal);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This will produce the following result \u2212<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\ntrue\ntrue\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">HAS-A relationship<\/h2>\n\n\n\n<p>These relationships are mainly based on the usage. This determines whether a certain class&nbsp;<strong>HAS-A<\/strong>&nbsp;certain thing. This relationship helps to reduce duplication of code as well as bugs.<\/p>\n\n\n\n<p>Lets look into an example \u2212<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Vehicle{}\npublic class Speed{}\n\npublic class Van extends Vehicle {\n   private Speed sp;\n} <\/code><\/pre>\n\n\n\n<p>This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.<\/p>\n\n\n\n<p>In Object-Oriented feature, the users do not need to bother about which object is doing the real work. To achieve this, the Van class hides the implementation details from the users of the Van class. So, basically what happens is the users would ask the Van class to do a certain action and the Van class will either do the work by itself or ask another class to perform the action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Inheritance<\/h2>\n\n\n\n<p>There are various types of inheritance as demonstrated below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tutorialspoint.com\/java\/images\/types_of_inheritance.jpg\" alt=\"Types of Inheritance\"\/><\/figure>\n\n\n\n<p>A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore following is illegal \u2212<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class extends Animal, Mammal{} <\/code><\/pre>\n\n\n\n<p>However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). extends Keyword extends&nbsp;is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. Syntax Sample Code Following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1125"}],"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=1125"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1125\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}