Inheritance in-depth with Java programming examples

  • Posted on
  • Posted in Java

Introduction

Inheritance is one of the main concepts of (OOP) Object-oriented programming. If you haven’t read my post yet about OOP you can visit the link below:

Inheritance is a mechanism that a particular class or object acquires the properties and functions of the other objects.

It is like creating a strong relationship between objects. In a real-life example, every child has a parent. Therefore, in OOP, the child class inherits some behaviors and look of its parent class. Including the color of the eyes, hair, or the way the parent walks or eat.

Additionally, Inheritance enables the reusability of the program. For example, you have an existing class and then you added a new one. And the new class is strongly related to the existing class. Therefore, you can extend the existing class to reuse its methods or functions in the new class.

Two terminologies are commonly used in inheritance, parent class, and subclass:

And the keyword used in Java programming language is extends:

public class Child extends Parent{
 ...
}

And also there are different types of inheritance:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

Single inheritance is when the subclass acquires properties and functions from one parent class. Let’s consider the diagram below as an example:

And also let’s take a look at how it works in Java:

public class Class1 {
	void greeting_class1() {
		System.out.println("Hello world from Class1");
	}
}
public class Class2 extends Class1 {
	void greeting_class2() {
		System.out.println("Greetings from Class2");
	}
}

As you can see, the Class1 has method greeting_class1() and the Class2 has a method greeting_class2(). And then the Class1 extends Class2, therefore, we can now use the method of the Class1 since it is inherited to Class2. And it is work when we created an instance from Class2:

public class SingleInheritance {
	public static void main(String[] args) {
		Class2 class2 = new Class2();
		class2.greeting_class1();
		class2.greeting_class2();
	}
}

Output:
Hello world from Class1
Greetings from Class2

Multilevel Inheritance

In multilevel inheritance, we can make the subclass as also as parent class. Meaning, we can inherit subclass to the another sub class. The diagram below defines multilevel inheritance:

In java, it will look like the program below:

public class Class1 {
	void greeting_class1() {
		System.out.println("Hello world from Class1");
	}
}
public class Class2 extends Class1 {
	void greeting_class2() {
		System.out.println("Greetings from Class2");
	}
}
public class Class3 extends Class2 {
	void greeting_class3() {
		System.out.println("Hi there! from Class3");
	}
}
public class MultilevelInheritance {
	public static void main(String[] args) {
		Class2 class2= new Class2();
		class2.greeting_class1();
		class2.greeting_class2();
		System.out.println("---------------------");
		Class3 class3 = new Class3();
		class3.greeting_class1();
		class3.greeting_class2();
		class3.greeting_class3();
	}
}

Output:
Hello world from Class1
Greetings from Class2
————————————-
Hello world from Class1
Greetings from Class2
Hi there! from Class3

Multiple Inheritance

In multiple inheritances, one class can extend one or more parent classes. However, Java doesn’t support this kind of inheritance because of the diamond problem. In the next section, we will talk about that problem.

The diagram below defines multiple inheritances:

Hierarchical Inheritance

Hierarchical inheritance is the opposite of multiple inheritances, one class can be inherited by one or more subclasses.

The diagram below defines Hierarchical inheritances:

Let’s take another coding example to see how is it works:

public static class Class1 {
	void greeting_class1() {
		System.out.println("Hello world from Class1");
	}
}
public static class Class2 extends Class1 {
	void greeting_class2() {
		System.out.println("Greetings from Class2");
	}
}
public static class Class3 extends Class1 {
	void greeting_class3() {
		System.out.println("Hi there! from Class3");
	}
}
public static class Class4 extends Class1 {
	void greeting_class4() {
		System.out.println("Hello there! from Class4");
	}
}

As you can see, the Class2, Class3, and Class4 are extended the Class1. And when we invoke the methods from subclasses, the result will be the same as below:

public class HierarchicalInheritance {
	public static void main(String[] args) {
		Class2 class2= new Class2();
		class2.greeting_class1();
		class2.greeting_class2();
		System.out.println("---------------------");
		Class3 class3 = new Class3();
		class3.greeting_class1();
		class3.greeting_class3();
		System.out.println("---------------------");
		Class4 class4 = new Class4();
		class4.greeting_class1();
		class4.greeting_class4();
	}
}
Output:
Hello world from Class1
Greetings from Class2
---------------------
Hello world from Class1
Hi there! from Class3
---------------------
Hello world from Class1
Hello there! from Class4

Hybrid Inheritance

Hybrid Inheritance is a combination of multiple inheritance and multilevel inheritance, to be clear how it’s look like, let’s see the example diagram below:

Note: The Hybrid Inheritance and Multiple Inheritance are both not supported in Java programming. And like I have mentioned in the Multiple inheritance section, it is because of the diamond problem in java.

The reason why the problem calls a diamond because of the relationship of the classes that look like a diamond. Therefore, the Hybrid Inheritance can turn into a diamond relationship like and the same as with Multiple Inheritance.

Furthermore, the diamond problem occurs when two-parent classes have the same methods. Meaning, they have the same functionally and they can cause an anonymous problem. Therefore, the subclass doesn’t know which method will use and acquires when both are extended.

Let’s try it in java and see how and when the error occur:

public class Class1 {
	void greeting_class1() {
		System.out.println("Hello world from Class1");
	}
}
public class Class2 extends Class1 {
	void greeting_class2() {
		System.out.println("Greetings from Class2");
	}
}
public class Class3 extends Class1 {
	void greeting_class3() {
		System.out.println("Hi there! from Class3");
	}
}
public class Class4 extends Class2, Class3 {
	void greeting_class4() {
		System.out.println("Hello there! from Class4");
	}
}

And when you try to run a test with this kind of inheritance you will receive an exception errors:

public class HybridInheritance {
	public static void main(String[] args) {
		Class2 class2= new Class2();
		class2.greeting_class1();
		class2.greeting_class2();
		System.out.println("---------------------");
		Class3 class3 = new Class3();
		class3.greeting_class1();
		class3.greeting_class3();
		System.out.println("---------------------");
		Class4 class4 = new Class4();
		class4.greeting_class1();
		class4.greeting_class2();
		class4.greeting_class4();
	}
}
Output:
Hello world from Class1
Greetings from Class2
---------------------
Hello world from Class1
Hi there! from Class3
---------------------
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	Syntax error on token "extends", delete this token
	Syntax error, insert "ClassBody" to complete CompilationUnit
	Syntax error, insert "}" to complete Block

	at Class4.<init>(Class4.java:2)
	at HybridInheritance.main(HybridInheritance.java:12)

Conclusion

In the next tutorial, we will learn about the other OOP concepts, including Polymorphism, Encapsulation, and abstraction.

If you want to now more about OOP you can visit the link below:

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

BCF Theme By aThemeArt - Proudly powered by WordPress.
BACK TO TOP