Q:

Java program to implement multiple interfaces in the same class

belongs to collection: Java Interface Programs

0

Java program to implement multiple interfaces in the same class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create multiple interfaces. Each interface contains an abstract method. Then we will implement all interfaces in a class and defined the abstract methods.

Program/Source Code:

The source code to implement multiple interfaces in the same class is given below. The given program is compiled and executed successfully.

// Java program to implement multiple interfaces 
// in the same class

interface Inf1 {
  void sayHello1();
}

interface Inf2 {
  void sayHello2();
}

interface Inf3 {
  void sayHello3();
}

class Sample implements Inf1, Inf2, Inf3 {
  public void sayHello1() {
    System.out.println("Hello World1");
  }

  public void sayHello2() {
    System.out.println("Hello World2");
  }

  public void sayHello3() {
    System.out.println("Hello World3");
  }
}

public class Main {
  public static void main(String[] args) {
    Sample S = new Sample();

    S.sayHello1();
    S.sayHello2();
    S.sayHello3();
  }
}

Output:

Hello World1
Hello World2
Hello World3

Explanation:

In the above program, we created three interfaces Inf1Inf2Inf3. Each Interface contains an abstract method. Then we defined all abstract methods in the Sample class by implementing created interfaces using the "implements" keyword.

The Main class contains a method main(). The main() method is the entry point for the program, here we created the object of the Sample class. After that, we called all abstracted methods and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to implement the interface with multi... >>
<< Java program to implement the method of an interfa...