Run-Time Type Identification (RTTI) is a very useful mechanism in Java. RTTI maintains class-related information when Java is running.
Polymorphism is implemented based on RTTI. The functions of RTTI are mainly implemented by the Class class.
Class class
Class class is "class of classes" (class of classes). If a class is an abstraction and collection of objects, then the Class class is an abstraction and collection of classes.
Each Class object represents another class. For example, in the following program, the object c1 of the Class class represents the Human class, and c2 represents the Woman class.
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
Class c1 = aPerson.getClass();
System.out.println(c1.getName());
Human anotherPerson = new Woman();
Class c2 = anotherPerson.getClass();
System.out.println(c2.getName());
}
}
classHuman
{
/**
*accessor
*/
public int getHeight()
{
return this.height;
}
/**
* mutator
*/
public void growHeight(int h)
{
this.height = this.height + h;
}
private int height;
}
class Woman extends Human
{
/**
* new method
*/
public Human giveBirth()
{
System.out.println("Give birth");
return (new Human());
}
}
When we call the getClass() method of the object, we get a reference to the corresponding Class object.
In c2, even if we upconvert the reference of the Women object to the reference of the Human object, the Class object pointed to by the object is still Woman.
Every object in Java has a corresponding Class object. Therefore, we can know the class to which an object "really" belongs at any time through the Class object. No matter what type conversion we perform on the reference, the Class object corresponding to the object itself is the same. When we call a method through a reference, Java can always find the method defined in the correct Class class and execute the code in the Class class. Due to the existence of Class objects, Java will not get lost due to type upcasting. This is the principle of polymorphism.
getClass: Who am I?
In addition to the getClass() method, we have other ways to call objects of the Class class.
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Class c3 = Class.forName("Human");
System.out.println(c1.getName());
Class c4 = Woman.class
System.out.println(c2.getName());
}
}
There are two ways shown above:
1. The forName() method receives a string as a parameter, which is the name of the class. This will return the corresponding Class object.
2. The Woman.class method directly calls the class member of the class. This will return the corresponding Class object.
Class method
The Class object records the information of the corresponding class, such as the name of the class, the package in which the class is located, etc. We can call the corresponding method, such as:
Copy the code code as follows:
getName() returns the name of the class
getPackage() returns the package where the class is located
You can use the newInstance() method of the Class object to create objects of the corresponding class, such as:
Copy the code code as follows:
Human newPerson = c1.newInstance();
newInstance() calls the default build method without parameters.
We can get the members of the class definition:
Copy the code code as follows:
getFields() returns all public data members
getMethods() returns all public methods
Reflection analysis classes can be further used. No more details here.
For more methods of the Class class, please check the official documentation:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html
Class class loading
When Java creates an object of a certain class, such as a Human class object, Java checks whether there is a corresponding Class object in the memory.
If there is no corresponding Class object in the memory, Java will look for the definition of the Human class in the .class file and load the Class object of the Human class.
After the Class object is loaded successfully, the creation and related operations of other Human objects will refer to the Class object.