Java.lang Package Classes

Java Class - isInterface() Method



The java.lang.Class.isInterface() method is used to determine if the specified Class object represents an interface type.

Syntax

public boolean isInterface()

Parameters

No parameter is required.

Return Value

Returns true if this object represents an interface; false otherwise.

Exception

NA.

Example:

In the example below, the java.lang.Class.isInterface() method is used to check if the given class object represents an interface type or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    //get a class object for this class
    MyClass x = new MyClass();
    Class xcls = x.getClass();

    //get a class object for Test
    Class ycls = Test.class;

    //check if x represents an interface
    Boolean retval1 = xcls.isInterface();
    System.out.println("Is this class an interface?: " + retval1);

    //check if x represents an interface
    Boolean retval2 = ycls.isInterface();
    System.out.println("Is Test an interface?: " + retval2);
  }
}

@interface Test {
  //code block
}

The output of the above code will be:

Is this class an interface?: false
Is Test an interface?: true

❮ Java.lang - Class