×

Java Programs

Java Practice

Java program to get the list of implemented interfaces in a class

Java example to get the list of implemented interfaces in a class.
Submitted by Nidhi, on May 06, 2022

Problem statement

In this program, we will get the list of implemented interfaces in a class using the getInterfaces() method and print the result.

Source Code

The source code to get the list of implemented interfaces in a class is given below. The given program is compiled and executed successfully.

// Java program to get the list of implemented interfaces 
// in a class

interface inf1 {}

interface inf2 {}

class Sample implements inf1, inf2 {}

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls = Class.forName("Sample");
    Class cInf[] = cls.getInterfaces();

    System.out.println("Interfaces implemented by Sample class: ");
    for (Class inf: cInf)
      System.out.println(inf);
  }
}

Output

Interfaces implemented by Sample class: 
interface inf1
interface inf2

Explanation

In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. Here, we created two interfaces inf1, inf2 and implemented them into the Sample class.

In the main() method, we got the list of implemented interfaces in the Sample class using the getInterfaces() method and printed them.

Java Class and Object Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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