×

Java Programs

Java Practice

Java program to implement multiple interfaces in the same class

Learn how to implement multiple interfaces in the same class in Java?
Submitted by Nidhi, on March 28, 2022

Problem statement

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.

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 Inf1, Inf2, Inf3. 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.

Java Interface Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.