getClass() and getSuperclass() in Java

By: Ivan Lim 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 = y.getClass(); // get Class reference
System.out.println("y is object of type: " +
clObj = clObj.getSuperclass();
System.out.println("y's superclass is " +

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