Tech Peak » What are the 5 types of inheritance in Java?

What are the 5 types of inheritance in Java?

by akshaysharma12
Multi-level inheritance in java

Did you know that prior to its current name, Java was referred to as “Oak”? Well, we certainly are grateful they didn’t stick to its former title. In this article we are going to discuss 5 types of inheritance on java.

By “they” we are referring to the Sun Microsystems who have designed the ultimate object-oriented prodigy- “Java”. 

There are countless applications and web pages that have been designed till date using the Java programming language. But what really plays a key role here? 

It has only been possible due to the multiple inheritances in Java. They have collectively facilitated the designing and execution of web programs. 

In this blog, you will find an elaborate discussion on the 5 different types of inheritances available in Java. 

Also since we are on the topic of Java, it is only fair to discuss one of its most important concepts. 

By the end of this blog, we have briefly mentioned the subject of String palindrome in Java. 

What are the 5 types of inheritance in Java?

Inheritance in one of the most basic terms can be explained by the following example:

Have you taken notice of how children often inherit the qualities or traits of their parents? 

In the programming language of Java, when one class acquires the properties of the other, the process is referred to as Inheritance. 

Why is it useful you may ask? 

Since the classes are inhibiting the properties of their former class, we can utilize the similar methods and fields of the former class while running a program. 

The phenomenon of inheritance is arguably one of the most stand-out qualities of an object oriented programming language such as Java. 

Now, The Java programming language facilitates the process of inheritance in 6 different formats. They are as follows:

Single inheritance

This mechanism revolves around the creation of a superclass also commonly referred to as the parent class, using a subclass in Java. 

Let us refer to the following example for better understanding:

A———— B

A and B are two classes in Java. A class in Java is basically a cumulation of objects that exhibit similar qualities. 

Now, class A is extended towards class B. This means that the class B is inheriting qualities of class A which facilitates reusability for the programmer. 

This makes class A- The sub class and class B- The super class. Note that the sub class is extending or leading to the formation of the parent and not the other way around. 

Multiple inheritance

Staying true to its name, the phenomenon of multiple inheritance in Java is referred to the formation of multiple superclasses by the extension of various subclasses. 

Object oriented programming languages are characterised for facilitating multiple inheritances, except for Java. 

Java is the only OOP (object oriented programming) language which does not support the formation of multiple superclasses. 

The only reason why allowing multiple inheritances is not supported in Java is to make sure that there are no chances of ambiguity. 

This checks out the fact that the programming language is not perceived in more than one format or form. 

Multi-level inheritance

This phenomenon is basically a chain mechanism where one class presents itself as the base of inheritance for the preceding class and so forth. 

Check out the following example for better understanding:

A ————– B ———— C

A, B and C are three classes in Java which are being observed in the above example for extending their traits to the next or preceeding class. 

Hence, class A becomes the sub class for class B and class B becomes the sub class for class C. 

Hierarchical inheritance

Quite similar to the concept of binary trees, in Java the phenomenon of hierarchical inheritance facilitates the formation of multiple super classes using a single sub class or vice-versa. 

The hierarchical inheritance structure of Java can be observed in the form of a tree-like pattern. Here the primary subclass is linked with all the superclasses forming a tree mechanism. 

Consider the following example to grasp the mechanism:

So far in the former examples the class A was only extended to the class B and so forth. 

But in the case of hierarchical inheritance, the class A will extend further to classes B, C and D. 

Hybrid inheritance

In this phenomenon, the classes are represented in the formation of a circular pattern where every class is connected to one another. 

This type is the culmination of the single and multiple inheritances. This is why you may observe that the subclass is linked with each superclass forming a circular structure. 

Let us consider the same classes as mentioned in the former types in the context of hybrid inheritance:

This means that classes A, B, C and D are connected in a circular format to one another with the class A as the sub class for B and C. And, the classes B and C being the subclasses for D. 

Note that this type of inheritance is also not supported in Java. 

Those were 5 of the types of inheritance in-built in the Java programming language. Note that two of the inheritances i.e multiple inheritance and hierarchical inheritance are not supported by Java. 

Now, while we are on the subject, let briefly discuss one of the major concepts of this programming language i.e the String palindrome in Java. 

What is String palindrome in Java? 

Have you ever noticed an Ambigram? Remember how the letters of an ambigram read the same both front and backwards? 

This is exactly what encompasses the concept of string palindrome in Java. A string palindrome is derived after reversing a string in Java. 

After the output is derived, it is matched with the input statement to check whether the letters spelt backwards read the same. 

After the execution of the program, if the output statement exactly resembles the input statement, we can confirm that the given string is a palindrome. 

Approach: 1

The first approach for the string palindrome in Java problem is as follows.

  • The string is stored in str.
  • The for loop runs from the end to the beginning of the given string.
  • The charAt() method accesses each character of the given string.
  • Each character of the given string is accessed in reverse order and stored in reverseStr variable.
  • The toLowerCase() method converts both the string and the reversed string to lowercase. This is because Java is case sensitive language and ‘r’ and ‘R’ are two different values.
  • The equals() method is used to check if two strings, str and reverseStr are equal.
  • If the string str and reverseStr are equal, the string is a palindromic string.
  • If the string str and reverseStr are not equal, the string is a not palindromic string.

Approach: 2

The second approach for the string palindrome in Java problem is as follows.

  • The string is stored in str.
  • First, convert the string to lowercase. 
  • Take two pointers, “i” pointing to the start of the string and “j” pointing to the end of the string.
  • Keep incrementing the “i” pointer and decrementing “j” pointer while i < j, and at every step, check whether the characters at these pointers are the same or not. 
  • If the characters are equal, the string is a palindromic string.
  • If the characters are not equal, the string is not a palindromic string.

Winding Up

Isn’t it intriguing that the Java programming language allows you to shift the elements from one system to another? 

Similarly, you will also find the concept of multiple inheritance in Java fascinating as well. 

Also while you are at it, make sure to check out the concept of string palindrome in Java which we have briefly mentioned by the end of this blog. 

You may also like

Leave a Comment