articles

Inheritance and its types

Anonymous User 1369 30-Aug-2018

INHERITANCE

The process of acquiring properties of a base class is known as inheritance where the base class is a class whose properties are inherited. It is a basic fundamental property of object-oriented programming.

Inheritance syntax:

Class subclass-Name extends superclass-Name {   } 

Types of Inheritance:

• Single-Level Inheritance

• Multi-level Inheritance

• Multiple Inheritance

• Multipath Inheritance

• Hierarchical Inheritance

• Hybrid Inheritance

Single-level Inheritance:

          In this inheritance single class is derived from the base class.

Inheritance and its types

Public class fruit { 
}
Public class apple extends fruit {
}

Multi-Level Inheritance:

               In this inheritance, more than one derived class is generated from another derived class.

                      Inheritance and its types

Public class fruit {
}
Public class apple extends fruit {
}
Public class banana extends fruit {
}

 Multiple Inheritance:

              In this inheritance, a derived class is generated from more than one base class.

                    Inheritance and its types

Public class mammal {
}
Public class wingedAnimal {
}
Public class Bat extends wingedAnimal, mammal{
}

Multipath Inheritance:

         In this inheritance, a derived class is created from another derived class and the same base class of other derived classes.

         Inheritance and its types

Public class grandParent {
}
Public class parent1 extends grandParent {
}
Public class parent2 extends grandParent {
}
Public class child extends parent2, grandParent, parent2 {
}

Hierarchical Inheritance:

          In this inheritance from a single base class, more than one derived class is generated.

        Inheritance and its types

Public class employee {
}
Public class permanentEmp extends employee{
}
Public class temporaryEmp extends employee{
}

Hybrid Inheritance:

          In this inheritance, it is a combination of more than one inheritance

              Inheritance and its types

Public class person {
}
Public student extends person {
}
Public gateScore {
}
Public class pg-student extends student, gateScore {
}

Advantages:

• It reduces redundancy.

• It increases readability.

• It provides code reusability.

Disadvantages:

• If there are any changes made in parent class then the child class will also get affected.

Why multiple inheritances are not supported in java?

      Due to the ambiguity problem, java does not support multiple inheritances.


The real-life example of inheritance in java:

    The real-life example of inheritance is the parent and its child.



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By