Since we lost specific types of information during tracing (moving up in the inheritance structure), we had to use the "trace-back" technique in order to get concrete type information-that is, moving down the hierarchy. However, we know that a tracing model is certainly safe; the underlying class cannot have a larger interface than a derived class. Therefore, every message we send through the underlying class interface is definitely acceptable. But when it comes to styling, we don't really know that a geometric shape is actually a circle, it's probably a triangle, a square, or some other shape.
In order to solve this problem, there must be a way to ensure that the next model is properly carried out. Only in this way will we be able to pose as a wrong type and send a message that the object cannot receive. It is very unsafe to do so.
In some languages (such as C + +), special actions must be taken to perform a "type-safe" traceability. But in Java, all styling will be automatically checked and verified! So even if we just do a normal bracket shape and go into the runtime, we'll still be able to look at this shape without any mercy, and make sure it's the type we want. If not, you will get a classcastexception (class styling violation). The behavior of checking types during run time is called "Run-time type Identification" (RTTI). The following example shows you the behavior of Rtti:
//: Rtti.java//Downcasting & Run-time Type//Identification (RTTI) import java.util.*; Class Useful {public void F () {} is public void G () {}} class Moreuseful extends useful {public void F () {} publi c void G () {} public void U () {} public void V () {} ' public void W () {}} ' public class RTTI {public static void M
Ain (string[] args) {useful[] x = {new useful (), New Moreuseful ()};
X[0].F ();
X[1].G (); Compile-time:method not found in useful://!
X[1].U (); ((moreuseful) x[1]). U (); Downcast/rtti ((moreuseful) x[0]). U (); Exception thrown}}///:~
As in the schematic diagram, moreuseful (more useful) extends the useful (useful) interface. But since it is inherited, it can also be traced back to a useful. We can see that this occurs when the array x (in main ()) is initialized. Because the two objects in the array belong to the useful class, the f () and g () methods can be sent to two of them at the same time. And if you try to call U () (it only exists in moreuseful), you will receive a compile-time error message.
If you want to access an extended interface for a Moreuseful object, you can try to perform a retroactive modeling. If it is the right type, this action will succeed. Otherwise, you will get a classcastexception. We don't have to write any special code for this violation because it points to a programming error that may occur anywhere in the program.
The significance of Rtti is not only reflected in the modelling process. For example, there is a way to know what type of work you are dealing with before you try to look up the shape. The entire 11th chapter covers all aspects of the Java runtime type identity.