Object: getClass() : Object « java.lang « Java by API






Object: getClass()

 
/*
 * Output: 
x is object of type: Class1
y is object of type: Class2
y's superclass is Class1

 *  
 */

class Class1 {
  int a;

  float b;
}

class Class2 extends Class1 {
  double c;
}

public class MainClass {
  public static void main(String args[]) {
    Class1 x = new Class1();
    Class2 y = new Class2();
    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());
  }
}


           
         
  








Related examples in the same category

1.Object: finalize()
2.Object: getClass() (2)
3.Object: hashCode()
4.Object: notifyAll()
5.Object: toString()
6.Object: wait(long timeout)