Inheritance in Java

In this article, we are going to study what exactly inheritance is in java and the types of inheritance in java. Along with that, we will study each case’s inheritance program in java to clearly understand its working and output.

What is Inheritance in Java?

Inheritance is one of the many features of Oriented Programming. With the help of inheritance, an object belonging to a class can inherit the functions and properties of another class. With the help of inheritance, the reusability of code is increased and code repetition can be avoided.

Inheritance in Java can be performed when we create a class, the extend keyword integrates all the properties and functions existing in the included class.

Terminologies to Know Before Learning Inheritance in Java

Class:
Class can be abstractly unit having particular properties and features and defined to create objects possessing the same properties and features.

Parent Class:
The Class whose properties and features are included in the newly created class or child class is known as the parent class. Parent Class must be defined already to be included in the child class. It can also be called a base class.

Child Class:
The class which gets the features such as properties and features from the parent class or base class is known as the child class. It is created with an extended keyword acquiring all the properties and features from the parent class. It is also known as a subclass, extended class or derived class.

Types of Inheritance in Java

Now that we have a clear picture of inheritance in our that how it works and what inheritance in java is based on, we will move on to study the types of inheritance in java and also various inheritance programs in java for each of the cases.

The fundamental idea of inheritance is the creation of a class with features of an already pre-existing class to increase reusability and avoid any form of code repetition or redundancy.

The four types of inheritance that exist in java are as follows:-

  1. Single Level Inheritance
  2. Multi-Level Inheritance
  3. Hybrid Inheritance
  4. Hierarchical Inheritance

Single Level Inheritance

This is the most simple form of inheritance in java. In Single Level Inheritance, the child class inherits the properties and features of the parent class directly.

This scenario can be justified as the direct transmission of properties and behaviour from the parent class to the child class. The single-level inheritance program in java will help understand the working with more clarity.

class Pets {

void eat()

{
System.out.println("Eat\n");
}
}

class Dog extends Pets {
void bark()

{
System.out.println("Bark\n");
}

}

class Check{

public static void main(String args[]){
Dog d=new Dog();

d.bark();

d.eat();

}
}

Output:
Bark
Eat

As it can be understood by the above Single Level Inheritance program in java, the Class Pet is the parent class while the class Dog is the child class as this real-world example shows how parent class and child class mechanisms work.

Multilevel inheritance in java

One of the types of inheritance in java where there are multiple classes involved in the transmission of properties and features. As the name suggests, there are many levels or perhaps multiple parties that are classes involved in sharing the class with child classes.

A class can derive properties from a parent class that has already derived properties from a different parent class thus, indicating multilevel inheritance from one class to another. Let’s look at the multilevel inheritance program in java for better understanding

of class Pets{

void eat(){
    System.out.println("eat");
}
}

class Dog extends Pets {

void bark(){
    System.out.println("bark");
}
}

class Puppy extends Dog{

void weep(){
    System.out.println("weep\n");
}
}

class Check{

public static void main(String args[]) {
Puppy d=new Puppy();

d.weep();
d.bark();
d.eat();

}
}

Output:
Weep

Bark
Eat

In the multilevel inheritance code in java, Pet can be assumed as the grandparent class that donated its properties to Dog, let’s assume that as our parent class and finally the properties that Dog had access to were donated to the child class in this context of multilevel inheritance.

Hierarchical Inheritance in Java

If more than one child class acquires the properties from the same parent class, it is said to be an instance of hierarchical inheritance. Let us understand this using the Hierarchical Inheritance program in Java

class Pets {

void eat() {
    System.out.println("eat");
}
}

class Dog extends Pets{

void bark(){
System.out.println("bark\n");
}
}

class Cat extends Pets{

void meow(){
    System.out.println("meow\n");
}
}

class Check{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

}
}

Output:
Meow
Eat

As the above Hierarchical based inheritance program in Java states that subclasses Cat and Dog were able to inherit the properties from the parent class, Pets.

Hybrid Inheritance in Java

Among the various types of Inheritance in Java can be a combination of multiple types of inheritance in Java as discussed in the above section.

Now we are done with all the types of inheritance in java, looking at working on each of the cases of inheritance.

Conclusion

We understood how inheritance works in object-oriented programming and studied all the terminologies used in inheritance, types of inheritance in java along with its practical use and code.

I hope you liked this article on Inheritance in Java. We hope to see you at PrepBytes soon.

FAQs related to Inheritance in Java

1. How an inheritance program in java is executed?
Inheritance programs are performed by creating a child class inheriting all the properties and features from the parent class. The child class is created using the extend keyword.

2. Is multiple inheritances possible in java?
Multiple inheritances are not supported in Java unlike other counterparts like C++ and other object-oriented programming languages.

3. Is hybrid inheritance related to other types of inheritance?
Hybrid Inheritance is a combination of more than one type of inheritance and is complex as compared to other types of inheritance.

Leave a Reply

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