Skip to main content

Practical-28

Write a java program which implements Multiple Interfaces.

Introduction

Writing a java program which implements Multiple Interfaces is a way of enabling a class to implement different interfaces at the same time and defining multiple behavior for the same class. This is often needed when a class needs to fulfill more than one function, as only one parent is allowed for inheritance.

Code

interface Interface1 {
public void method1(String str);
}

interface Interface2 {
public void method2();
}

interface Interface3 {
public void method3();
}

class DemoClass implements Interface1, Interface2, Interface3 {
public void method1(String str) {
System.out.println("Implementation of method1:"+str);
}

public void method2() {
System.out.println("Implementation of method2");
}

public void method3() {
System.out.println("Implementation of method3");
}
}

public class Main {
public static void main(String args[]) {
DemoClass myObj = new DemoClass();
myObj.method1("multiple interfaces");
myObj.method2();
myObj.method3();
}
}

Output

Implementation of method1:multiple interfaces
Implementation of method2
Implementation of method3
Explanation

This code creates three interfaces and a class DemoClass which implements all the three interfaces. The main method creates an object of DemoClass class and then call all three methods of the respective interfaces.