Java Inheritance

Java Inheritance, Java Object-Oriented Programming, Types of Inheritance in Java, single inheritance, multiple inheritances, and ‘extends’ keyword.

Java OOP – Inheritance: 

• It is a process of inheriting (reusing) class members (Fields/Variables and Methods) from one class to another.

• The class where the class members are getting inherited is called Parent/Super/Base Class.

• The class to which the class members are getting inherited is called Child/Sub/Derived Class.

• The inheritance between parent class and child class is achieved using the ‘extends‘ keyword.

Creating & Calling Java Static & Non-static class members

public class Class1 {
//Declare Static variables
static int a=10, b=20;

//Declare Non static/instance variables
int c=30, d=40;

//Create a static method with return a value
public static int addition() {
int result;
result=a+b;
return result;
}
//Create a Non static method with return a value
public int multiply() {
int result;
result=c*d;
return result;
}
public static void main(String[] args) {
//Call static class members
System.out.println(b);//20
int res= addition();
System.out.println(res);//30

//Call Non static class members
Class1 obj = new Class1();
System.out.println(obj.d);//40
int res2= obj.multiply();
System.out.println(res2);//1200
}
}

Reusing class members from one class to another without inheritance

public class Class2 {

public static void main(String[] args) {
//Call static class members
System.out.println(Class1.b);//20
int res= Class1.addition();
System.out.println(res);//30

//Call Non static class members
Class1 obj = new Class1();
System.out.println(obj.d);//40
int res2= obj.multiply();
System.out.println(res2);//1200
}
}

Reusing class members from one class to another with inheritance

public class Class2 extends Class1{
int x=100;
public int sub(int num1, int num2) {
return num1-num2;
}
public static void main(String[] args) {
//Call static class members
System.out.println(Class1.b);//20
int res= Class1.addition();
System.out.println(res);//30

//Call Non static class members
Class2 obj = new Class2();
System.out.println(obj.d);//40
int res2= obj.multiply();
System.out.println(res2);//1200

int res3= obj.sub(123, 34);
System.out.println(res3);
System.out.println(obj.x);//100
}
}


Types of Inheritance in Java

1. Single Inheritance
ClassB extends ClassA

2. Multi-Level Inheritance
ClassB extends ClassA
ClassC extends ClassB
.

3. Hierarchical Inheritance
ClassB extends ClassA
ClassC extends ClassA
ClassD extends ClassA
.
.

4. Hybrid Inheritance
ClassB extends ClassA
ClassC extends ClassA
ClassD extends ClassC
.

5. Multiple Inheritance (* Java doesn’t support)
ClassB extends ClassA
ClassB extends ClassZ
.

Reusing Class members from two different classes – Single Inheritance

Class1:

public class Class1 {
int a=10, b=20;

public int add() {
int result=a+b;
return result;
}
public static void main (String [] args) {
Class1 obj = new Class1();
System.out.println(obj.a);//10
int res= obj.add();
System.out.println(res);//30
}
}

Class2:

public class Class2 extends Class1{

public static void main(String[] args) {
Class2 obj = new Class2();
System.out.println(obj.b);
int res= obj.add();
System.out.println(res);//30

System.out.println(obj.add());//30
}
}

Multi-Level Inheritance

Class1:

public class Class1 {
int a=10, b=20;

public int add() {
int result=a+b;
return result;
}
public static void main (String [] args) {
Class1 obj = new Class1();
System.out.println(obj.a);//10
int res= obj.add();
System.out.println(res);//30
}
}

Class2:

public class Class2 extends Class1{
int x=100, y=200;

public int multiply() {
int result=x*y;
return result;
}
public static void main(String[] args) {
Class2 obj = new Class2();
System.out.println(obj.b);
int res= obj.add();
System.out.println(res);//30

System.out.println(obj.add());//30
}
}

Class3:

public class Class3 extends Class2{
int c=1, d=2;

public int sub() {
int result=c-d;
return result;
}
public static void main(String[] args) {
Class3 obj = new Class3();
System.out.println(obj.a);//10
System.out.println(obj.b);//20
System.out.println(obj.x);//100
System.out.println(obj.y);//200
System.out.println(obj.c);//1
System.out.println(obj.d);//2

int res= obj.add();
System.out.println(res);//30

res= obj.multiply();
System.out.println(res);//20000

res= obj.sub();
System.out.println(res);//-1
}
}

Inherit two classes from two different packages

Class1 in Package1

package package1;

public class Class1 {
public static int x=10, y=20;

public static int add() {
int result=x+y;
return result;
}

public static void main(String[] args) {
System.out.println(y);//20
int res= add();
System.out.println(res);//30

}
}

Class1 in Package2

package package2;

import package1.Class1;

public class Class2 extends Class1{

public static void main(String[] args) {
System.out.println(y);//20
int res= add();
System.out.println(res);//30

}
}

Inherit two classes from two different projects

Class1 in Project1:

package package1;

public class Class1 {
public static int x=10, y=20;

public static int add() {
int result=x+y;
return result;
}

public static void main(String[] args) {
System.out.println(y);//20
int res= add();
System.out.println(res);//30

}
}

Class2 in Project2:

package package2;

import package1.Class1;

public class Class2 extends Class1{

public static void main(String[] args) {
System.out.println(y);//20
int res= add();
System.out.println(res);//30

}
}


Java Tutorial

Java Videos

Follow me on social media:

2 thoughts on “Java Inheritance”

Comments are closed.