dcsimg
 

Using Java to Identify Class Objects as Type Interface

Friday May 14th 2021 by MS Sridhar
Article Image

Learn how to use Java to identify if a class object is of type interface in this developer tutorial. Read more.

In this brief Java tutorial, we are going to look at how to identify if a particular class object is of the type interface. Observe the code snippet below:

*/

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class IsInterface 
{

    public static void main(String[] args)
	{
		IsInterface isInterface = new IsInterface();
		isInterface.proceed();
	}

	private void proceed()
	{
		String classToBeCheckedStr = "InterfaceToBeChecked";
		try
		{  
			Class classToBeChecked = Class.forName(classToBeCheckedStr);  
			System.out.println(classToBeCheckedStr + " is an interface: " + classToBeChecked.isInterface());  
		}
		catch(ClassNotFoundException cnfe)
		{
			System.out.println(cnfe);
		}  
	}
}

interface InterfaceToBeChecked
{
	
}  

/*

Output:

$java IsInterface
InterfaceToBeChecked is an interface: true
Home
Mobile Site | Full Site