Implement Multiple Inheritance in Java

Exercise:

Write a Java Program to implement multiple inheritance.

Click Here to View the Solution!
interface Backend {

     // declare abstract class
     public void connectServer();
}
class Frontend {
        public void responsive(String str) {
        System.out.println(str + " can also be used as frontend language.");
    }
}
       // Java extends Frontend class
       // Java implements Backend interface
public class Java extends Frontend implements Backend {
       String language = "Java";

        //implement method of interface
        public void connectServer() {
        System.out.println(language + " can be used as backend language.");
    }
public static void main(String[] args) {
        Java java = new Java();
        java.connectServer();

        // call the inherited method of Frontend class
        java.responsive(java.language);
    }
}
Click Here to View the Output!
Java can be used as backend language.
Java can also be used as frontend language.
Click Here to View the Explanation!
  • This program is used to perform the implementation of multiple inheritance in Java using an Interface.
  • Initially, an interface Backened is created holding a method ConnectServer() in its body and a class Frontend is created that holds the void method responsive() in its body.
  • A subclass Java is then created that will extend the superclass Frontend and implement the interface Backend. This subclass will initialize a string variable language as language = Java and implement the method ConnectServer() of the interface in its body.
  • In the Main class , an object java of the Java class is created that will be used to call the method of the interface and the method of the superclass.
  • With this program, the Java subclass inherits the attributes of both the interface and superclass hence, satisfying the concept of multiple inheritance in Java.

%d bloggers like this: