dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
May 14, 2021

Using Java to Identify Class Objects as Type Interface

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
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date