Inheritance In Java
By AmarSivas | | Updated : 2021-12-04 | Viewed : 199 times

Table of Contents:
What Is Inheritance
Inheritance is one of the main principles of OOPs. Inheritance means acquiring the properties like attributes and methods. For example Fruit, and Berry. Here properties of Fruit will be acquired by another class called Berry. We will see this in later sections.
In Java, the class from which properties are inherited is known as the
Suppose consider the example
public class Fruit {
public String name = "Fruit";
public void eat(){
System.out.println("Fruit is an eatable item.");
}
}
public class BlueBerry extends Fruit {
//public String name = "BlueBerry";
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
}
So here Parent class is Fruit and the Child class is
public class MainClient {
public static void main(String[] args) {
BlueBerry fruit = new BlueBerry();
fruit.eat();
System.out.println("Inherited Field: "+ fruit.name);
fruit.getBlueBerryTaste();
}
}
Once you execute the above Main method then we can see the resulted output and can perceive that properties inherited.
Fruit is an eatable item.
Inherited Field: Fruit
BlueBerry's taste is sweet.
Why Do We Use Inheritance
Inheritance always deals with inheriting the properties which means code reusability. Common properties of Parent and Child can not be implemented in both classes as these properties will be inherited from Parent into Child class.
Types Of Inheritance In Java
Different types of inheritance in Java
-
Single Inheritance
-
Multiple Inheritance
-
Multi-Level Inheritance
-
Hierarchical Inheritance
-
Hybrid Inheritance
Single Inheritance
In
Consider
public class Fruit {
public String name = "Fruit";
public void eat(){
System.out.println("Fruit is an eatable item.");
}
}
public class BlueBerry extends Fruit {
//public String name = "BlueBerry";
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
}
When the
Multiple Inheritance
In Multiple Inheritance, one Subclass should inherit from multiple classes. Using classes
We write code for Multiple inheritance. Here we use
public interface Berry {
public void getFruitCategory();
}
public interface Fruit {
public void eat();
}
public class BlueBerry implements Fruit, Berry {
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
@Override
public void getFruitCategory() {
System.out.println("The fruit category is Berry");
}
@Override
public void eat() {
System.out.println("Fruit is an eatable item.");
}
}
You can see here multiple Parent properties is been inherited into Child classes. As Java does not support extends keyword for extending more than one class, we used here interfaces.
Multi-Level Inheritance
In
Now we write code for multi-level inheritance.
public class Fruit {
public void eat() {
System.out.println("Fruit is an eatable item.");
}
}
public class Berry extends Fruit {
public void getFruitCategory() {
System.out.println("The fruit category is Berry");
}
}
public class BlueBerry extends Fruit {
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
}
Notice here Berry class is Child class to Fruit at same it is Parent to
Hierarchical Inheritance
In
public class Fruit {
public void eat() {
System.out.println("Fruit is an eatable item.");
}
}
public class Apple extends Fruit {
public void getAppleTaste() {
System.out.println("Apple's taste is sweet.");
}
}
public class BlueBerry extends Fruit {
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
}
Hybrid Inheritance
Hybrid inheritance is a combination of two or more. One of the examples is Hybrid is two classes are inherited from one parent and these two classes again will become Parent classes to another Child class. We will get a clear idea if we have a nice example as below.
public interface Berry {
void getFruitCategory();
}
public interface BlackBerry extends Berry {
void getBlackBerryTaste();
}
public interface BlueBerry extends Berry {
void getBlueBerryTaste();
}
public class HybridBerry implements BlackBerry, BlueBerry {
@Override
public void getFruitCategory() {
System.out.println("The fruit category is Berry");
}
@Override
public void getBlueBerryTaste() {
System.out.println("BlueBerry's taste is sweet.");
}
@Override
public void getBlackBerryTaste() {
System.out.println("BlackBerry's taste is sweet.");
}
}
See here HybridBerry implemented both Berry Parent classes. So here inherited properties are
For code repo for Inheritance In Java, you can get the entire code Java-Inheritance-Example-App