5 Different Types of Inheritance in Java With Example. The Java development services have been progressing for many years and we can definitely see a huge rise in the future. There is plenty of Java uses in your application; among them, inheritance is a process in which one object gains all the properties and behaviours of a parent object. Types of Inheritance in Java and the Applications of Inheritance in Java Language.
We Are Going to Discuss Each and Single Detail with the Help of Syntax, Examples, and Images. Inheritance is considered a crucial part of the Object Oriented programming system. You may think about the application of inheritance in Java. Not to worry as we have got a quick summary of the Java inheritance and how to utilize it in your next project.
Things to Remember: Parent or Superclass for all classes are Object Class that is present in java.lang Package.
The main idea behind Types of Inheritance in Java is that you are allowed to create new classes upon the existing classes. By reusing the methods or fields of the parent class, you can inherit from an existing class and also add new methods or fields in your current class. Generally, the inheritance is known as a parent-child relationship that is represented by the IS-A relationship. There are duly two main reasons why you should opt for Types of Inheritance in Java.
Let us look at basic Java Inheritance.
Syntax:
class Subclass-name extends Superclass-name
{
// methods and fields
}
The Java inheritance also consists of a few terminologies which are mostly used while inheriting the classes. Go through these given terms to understand the concept in a better way.
Class -A group of objects that has common properties is defined as a class. Objects are created from a class.
Child Class -A class which inherits the other class. Also known as a subclass, derived class or extended class.
Parent Class -A class from where a subclass inherits the features. Also known as the superclass or base class.
Reusability -It is a mechanism that allows you to reuse the fields and methods of the existing class when creating a new class. You can also use the same fields and methods that are already defined in the previous class.
Now, it's time to dive into different types of Java inheritance along with its applications.
It is one of the easy inheritances to understand and implement as well. When a parent class gets extended by a child class then we call it a single inheritance. It is direct to direct connection defined as in the figure. Single Inheritance is one of the Different Types of Inheritance in Java.
A mechanism in an object-oriented language where you can inherit from a derived class thereby making this derived class the parent class for the new class is defined as a multilevel inheritance. Hence, in Java, a class cannot directly access the members of the grandparents. Multilevel Types of Inheritance in Java.
When one class is inherited by many subclasses, it is said to have a hierarchical inheritance. A better pictorial representation helps you to understand better. Hierarchical Types of Inheritance in Java.
Hybrid inheritance is a combination of single and multiple inheritances as depicted in the below-given figure. It can be achieved in the same way as the multiple inheritances in java. With the help of interfaces, you can have multiple as well as hybrid Different Types of Inheritance in Java With Example.
When a child class inherits or extends more than one parent class, it is said to have multiple inheritances. There is an issue when it comes to multiple Inheritance in Java as the derived class will have to manage the dependency on two base classes. It is mandatory to know that multiple inheritances are used very rarely in software projects as they can often lead to problems in the hierarchy.
Things to Remember: Parent or Superclass for all classes are Object Class that is present in java.lang Package.
The main idea behind Types of Inheritance in Java is that you are allowed to create new classes upon the existing classes. By reusing the methods or fields of the parent class, you can inherit from an existing class and also add new methods or fields in your current class. Generally, the inheritance is known as a parent-child relationship that is represented by the IS-A relationship. There are duly two main reasons why you should opt for Types of Inheritance in Java.
- Code Reusability
- Method Overriding to achieve runtime polymorphism
Let us look at basic Java Inheritance.
Syntax:
class Subclass-name extends Superclass-name
{
// methods and fields
}
The Java inheritance also consists of a few terminologies which are mostly used while inheriting the classes. Go through these given terms to understand the concept in a better way.
Class -A group of objects that has common properties is defined as a class. Objects are created from a class.
Child Class -A class which inherits the other class. Also known as a subclass, derived class or extended class.
Parent Class -A class from where a subclass inherits the features. Also known as the superclass or base class.
Reusability -It is a mechanism that allows you to reuse the fields and methods of the existing class when creating a new class. You can also use the same fields and methods that are already defined in the previous class.
Now, it's time to dive into different types of Java inheritance along with its applications.
5 Different Types of Inheritance in Java With Example
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
- Multiple Inheritance
Note: Java does not support Hybrid and Multiple Inheritance at the class level because of the ambiguity problem.
1. Single Inheritance in Java
It is one of the easy inheritances to understand and implement as well. When a parent class gets extended by a child class then we call it a single inheritance. It is direct to direct connection defined as in the figure. Single Inheritance is one of the Different Types of Inheritance in Java.
Example of Single Inheritance in Java
Class A {
public void method A() {
System.out.println("Parent class");
}
}
Class B extends A {
public void method B() {
System.out.println("Child class");
}
public static void main(String args[]) {
B obj = new B();
obj.methodA(); //calling super class
obj.methodB(); //calling sub class
}
}
2. Multilevel Inheritance in Java
A mechanism in an object-oriented language where you can inherit from a derived class thereby making this derived class the parent class for the new class is defined as a multilevel inheritance. Hence, in Java, a class cannot directly access the members of the grandparents. Multilevel Types of Inheritance in Java.
Example of Multilevel Inheritance in Java
Class X {
public void method X() {
System.out.println("Class X method");
}
}
Class Y extends X {
public void method Y() {
System.out.println("class Y method");
}
}
Class Z extends Y {
public void methodZ() {
System.out.println("class Z method");
}
public static void main(String args[]) {
Z obj = new Z();
obj.methodX(); //calling grand parent class
obj.methodY(); //calling base class
obj.methodZ(); //calling local method
}
}
3. Hierarchical Inheritance in Java
When one class is inherited by many subclasses, it is said to have a hierarchical inheritance. A better pictorial representation helps you to understand better. Hierarchical Types of Inheritance in Java.
Example of Hierarchical Inheritance in Java
class A {
public void method A() {
System.out.println("method of Class A");
}
}
class B extends A {
public void method B() {
System.out.println("method of Class B");
}
}
class C extends A {
public void method C() {
System.out.println("method of Class C");
}
}
class D extends A {
public void method D() {
System.out.println("method of Class D");
}
}
class JavaExample {
public static void main(String args[]) {
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
4. Hybrid Inheritance in Java
Hybrid inheritance is a combination of single and multiple inheritances as depicted in the below-given figure. It can be achieved in the same way as the multiple inheritances in java. With the help of interfaces, you can have multiple as well as hybrid Different Types of Inheritance in Java With Example.
Example of Hybrid Inheritance in Java
class C {
public void display() {
System.out.println("C");
}
}
class A extends C {
public void display() {
System.out.println("A");
}
}
class B extends C {
public void display() {
System.out.println("B");
}
}
class D extends A {
public void display() {
System.out.println("D");
}
public static void main(String args[]) {
D obj = new D();
obj.display();
}
}
5. Multiple Inheritance in Java
When a child class inherits or extends more than one parent class, it is said to have multiple inheritances. There is an issue when it comes to multiple Inheritance in Java as the derived class will have to manage the dependency on two base classes. It is mandatory to know that multiple inheritances are used very rarely in software projects as they can often lead to problems in the hierarchy.
This might end up in unwanted complexity when further you want to extend the class. All of the new object-oriented languages like Small Talk, Java, and C# do not support multiple inheritances while it is fully supported in C++.
Ava Barker working as a Technology Consultant at Tatvasoft UK which provides java development services in London. Coming from a technology background she likes to share her insights about development, design and more. She has also published her author bylines in many different publications online.
Author Bio:
Ava Barker working as a Technology Consultant at Tatvasoft UK which provides java development services in London. Coming from a technology background she likes to share her insights about development, design and more. She has also published her author bylines in many different publications online.
0 Comments: