01/01/2020

Inheritance in Java :Studywow | Inheritance Types

Studywow: Read Inheritance topic of java and read more Java topics on studywow to click here.

Q1. What is inheritance? In Java multiple inheritance is possible or not?

Ans. Inheritance

1. Inheritance(in java) is an OOPs concept by which a class can acquire the characteristics of another class, i.e. it's a method of creating a new class based on already existing class.
2. The class from which its inheriting is called base class and the class which is inheriting is called sub-class.
3. For example:
class demo1
{
.............
.............
}
} class demo2 extends demo1
{
..............
..............
}
4. Java only allows a class to have one immediate base class,i.e. single class inheritance.
5. The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance.
It is true that Java does not support multiple inheritance because of some inconsistency that arises during multiple inheritance.
For example: Suppose consider a method add () which is in class Z. Suppose a programmer ABC inherited the class Z to class X and overrided the add(). So this class will have the new implementation of add (). Suppose a programmer DEF inherited the class Z to class Y and overrided the add (). So this class will have the new implementation of add ().
If multiple inheritance is permitted in Java, then if the new programmer inherited both the classes and he didn't done any overriding of method add () then if he calls the add (), the JVM will not know which method to call, i.e. either the method in class X or method in class Y.
Because of this inconsistencies, multiple inheritance is not permitted in Java. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface. In Java multiple inheritance can be achieved through use of interfaces by implementing more than one interfaces in a class.

Q2. Types of Inheritance ?

Ans. Below are the different types of inheritance which is supported by Java:
(a) Single Inheritance: Single inheritance is the simple inheritance of all. When a class extends another class(Only one class) then we call it as single inheritance. The below diagram represents the single inheritance in Java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only superclass.

(b) Multiple Inheritance: Multiple inheritance is nothing but one class extending more than one class. Multiple inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C#, etc. (C++ Supports Multiple Inheritance). As the child class has to manage the dependency of more than one parent class. But we can achieve multiple inheritance in Java using interfaces.

(c) Multilevel Inheritance: In multilevel inheritance, a derived class will be inheriting a parent plass and as well as the derived class act as the parent class to other class. As seen in the below diagram. Class B inherits the property of Class A and again Class B act as a parent for Class C. In short. Class A is parent for Class B and Class B is parent for Class C.

(d) Hierarchical Inheritance: In hierarchical inheritance, one parent class will be inherited by many sub classes. As per the below example, Class A will be inherited by Class B, Class C and Class D. Class A will be acting as a parent class for Class B, Class C and Class D.

(e) Hybrid Inheritance: Hybrid inheritance is the combination of both single and multiple inheritance. Again hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the hybrid inheritance will look like below. Here, Class A will be acting as the parent class for Class B and Class C & Class B and Class C will be acting as parent for Class D.

Q3. Multiple Inheritance in Java Interfaces?

Multiple inheritance is not supported in classes but it's supported in interfaces. A single interface can extend to multiple interfaces.
InterfaceA.java
package com.journaldev.inheritance;
public interface InterfaceA {
public void do Something ();
}
InterfaceB.java
package com.journaldev.inheritance;
public interface InterfaceB {
public void doSomething();
}
Both the interfaces are declaring same method, now we can have an interface extending both these interfaces like below:
Interfacec.java
package com.journaldev.inheritance;
public interface Interfacec extends InterfaceA, InterfaceB {
// same method is declared in InterfaceA and InterfaceB both
public void doSomething();
}
This is perfectly fine because the interfaces are only declaring the methods and the actual implementation will be done by concrete classes implementing the interfaces. So there is no possibility of any kind of ambiguity in multiple inheritance in java interfaces.


Studywow: Inheritence in Java and Read more topics of Java on studywow.com(click here).


Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!