Pages

Determine what package from a class object ???

Source code below will print name for package that belong to it's own class object. It should print null because class object that create from java file below not belong to any package.

************************************************************
COMPLETE SOURCE CODE FOR : DeterminePackageForClassObject.java
************************************************************


public class DeterminePackageForClassObject
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("DeterminePackageForClassObject");
System.out.println("PACKAGE FOR THIS CLASS OBJECT IS :"+temp.getPackage());
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************

Get all interface from a class object

This program will print name of interface that implemented by this class

**********************************************************
COMPLETE SOURCE CODE FOR : ListingAllInterfaceFromClassObject.java
**********************************************************


import java.awt.event.ActionListener;//INTERFACE 1
//Runnable-INTERFACE 2
import java.awt.event.ActionEvent;

public class ListingAllInterfaceFromClassObject implements ActionListener,Runnable
{
public void actionPerformed(ActionEvent evt)
{
//NO IMPLEMENTATION
}

public void run()
{
//NO IMPLEMENTATION
}

public static void main(String[]args)
{
try
{
Class temp=Class.forName("ListingAllInterfaceFromClassObject");
Class[]allInterface=temp.getInterfaces();
int allInterfaceLength=allInterface.length;
int temp2=0;
while(temp2!=allInterfaceLength)
{
System.out.println("INTERFACE "+(temp2+1)+" :"+(allInterface[temp2].getSimpleName()));
temp2=temp2+1;
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Get superclass for an object class ???

Source code below will get superclass for it's own object class. It will print name for it's superclass.

*****************************************************************
COMPLETE SOURCE CODE FOR : GettingSuperclass.java
*****************************************************************


public class GettingSuperclass
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("GettingSuperclass");
Class thisIsSuper=temp.getSuperclass();

System.out.println("Superclass for this class is : "+thisIsSuper.getSimpleName());
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Determine what is represent by a class object, whether an interface or a class ???

This tutorial will use CreateInterface.java to determine whether class object from it, is an interface or a class. You can get it at Java Interface ( Part 1). Before you compile and execute source code below, you must make sure CreateInterface.class is locate same location with this java file.

***********************************************************
COMPLETE SOURCE CODE FOR : DetermineClassObjectIsClassOrInterface.java
***********************************************************


public class DetermineClassObjectIsClassOrInterface
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("CreateInterface");
if(temp.isInterface())
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN INTERFACE");
}
else
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN CLASS");
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You