1.

Java Inheritance

Answer»

In Java, Inheritance is a feature that allows one object to inherit all of the characteristics and behaviours of its parent object. It's a crucial component of OOPs (Object Oriented programming systems). Inheritance in Java refers to the ability to build new classes that are based on existing ones. When you inherit from an existing class, you can use the parent class's methods and fields. You can also add additional methods and properties to your current class.



  • Subclasses/Child Classes: A subclass is a class that inherits from another. A derived class, extended class, or kid class is another name for it.


  • Superclasses: A superclass (sometimes known as a parent class) is the class from which a subclass derives its features. It's also known as a parent class or a base class.

Types of inheritance in Java:

1. Single Inheritance - Subclasses inherit the features of a single superclass via single inheritance. Class A acts as a base class for the derived class B in the figure below.

Example -

class Sample{
void fun(){
System.out.println("Inside Sample's fun method.");
}
}
class Test extends Sample{
void foo(){
System.out.println("Inside Test's foo method.");
}
}
class Main{
public static void main(String args[]){
Test t = new Test();
t.fun();
t.foo();
}
}

Output -

Inside Sample's fun method.
Inside Test's foo method.

Explanation - In the above code, the class Test inherits from the class Sample. We create an object of the Test class and call the methods defined in both classes.

2. Multilevel Inheritance - Multilevel Inheritance: In Multilevel Inheritance, a derived class inherits from a base class, and the derived class also serves as the base class for other classes. Class A serves as a base class for derived class B, which in turn serves as a base class for derived class C in the diagram below. In Java, a class cannot directly access the members of a grandparent.

Example -

class Sample{
void fun(){
System.out.println("Inside Sample's fun method.");
}
}
class Test extends Sample{
void foo(){
System.out.println("Inside Test's foo method.");
}
}
class Result extends Test{
void display(){
System.out.println("Inside display method of Result class.");
}
}

In the above code snippet, the class Test inherits from the class Sample and the class Result inherits from the class Test.

3. Hierarchical Inheritance - One class serves as a superclass (base class) for several subclasses in Hierarchical Inheritance. Class A acts as a base class for the derived classes B, C, and D in the diagram below.

Example - 

class Sample{
void fun(){
System.out.println("Inside Sample's fun method.");
}
}
class Test extends Sample{
void foo(){
System.out.println("Inside Test's foo method.");
}
}
class Result extends Sample{
void display(){
System.out.println("Inside display method of Result class.");
}
}
class Example extends Sample{
void display(){
System.out.println("Inside display method of Example class");
}
}

In the above code snippet, the classes Test, Result and Example inherit from the Sample class.

4. Multiple Inheritance - When a class inherits from more than one class, it is referred to as multiple inheritances. The below image shows class C inheriting from classes A and B.

Java does not support multiple inheritances as it can lead to ambiguity. We can implement the concept of multiple inheritances through the use of interfaces which we will discuss later in this article.

Hybrid Inheritance - Hybrid Inheritance is a blend of two or more of the inheritance kinds listed above. The below figure shows two classes B and C inheriting from class A and a class D inheriting from both classes B and C. 

Thus hybrid inheritance requires multiple inheritances and since Java does not support multiple inheritances, hybrid inheritance is not supported by Java.

Diamond Problem in Java:

Let us consider the previous example of hybrid inheritance to understand the diamond problem. Let us also consider the following code snippet:

class Sample{
void fun(){
System.out.println("Inside Sample's fun method.");
}
}
class Test extends Sample{
void foo(){
System.out.println("Inside Test's foo method.");
}
}
class Result extends Sample{
void foo(){
System.out.println("Inside display method of Result class.");
}
}
class Example extends Test and Result{
void display(){
System.out.println("Inside display method of Example class");
}
}
class Main{
public static void main(String args[]){
Example e = new Example();
e.foo(); // Ambiguity
}
}

In the above code, the classes Test and Result inherit from the class Sample and the class Example inherits from the classes Test and Result. Here, we have assumed for now that Java supports multiple inheritances.
The above code gives a compilation error. The reason is that on calling the foo() method, there is an ambiguity of which foo() method is being referred to. There are 2 foo() method definitions available: one in the Test class and the other in the Result class. This is known as the diamond problem.

It is because of this problem that Java does not support multiple inheritances of classes and hence all of this is avoided. 




Discussion

No Comment Found