ADVERTISEMENT
ADVERTISEMENT

Java program to check whether the specified Class object represents an interface type or not

Java example to check whether the specified Class object represents an interface type or not.
Submitted by Nidhi, on April 29, 2022

Problem Solution:

In this program, we will check whether an object represents an interface type or not using the isInterface() method.

Program/Source Code:

The source code to check whether the specified Class object represents an interface type or not is given below. The given program is compiled and executed successfully.

// Java program to check the specified Class object 
// represents an interface type

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls1 = Class.forName("Main");
    Class cls2 = Class.forName("java.lang.Runnable");

    boolean res1 = cls1.isInterface();
    boolean res2 = cls2.isInterface();

    System.out.println("Is Main an interface : " + res1);
    System.out.println("Is java.lang.Runnable an interface : " + res2);
  }
}

Output:

Is Main an interface : false
Is java.lang.Runnable an interface : true

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 checked whether an object represents an interface type or not using the isInterface() method. The isInterface() method returns true if the specified object represents an interface otherwise it returns false.

Java Class and Object Programs »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.