Class

  • Class encapsulates the run-time state of a class or interface.

  • Objects of type Class are created automatically, when classes are loaded.

  • We cannot explicitly declare a class object.

  • We can obtain a class object by calling getClass() method defined by Object.

  • Class generic type is as follows

    class Class<T>

  • T is the type of the class or interface represented.

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

class Y extends X 
{
 double c;
}

public 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());
 }
}

Output:

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

ClassLoader

  • ClassLoader is the abstract class which defines how the classes are loaded.
  • We can create sub classes to ClassLoader by implementing the methods defined by the ClassLoader which makes classes to load in some other way than the way they are normally loaded by the Java run-time system.

results matching ""

    No results matching ""