RTTI in Java using getClass() and instanceof
Source listing for the CShape parent class
//======================================================================
// CShape.java
// Purpose: Class file defining a base class for this RTTI demonstration.
// Author: Dr. Rick Coleman
//======================================================================
public class CShape
{
/** Shape type */
protected int m_iType;
/** Class name */
protected String m_sTypeName;
public static final int SHAPE = 0;
public static final int RECTANGLE = 1;
public static final int OVAL = 2;
public static final int LINE = 3;
public static final int POLYGON = 4;
//----------------------------------------------
/** Default constructor */
//----------------------------------------------
public CShape()
{
m_iType = SHAPE;
m_sTypeName = "CShape";
}
//----------------------------------------------
/** Get the type */
//----------------------------------------------
public int getType()
{
return m_iType;
}
//----------------------------------------------
/** Get the class name */
//----------------------------------------------
public String getTypeName()
{
return m_sTypeName;
}
}
Source listing for the CRectangle sub-class
//===========================================================================
// CRectangle.java
// Purpose: Class file defining a derived class for this RTTI demonstration.
// Author: Dr. Rick Coleman
//===========================================================================
public class CRectangle extends CShape
{
//----------------------------------------------
/** Default constructor */
//----------------------------------------------
public CRectangle()
{
m_iType = RECTANGLE;
m_sTypeName = "CRectangle";
}
}
Source listing for the COval sub-class
//===========================================================================
// COval.java
// Purpose: Class file defining a derived class for this RTTI demonstration.
// Author: Dr. Rick Coleman
//===========================================================================
public class COval extends CShape
{
//----------------------------------------------
/** Default constructor */
//----------------------------------------------
public COval()
{
m_iType = OVAL;
m_sTypeName = "COval";
}
}
Source listing for the CLine sub-class
//===========================================================================
// CLine.java
// Purpose: Class file defining a derived class for this RTTI demonstration.
// Author: Dr. Rick Coleman
//===========================================================================
public class CLine extends CShape
{
//----------------------------------------------
/** Default constructor */
//----------------------------------------------
public CLine()
{
m_iType = LINE;
m_sTypeName = "CLine";
}
}
Source listing for the CPolygon sub-class
//===========================================================================
// CPolygon.java
// Purpose: Class file defining a derived class for this RTTI demonstration.
// Author: Dr. Rick Coleman
//===========================================================================
public class CPolygon extends CShape
{
//----------------------------------------------
/** Default constructor */
//----------------------------------------------
public CPolygon()
{
m_iType = POLYGON;
m_sTypeName = "CPolygon";
}
}
Source listing for the demonstration.
import java.util.Vector;
//======================================================================
// RTTI_DemoMain.java
// Purpose: This is a demonstration of Run Time Type Identification
// in Java using the instanceof operator.
// Author: Dr. Rick Coleman
//======================================================================
public class RTTI_DemoMain
{
private Vector sVec;
//-----------------------------------------------
/** Default constructor */
//-----------------------------------------------
public RTTI_DemoMain()
{
sVec = new Vector();
// Create some shapes and add to the vector
sVec.add(new CRectangle());
sVec.add(new COval());
sVec.add(new CLine());
sVec.add(new CPolygon());
// See what we have
for(int i=0; i<sVec.size(); i++)
{
CShape s = (CShape)sVec.elementAt(i);
System.out.println("Type = " + s.getType() + ", Class = " + s.getTypeName() +
" (from calling functions inherited from CShape)");
System.out.println("Class = " + s.getClass() +
" (from calling the getClass() function)");
if(s instanceof CRectangle)
{
System.out.println("\tinstanceof recognizes a CRectangle instance.");
}
else if(s instanceof COval)
{
System.out.println("\tinstanceof recognizes a COval instance.");
}
else if(s instanceof CLine)
{
System.out.println("\tinstanceof recognizes a CLine instance.");
}
else if(s instanceof CPolygon)
{
System.out.println("\tinstanceof recognizes a CPolygon instance.");
}
if(s instanceof CShape)
{
System.out.println("\tinstanceof also recognizes a CShape instance.");
}
}
}
public static void main(String[] args)
{
RTTI_DemoMain rDemo = new RTTI_DemoMain();
}
}