multiple inheritance – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:24:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg multiple inheritance – Programmerbay https://programmerbay.com 32 32 Multiple Inheritance in Java With Program Example https://programmerbay.com/multiple-inheritance-in-java-with-program-example/ https://programmerbay.com/multiple-inheritance-in-java-with-program-example/#respond Sat, 03 Feb 2024 16:27:51 +0000 https://programmerbay.com/?p=9046 In object-oriented programming (OOP), inheritance is a feature that allows a class or an interface to inherit attributes and behaviour from another class or interface. The class that is being inherited, is known as the parent class, while the class that inherits is termed as the child class.

In Java, inheritance can be implemented either through classes or interfaces. In the case of class, the parent class provides behavioural implementation (non-abstract method) that its child class acquires and reuses. But in the case of interfaces, the parent interface only supplies behavioural specification (abstract method) and the implementation is provided only when a class implements the interface.

Multiple Inheritance

Multiple Inheritance is a type of inheritance in which a class inherits more than one class. It may increase the complexity of a program as it can introduce deadly diamond or ambiguity problems. Apart from this, one also has to take care of special rules to prevent ambiguities, leading to extra efforts to deal with such cases.

multiple inheritance in Java

Therefore, unlike C++, Java doesn’t support multiple inheritance of implementation. As a result, a class cannot have more than one superclass or parent class. However, it does support this type of inheritance in the case of the interface. A basic construct, named interface, consists only of declaration, not implementation. Hence, an interface can inherit multiple interfaces.

multiple inheritance in java using interfaces

 

Syntax for multiple inheritance in Java :

interface A {
// members
}

interface B {
// members
}

interface C extends A, B{
// members
}

 

Ambiguity Problem or Diamond Problem in Java [Read more in brief : click here]

It is also known as the deadly diamond of death. This problem arises when a class acquires properties from multiple parent classes. For instance, suppose there are two classes, namely Person and Employee and both are having a common functionality getAge().

If a new class tries to inherit Programmer and Person at the same time, then it would automatically get getAge() method from both the parent classes. Further, the class would have two versions of getAge() with different implementations.

As a result, it would create ambiguity problem for the compiler, while identifying which version of getAge() to be selected – Person or Programmer.

This ambiguity or confusion is termed the diamond problem.

Therefore, Java doesn’t support multiple inheritance in classes.

Trying to Implement Multiple inheritance program in Java

As we know, multiple inheritance is not supported in the case of classes. It happens because two classes having the same method and different implementations can create ambiguity.

To check this on a programmatic level, we’ll be trying to implement the inheritance in form of a program.

Program:

class Employee {
    float salary;

// getting age of an employee
    int getAge(){
        return 21;
    }
}

class Person {
    String name;

    // getting age of a person
    int getAge(){
        return 23;
    }
}

// Trying to apply multiple inheritance by extending two class at time
class Programmer extends Employee,Person{

    public static void main(String[] args) {
        Programmer programmer = new Programmer();
        // Trying to get Age of a programmer
        System.out.println("Total salary for you programmer :: "+programmer.getAge());
    }
}

Error:

error: '{' expected
class Programmer extends Employee,Person{
                                 ^

Explanation:

  1. There are two classes named Person and Employee that have a common method getAge() in which each of them has their implementations
  2. When the Programmer class is trying to extend both the classes Person and Employee, a compile time exception is occurred

Multiple inheritance using interface in Java

Java doesn’t provide support for multiple inheritance of implementation ( non-abstract method or concrete class) but in the case of inheritance of interfaces or specifications (abstract methods), Java provides support for it.

It is because an interface can only provide only a declaration of a method, not implementation. As a result, when a class implements multiple interfaces and if a common method is available in those interfaces, it has to provide a single implementation which doesn’t create ambiguity.

An interface can have multiple parent interfaces at the same time. The functionality’s been demonstrated in the below example.

Java program to implement multiple inheritance using interface

Program:

interface Employee {
// getting age of an employee
    int getProgrammerAge();
}

interface Person {

    // getting age of a person
    int getProgrammerAge();
}
// Applying multiple inheritance by extending two interfaces at a time

interface Programmer extends Employee,Person{

    String getProgrammerName();
}

class Program implements Programmer{

    public static void main(String[] args) {
        Program code1 = new Program();
        // Trying to get Age of a programmer
        System.out.println("The code is written by :: "+code1.getProgrammerName()+" and age :: "+code1.getProgrammerAge());
    }

    @Override
    public int getProgrammerAge() {
        return 21;
    }

    // Overriding the method inherited from the interface
    @Override
    public String getProgrammerName() {
        return "Kui";
    }
}

Output:

The code is written by :: Kui and age :: 21

Explanation:

  1. In the above example, there are interfaces named Employee and Person.
  2. Both the interfaces is having abstract method getProgrammerAge(). Another interface Programmer is extending both of them, Employee and Person. And it also has a non-abstract method gerProgrammerName().
  3. As a result, the child interface has the access of getProgrammerAge() of both the parent interfaces.
  4. Now, the Program class has implemented the programmer interface. The class has provided implementation for both the methods gerProgrammerName() and getProgrammerAge().
  5. Since,  getProgrammerAge() was only a declaration, not implementation, it doesn’t matter, if two interfaces has the same method or not, at last they would be limited to declaration. The only part that differentiate them is implementation of the method, which is at the end provided by inheriting class.

The Solution of Diamond Problem

The default method that was introduced in Java 8, is the solution of the diamond problem or ambiguity problem. A default method is an implementation method within an interface. It is not mandatory for implementing classes to override it as its implementation is already provided by the interface.

It allows multiple inheritance to some extent. A default method with the same method name can be present in two or more interfaces. If a class implements them, then it would inherit properties of both.

In the case of default methods, Java defines a set of rules which needs to follow:
1) When a class overrides a default method of the implementing interface, then the class implementation would take the priority.

Example.

interface A {
    default void display() {
        System.out.println("I am a method from interface A");
    }
}

interface B {
    default void display() {
        System.out.println("I am a method from interface B");
    }

}

class C implements A,B{

    @Override
    public void display() {
        System.out.println("I am a method from class C");
    }

    public static void main(String[] args) {
        C obj = new C();
        obj.display();
    }
}

Output:

I am a method from class C

2) When a class doesn’t provide implementation, then an error would occur.

Example:

interface A {
    default void display() {
        System.out.println("I am a method from interface A");
    }
}

interface B {
    default void display() {
        System.out.println("I am a method from interface B");
    }

}

class C implements A,B{

    public static void main(String[] args) {
        C obj = new C();
        obj.display();
    }
}

Error:

error: types A and B are incompatible;
class C implements A,B{
^
  class C inherits unrelated defaults for display() from types A and B

3) If an interface is having a default method and is extended by another interface which consists same default method, then the most recent inheriting interface’s version in the hierarchy is considered.

Example.

interface A {
    default void display() {
        System.out.println("I am a method from interface A");
    }
}

interface B extends A {
    default void display() {
        System.out.println("I am a method from interface B");
    }

}

class C implements A,B{

    public static void main(String[] args) {
        C obj = new C();
        obj.display();
    }
}

Output:

I am a method from interface B

Java also allows the privilege to explicitly refer to a particular interface’s default implementation.

InterfaceName.super.methodName( )

 

]]>
https://programmerbay.com/multiple-inheritance-in-java-with-program-example/feed/ 0
Why Multiple Inheritance is Not Supported in Java https://programmerbay.com/why-multiple-inheritance-is-not-supported-in-java/ https://programmerbay.com/why-multiple-inheritance-is-not-supported-in-java/#respond Fri, 26 Aug 2022 07:34:33 +0000 https://programmerbay.com/?p=5824 In this article, we’ll be discussing what is use of inheritance and why multiple inheritance is not supported in Java .

One of the main features of object-oriented programming is Inheritance. In Java, Inheritance can be defined as a way by which a class acquires properties from another class.

A class which inherits properties from another class is known as subclass or child class, while the class which is inherited, is termed as superclass or parent class. extends keyword is used in order to inherit the properties of another class.

Syntax:

class SubClass extends ParentClass { 
// Attributes and behavior 
}

As and when, a class uses extends keyword, it can access and include all data members and member functions of the inherited class . However, a child class can never access and inherit the private members of parent class.

Why we use Inheritance in Java ?

Inheritance provides a number of features:

  •  Code Reusability:

Code reusability means an existing class can be reused in other class, specifically for similar type of classification.

  • Ease to add new feature:

One can provide new functionality to a new class by using the existing class without changing it.

  • Overriding

This provides the freedom of having methods with the same signature as in parent class but with meaningful implementation.

Why Multiple Inheritance is Not Supported in Java?

Multiple Inheritance is a type of inheritance in which a class extends two or more classes.
Unlike C++, Java doesn’t support multiple inheritance.

The reason because multiple inheritance raises ambiguity problem which creates the possibility of inheriting multiple copies or generating multiple paths of same data for child class.

In other words, a compiler can’t able to determine which member to choose if inheriting classes have common members. This problem is termed as Diamond Problem.

image 2020 12 27 141335
Figure: Diamond Problem in Inheritance

 

For example, suppose a class represents Person having getGender() method and this Person Class is inherited by Man and Woman Class. Since both have inherited getGender() method from Person Class.

However, if Man and Woman Class is further extended by another class named Programmer. As a result, the Programmer Class would get two copies of “getGender()” method which leads to ambiguity for compiler. Meaning, a compiler won’t be able resolve method call at runtime.

Program to show the concept of Inheritance

class A
{
int a;
A() // constructor
{
a=10;
}
void display() // method
{
System.out.println("I am here with "+ a);
}
}

class B extends A{ // inheriting
void show()
{
display();
}
}
public class Inheritanceuseing {
public static void main(String[] args) {
B obj = new B();
obj.show();
}

}

]]>
https://programmerbay.com/why-multiple-inheritance-is-not-supported-in-java/feed/ 0