Java Inheritance - W3school | Tutorialspoint

Java Inheritance

Inheritance can be characterized as the procedure where one class gets the properties (strategies and fields) of another. With the utilization of legacy the data is made reasonable in a various leveled arrange.

The class which acquires the properties of other is known as subclass (determined class, tyke class) and the class whose properties are acquired is known as superclass (parents, base class)

Extends Keyword:

Extend is a kind of keyword that is used to inherit the class properties. Following are the syntax of the keyword.

Syntax

class Super {
…..
…..
}
class Sub extends Super {
…..
…..
}

Sample code:

Next is an illustration showing Java Inheritance. In this illustration you will be understand and observe the two classes namely My_calculation and calculation.
By making use of extend keyword, My_calculation inherits this method subtraction() and addition() of the calculation class.
Now you have to copy and paste the next program in the file and save with My_calculation. Java

Example

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 accomplish the above code as represented below.

javac                                   My_Calculation.java    
java                                    My_Calculation         

After executing the program, it will produce the following result −

javac                              My_Calculation.java        
java                               My_Calculation             

After performing the program, it will produce the following result −

Output:

javac                              My_Calculation.java        
java                               My_Calculation             

After executing the program, it will produce the following result −

In this program, when an object created to My-calculation, a copy of contents of the class is made within it. This is why making use of subject class is vital to access the members of superclass.

daha

uperclass reference variable hold the subclass object, but using that variable will make you only access to only members of the superclass, so to access members of both classes it is suggested to always make reference variable to the subclass.

If you choose the above program, then you can instantiate the class as given below. But then use of superclass reference variable ( cal in this case) you cannot call the method multiplication(), which related to the subclass My_Calculation.

Calculation cal = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);

Note:

Subclass inherits all the members) methods, fields, as well as nested classes from its superclass. Remember, constructors are not members, which means they are not inherited by subclasses, however the constructor of the superclass will invoked from the subclass.