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 methodConnectServer()
in its body and a classFrontend
is created that holds the void methodresponsive()
in its body. - A subclass
Java
is then created that will extend thesuperclass Frontend
and implement the interfaceBackend
. This subclass will initialize a string variablelanguage
aslanguage = Java
and implement the methodConnectServer()
of the interface in its body. - In the
Main class
, an object java of theJava
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.