getClass() and getSuperclass() in Java

By: Ivan Lim Viewed: 177 times  Printer Friendly Format    


The following program demonstrates getClass() (inherited from Object) and getSuperclass() (from Class):

// Demonstrate Run-Time Type Information.
class X {
int a;
float b;
}

class Y extends X {
double c;
}

class RTTI {
public static void main(String args[]) {
X x = new X();
Y y = new Y();
Class clObj;
clObj = x.getClass(); // get Class reference
System.out.println("x is object of type: " +
clObj.getName());
clObj = y.getClass(); // get Class reference
System.out.println("y is object of type: " +
clObj.getName());
clObj = clObj.getSuperclass();
System.out.println("y's superclass is " +
clObj.getName());
}
}

The output from this program is shown here:

x is object of type: X
y is object of type: Y
y's superclass is X



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial