What do you know about Inheritance in java?

Java Inheritance:

                  Acquiring features from the super class (parent class) to subclass(child class) in known as inheritance.
Inheritance follows IS-A relationship, it is also known as  parent to child relation.
By using of extends keyword we can inherit super class into subclass.

Derived class: Derived class is also known as child class (or) subclass.
Base class: In Java base class is also known as parent class (or) super class.

                In inheritance subclass has all properties  of super class &  subclass have extra properties.

Types of inheritance:

There are Five  types of inheritances in java.
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4. Hierarchical inheritance
5. Hybrid Inheritance

 1.Single inheritance:        
                                                When a class extends another  class is known as single inheritance. It containing single base class and single derived class in the example manager class is a base class(parent class) and Employee class is derived class(child class).
Example :

2. Multilevel inheritance:
                                In Multilevel inheritance  initially  consisting one base class(parent class) and one derived class(child class) and derived class(child class) act as a base class to another class.
Example:Multilevel  inheritance                     
Initially HOD class act as a parent class to student class.Principal class act as the parent class to HOD class and now HOD class becomes child class to Principal class.


3.Multiple inheritance:
                             Java doesn’t  support  Multiple inheritance, because of Diamond problem.         
 Multiple inheritance consisting of one parent class and two child classes.

4. Hierarchical inheritance:
                                In Hierarchical inheritance  one parent class(base class) and two many child classes(derived classes).
Example:
                Mobile subscribers there is postpaid subscribers and prepaid subscribers.
                                        


5. Hybrid Inheritance:
                                Hybrid Inheritance  is a combination of both multiple inheritance and single inheritance. Java doesn’t  support Hybrid Inheritance but by using of Interface we can achieve this.
Example:

In Hybrid Inheritance   class A acting as a base class for class B, class C(derived classes) and class B and class C acting as a base class(parent class) to class D(child class).
Syntax for inheritance:
                Class child class extends  parent class{
                //methods
                }
Advantages with inheritance:
1.Inheritance provides Code reusability.
2.By using inheritance we can achieve Run time polymorphism.
3. Method overriding.
Limitations:
1.Minimum two java classes are required.
2.Handling complexity.
3.Multiple inheritance doesn't  support java.                  
4. Hybrid Inheritance doesn't  support java but by using the interface we can achieve.
5.Java extends only one class.


Interview Questions:
1.Explain about Java inheritance?
2.Is Java support multiple inheritance?
3.Limitations of inheritance?
4.Explain about oops concepts?
5.What is the relationship inheritance follows?
6.How many class can extend at a time in a Java class?
7.How can we achieve Hybrid inheritance?
8.Write syntax of inheritance?
9.Why do we need inheritance?
10.Why Java doesn't support multiple inheritance?


EmoticonEmoticon