Programming Tutorials

getClass() and getSuperclass() in Java

By: Ivan Lim in Java Tutorials on 2007-09-12  

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





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)