Inheritance in Java with Example

In this chapter, we are going to learn examples and explanation of Inheritance in Java.

Inheritance is a process where one class can inherit the properties and functionalities of another class. This object-oriented approach is best for code reusability.

Hence, we don’t need to redeclare the common methods and variables in every other class.

What we can do we can simply put all our common code in a single class and other classes can extend that particular class. Moreover, we can add new methods and fields in the current class also.

This mechanism of reusing the code and functionality of another class is known as Inheritance.

Different Types of Inheritance in Java:

1. Single Inheritance in Java

In single inheritance, child class will extend only one parent class. Therefore, ClassA is extending ClassB.

Single Inheritance in Java

2. Multiple Inheritance in Java

In multiple inheritance, child class will extend more than one class. Also, Multiple Inheritance is not supported in Java.

Multiple Inheritance in Java

3. Multilevel Inheritance in Java

In multilevel inheritance, one class can extend from a derived class and next time derived class will become the parent class for a new class. Therefore, code defined in ClassC can be easily accessible to ClassA in a multilevel manner.

Here you can see ClassA extends ClassB where ClassB is a parent to Class A. In addition when ClassB extends ClassC, then ClassB will become the child to ClassC.

Multilevel inheritance in Java

4. Hierarchical Inheritance in Java

In hierarchical inheritance, one class is inherited by many other child class. Therefore, code will look like:

  • ClassA extends ClassD
  • ClassB extends ClassD
  • ClassC extends ClassD
Hierarchical Inheritance in Java

5. Hybrid Inheritance in Java

Hybrid Inheritance is a combination of single inheritance and multiple inheritance. Since Java doesn’t support multiple inheritance. Therefore, hybrid inheritance is also not possible.

Hybrid Inheritance in Java

Java Inheritance Example:

1. Single Inheritance Example

In this example, add() method is defined in ClassB but I am using it in ClassA.

class ClassA extends ClassB {
	public static void main(String args[]) {
		int a = add();
		System.out.println("Addition is " + a);
	}
}
public class ClassB{
	protected static int add() {
		int sum = 5 + 6;
		return sum;
	}
}

2. Multilevel Inheritance Example

In this example, the method defined in ClassC can be easily accessible in ClassA because of multilevel inheritance. Try practising this code in your system for better understanding.

class ClassA extends ClassB {
	public static void main(String args[]) {
		int a = add();
		int b = multiply();
		System.out.println("Addition is " + a);
		System.out.println("Multiplication is " + b);
	}
}
public class ClassB extends ClassC {

	protected static int add() {
		int sum = 5 + 6;
		return sum;
	}
}
public class ClassC {
	protected static int multiply() {
		int mul = 5 * 6;
		return mul;
	}
}

Why Multiple Inheritance is not supported in Java?

Java doesn’t support multiple inheritance in order to reduce complexity in code and to simply the Java programming language.

If both parent class will have a method with the same name then, in that case, the compiler will not be able to decide which method to invoke. Also, it can lead to the diamond problem.

class ClassB {
	void print() {
		System.out.println("Hello");
	}
}

class ClassC {
	void print() {
		System.out.println("World");
	}
}

class ClassA extends ClassB, ClassC { //suppose  

	public static void main(String args[]) {
		ClassA obj = new ClassA();
		obj.print(); // Now which print() method would be invoked?
	}
}

Happy Learning!