Java and multiple inheritance


In Java, we use inheritance to allow the creation of a hierarchical classification of classes and objects. As the name suggests, inheritance is the ability of a class to inherit members of another class. The class whose properties are inherited is called a superclass whereas the class that inherits a superclass is called a subclass. We use the extends keyword to inherit the class. There are several types of inheritance in Java such as single and multilevel. In this article, we will specifically explore the multiple inheritance.

Multiple Inheritance in Java

In the terminology of object-oriented programming, multiple inheritance is the capability of inheriting multiple superclasses by a single subclass. Unlike other object-oriented programming languages, Java does not support multiple inheritance of classes rather it allows the multiple inheritance of Interfaces.

Visual Representation of multiple inheritance −

Why Java does not support multiple inheritance of classes?

In one line, the answer to this question is to prevent ambiguity. In the above figure, we can see that Class C extends both Class A and Class B. Suppose both classes have the same method display(). In this case, the Java compiler won't be able to decide which display method it should call. Hence, to prevent this kind of ambiguity Java restricts the multiple inheritance of classes. Example of one such ambiguity is the Diamond Problem.

Example 

The following example illustrates what would be the result if one tries multiple inheritance of classes.

class Event { // first superclass 
   public void start() {
      System.out.println("Start Event");
   }
}
class Sports { // 2nd superclass 
   public void play() {
      System.out.println("Play Sports.");
   }
}
// subclass extending both superclass
class Hockey extends Sports, Event {
   public void show() {
      System.out.println("Show Hockey.");
   }
}
public class Tester{
   public static void main(String[] args) {
      // creating instance of subclass
      Hockey hockey = new Hockey();
      // calling the method of subclass
      hockey.show(); 
   }
}

Output

Tester.java:12: error: '{' expected
class Hockey extends Sports, Event {
                           ^
1 error

The error shows that even with distinct methods multiple inheritance of classes is not supported in Java.

Why Java supports multiple inheritance of Interfaces?

In Java, interfaces serve two purposes pure abstraction and multiple inheritance. Generally, an interface consists of abstract methods and variables that define the behavior which a class can implement. Here, abstract method is a method without any implementation or body and is declared using the abstract keyword.

Due to this feature of abstract methods, Java allows the multiple inheritance of interfaces. The abstract method does not contain any definition and if we want to use it, we need to define it in subclass. In this way, only a single definition exists of that method and we can easily implement multiple inheritance.

To create an interface, we use the keyword 'interface' and to access its members within a class, we need to use the 'implements' keyword while defining that class.

Syntax

interface nameOfInterface {
   method1();
   method2();
}

Example 

The following example illustrates the practical implementation of multiple inheritance using interfaces.

interface Event { // interface 1
   public void start();
}
interface Sports { // interface 2
   public void play();
}
// another interface extending 1st and 2nd interface
interface Hockey extends Sports, Event{
   public void show();
}
public class Tester{
   public static void main(String[] args){
      // creating instance of hockey
      Hockey hockey = new Hockey() {
         // implementing the methods of interfaces
         public void start() {
            System.out.println("Start Event");
         }
         public void play() {
            System.out.println("Play Sports");
         }
         public void show() {
            System.out.println("Show Hockey");
         }
      };
      // calling the methods using instance
      hockey.start();
      hockey.play();
      hockey.show();
   }   
}

Output

Start Event
Play Sports
Show Hockey

Example 

In this example, we will declare two interfaces having methods with the same name but different signatures and then try to implement multiple inheritance.

interface Message1 { // interface 1
   public void method();
}
interface Message2 { // interface 2
   public void method(int id);
}
public class IntrfExample3 implements Message1, Message2 {
   // using the method here with different definition
   public void method() {
      System.out.println("Tutorialspoint");
   }
   public void method(int id) {
      System.out.println("ID: " + id);
   }
   public static void main(String []args){
      // object creation
      IntrfExample3 exp = new IntrfExample3();
      // method calling
      exp.method(); 
      exp.method(125);
   }
}

Output

Tutorialspoint
ID: 125

Conclusion

We started this article by defining the inheritance and in the next section, we discussed the multiple inheritance which is a type of inheritance where one subclass extends the properties of two or more than two superclasses. In general, Java does not support multiple inheritance of classes, but we can use interfaces instead of classes for the same purpose.

Updated on: 10-Aug-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements