Detailed Explanation of Inheritance in Java with example - JavaTechSeries


Introduction - 

Inheritance in java is the most important concept and pillar of OOPs(Object Oriented Programmings).
 
The process of acquiring properties i.e data members and functionalities i.e methods of one class to another class is known as Inheritance in Java

The main advantage of Inheritance is to provide code reusability. There are two class will be used in Inheritance below as follows
1. Child class - The class that extends the feature of another class is known as Child class, subclass, and derived class.
2. Parent class - The class that data members and methods are used (extended) by another class is known as a parent class, superclass, and Base class. 

Inheritance represents the IS-A relationship, also known as the parent-child relationship.

The extends keyword is used to get all features of the parent class. Here child class will use the extends keyword to extends from the parent class.

A real-Life example could be: son and father,  that is all properties of a father are inherited by his son.


Note: Parent class reference can be used to hold child class object but by using that reference we are not allowed to call child class-specific methods.

Let's understand by example.

Syntax: 

class Child-class name extends Parent-Class name
{

}

With Example: 

Dog is the child class and Animal is the parent class. Now the relationship between these two classes is Dog IS-A Animal.  That means Dog is a type of Animal but Animal is not a type of Dog.

class Animal
{
    public void eat()
   {
        System.out.println("I can eat: Parent class");
    }

}

class Dog extends
{
    public void bark()
    {
        System.out.println("I can bark: Child class");
    }
}

public class DemoCheck
{
    public static void main(String args[])
    {
        Dog dogObj=new Dog();
           dogObj.eat();
           dogObj.bark();
    }
}

Output:
I can eat: Parent class
I can bark: Child class

Eclipse Code:
In this example, we have a base class as Animal and a Subclass as Dog. As Dog is extends the eat functionalities of Animal class. So Dog class no need to explicitly write eat method. Dog class can acquire the eat method features by extending the class Animal using extends keyword.





Types of Inheritance in Java:
To Learn Inheritance in detail, you can refer to Types of Inheritance of Java.


1). Single Inheritance
This type of inheritance is referred to when a class extends only and another class. Class B extends Class A.

Single Inheritance
 2). Multilevel Inheritance
This is the type of Inheritance is referred to relationship between the child and parent class.
Multilevel Inheritance

3). Hierarchical Inheritance
This type of inheritance explains the relationship between the child and parent class. Where one class extends more than one class.

                
Hierarchical Inheritance

4). Multiple Inheritance
This type of inheritance explains the relationship between one class with a two-parent class. That means Class c will extends from class A and Class B.
Multiple Inheritance


5). Hybrid inheritance: Combination of more than one types of inheritance in a single program. For example, class A & B extends class C and another class D extends class A then this is a hybrid inheritance example because it is a combination of single and hierarchical inheritance. 

NOTE: Java does not support Multiple Inheritance as if parents have a method with the same name and same type signatures then it creates ambiguity when the subclass tries to access that method as it gets confused about which method to call.

That is all about a detailed explanation of Inheritance in Java and its types. Also, you can check out the recommended topics.


Recommended Topics

1. Core Java Tutorial

2.  OOPs Concepts in detail

3. Java Programming


Previous
Next Post »

1 comments:

Click here for comments
Anonymous
admin
3 September 2020 at 18:38 ×

Detailed explanation..
Very helpful..
Thanks keep posting.
Could u post something on Spring or spring boot as well

Reply
avatar