Inheritance could be described as the procedure where one class obtains the properties (methods and fields) of one more. With the employ of inheritance, the knowledge is made convenient in a hierarchical order.
The class, which inherits the properties of others, is recognized as a subclass (derived class, child class) and the group whose properties are inherited is identified as superclass (base class, parent class).
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.
class Super { ..... ..... } class Sub extends Super { ..... ..... }
Following is an example demonstrating Java inheritance. In this example, you can observe two classes namely Calculation and My_Calculation.
Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of Calculation class.
Copy and paste the following program in a file with name My_Calculation.java
class Calculation { int z; public void addition(int x, int y) { z = x + y; System.out.println("The sum of the given numbers:"+z); } public void Subtraction(int x, int y) { z = x - y; System.out.println("The difference between the given numbers:"+z); } } public class My_Calculation extends Calculation { public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given numbers:"+z); } public static void main(String args[]) { int a = 20, b = 10; My_Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b); demo.multiplication(a, b); } }
Compile and execute the above code as shown below.
javac My_Calculation.java java My_Calculation
After executing the program, it will produce the following result:
The sum of the given numbers:30 The difference between the given numbers:10 The product of the given numbers:200
In the known program, when an entity to My_Calculation class is generated, a copy of the contents of the super class is completed in it. That is why; utilizing the object of the subclass, you could entrée the members of a superclass.
The Superclass position changeable could grasp the subclass object, however utilizing that inconsistent you can contact only the members of the superclass, so to admission the members of together classes it is optional to always build reference patchy to the subclass.
If you believe the above plan, you can instantiate the course group as known below. Except utilizing the superclass position changeable ( cal in this case) you never call the system multiplication(), which goes to the subclass My_Calculation.
Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b);
Note − A subclass become heir to entire the members from its superclass. Constructors are not affiliates, so they are not succeed to by subclasses, however the constructor of the superclass can be appeal to the subclass.
The super keyword
The super keyword is alike to this keyword. Subsequent are the scenarios where the wonderful keyword is used.
If a class be left the properties of a new class. In addition, if the affiliates of the super class have the names similar as the sub class, to discriminate these changeable we use super keyword as revealed below.
super.variable super.method();
This section offers you a agenda that reveals the practice of the super keyword.
In the specified program, you comprise two classes specifically Sub_class and Super_class, both have a technique named display() with dissimilar implementations, and a changeable named num with diverse values. We are appealing to display() method of together classes and printing the worth of the changeable num of both classes. Here you can watch that we contain used super keyword to distinguish the members of superclass from subclass.
Copy and paste the program in a file with name Sub_class.java.
class Super_class { int num = 20; // display method of superclass public void display() { System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; // display method of sub class public void display() { System.out.println("This is the display method of subclass"); } public void my_method() { // Instantiating subclass Sub_class sub = new Sub_class(); // Invoking the display() method of sub class sub.display(); // Invoking the display() method of superclass super.display(); // printing the value of variable num of subclass System.out.println("value of the variable named num in sub class:"+ sub.num); // printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]) { Sub_class obj = new Sub_class(); obj.my_method(); } }
Compile and execute the above code using the following syntax.
javac Super_Demo java Super
On executing the program, you will get the following result:
This is the display method of subclass This is the display method of superclass value of the variable named num in sub class:10 value of the variable named num in super class:20
If a class is herring the properties of one more class, the subclass mechanically obtains the defaulting constructor of the superclass. However, if you desire to call a parameterized constructor of the superclass, you require using the super keyword as exposed below.
super(values);
The program known in this section reveals how to employ the super keyword to appeal to the parametrized constructor of the super class. This program holds a superclass and a subclass, where the superclass have a parameterized constructor which recognizes a integer price, and we utilized the super keyword to call upon the parameterized constructor of the superclass.
class Superclass { int age; Superclass(int age) { this.age = age; } public void getAge() { System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age) { super(age); } public static void main(String args[]) { Subclass s = new Subclass(24); s.getAge(); } }
Compile and execute the above code using the following syntax.
javac Subclass java Subclass
On executing the program, you will get the following result:
The value of the variable named age in super class is: 24
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }
Now, based on the above example, in Object-Oriented terms, the following are true −
Now, if we consider the IS-A relationship, we can say −
With the utilize of the expands keyword, the subclasses would be intelligent to inherit every the property of the superclass excluding for the confidential properties of the superclass.
We can assure that Mammal is actually an Animal with the use of the instance operator.
class Animal { } class Mammal extends Animal { } class Reptile extends Animal { } public class Dog extends Mammal { public static void main(String args[]) { Animal a = new Animal(); Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } }
This will produce the following result:
true true true
while we contain a high-quality appreciating of the expands keyword, let us appear into how the realizes keyword is adopted to get the IS-A relationship.
Usually, the applies keyword is adopted with classes to come into the properties of an line. Interfaces can never be expanded by a class.
public interface Animal { } public class Mammal implements Animal { } public class Dog extends Mammal { }
Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal.
interface Animal{} class Mammal implements Animal{} public class Dog extends Mammal { public static void main(String args[]) { Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } }
This will produce the following result:
true true true
These associations are mostly anchored in the practice. This settles on whether a positive class HAS-A convinced thing. This relationship aids to lessen duplication of code as well as bugs.
Lets look into an example:
public class Vehicle{} public class Speed{} public class Van extends Vehicle { private Speed sp; }
This illustrates that class Van HAS-A Speed. By having a divide class for Speed, we never have to put the whole code that fits into speed within the Van class, which makes it probable to recycle the Speed class in numerous applications.
In Object-Oriented feature, the consumers never require to bother concerning which entity is doing the genuine work. To attain this, the Van class hides the completion features from the consumers of the Van class. So, essentially what occurs is the users would inquire the Van class to do a convinced exploit and the Van class resolve either do the job by itself or request one more class to achieve the action.
There are various types of inheritance as demonstrated below.
A extremely significant fact to memorize is that Java never support manifold inheritance. This signifies that a class never extends more than one class. Consequently following is illegal −
public class extends Animal, Mammal{}
However, a class can realize one or more borders, which has lend a hand Java dispose of the unfeasibility of multiple legacy.
Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.