Here is a Java Program to Demonstrate Run-Time Type Information.
Output of Above Java Program
x is object of type: X
y is object of type: Y
y's superclass is X
class TestClone implements Cloneable { int a; double b; // This method calls Object's clone(). TestClone cloneTest() { try { // call clone in Object. return (TestClone) super.clone(); } catch(CloneNotSupportedException e) { System.out.println("Cloning not allowed."); return this; } } } 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()); } } class CloneDemo { public static void main(String args[]) { TestClone x1 = new TestClone(); TestClone x2; x1.a = 10; x1.b = 20.98; x2 = x1.cloneTest(); // clone x1 System.out.println("x1: " + x1.a + " " + x1.b); System.out.println("x2: " + x2.a + " " + x2.b); } }
Output of Above Java Program
x is object of type: X
y is object of type: Y
y's superclass is X