Q:

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

belongs to collection: Java Class and Object Programs

0

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

All Answers

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

Program/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 inf1inf2 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.

 

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

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to get the package name of a class... >>
<< Java program to get the name of the generic superc...