RTTI, Class & Reflection

Lectures 22

((Circle)S).getRadius(); // method is not a member of Shape class

((Square)S).getCorner();

if (x instanceof Circle)

((Circle)S).getRadius();

if (x instanceof Square)

((Square)S).getCorner();

No Runtime ClassCastException

Shape c = new Circle()

c instanceof Circle //true

c instanceof Shape //true

c instanceof Square //false

public boolean isInstance(Object obj)

Class cls = Shape.class; // no constructor

Shape s = new Circle();

// checking for an Shape instance

boolean retval = cls.isInstance(s);

Class x_class = x.getClass();

Class super_class = x.getSuperclass();

Class circle_class = Class.forName(“Circle“);

Class square_class = Square.class;

String c_name = x_class.getName();

Class[] x_ifaces = x.getInterfaces();

for (int i = 0; i < x_ifaces.length; i++) {

String ifaceName = x_ifaces[i].getName();

isInterface() method

isPrimitive();

Field [] get Declared Fields();

isArray();

Methods[] getDeclaredMethods();

isLocalClass(); …

isMemberClass();