Downcasting and upcasting in Java, rtti and reflection mechanisms

Source: Internet
Author: User

 

JavaIn the downward Transformation (Downcasting) And generic and upward Transformation (Upcasting) AndRttiReflection mechanism

 

Upward Transformation (Upcasting): An object can be used as its own type or as its parent class (base class) object,JavaIs a single inheritance, so it is a safe approach.

When you initialize a subclass and generate an object, the object contains the parent class object as a sub-object. The subclass method will be lost in the upward transformation. However, all objects can be stored and called in a common container to achieve the reuse of containers (the collection container sets store all objects as objects by default ), however, you must determine the original identity of the object.

Java dynamically identifies objects and classes at runtime. There are two mechanisms:

1. rtti (run-time type identification) class identification during runtime (forced type conversion "()", type conversion is performed at runtime ), it assumes that all types are known during compilation and runtime.

Java implements the rtti mechanism through the Class Object

Java creates a Class Object for each class (*. class), which includes information related to the class, used to create a regular object of the class, at runtime, once we want to generate an object of a class, the JVM running this program first checks whether the class object of this class is loaded. If the class object is not loaded yet, the JVM will search for it based on the class name. class file, and load it, then you can create any object of this class. Java programs are not fully loaded when they are not executed at the beginning. The class object is loaded only when needed, and the static statement block is executed when the class is loaded.

Generate two reference methods for the Class Object:

  • Class. forname("Gun")

  • Literal constants:Gun. Class

RttiRestrictions: classes must be known during compilation, opened and checked during compilation. ClassFile.

2. The reflection mechanism (reflection) allows us to obtain class information at runtime.

Reflection mechanism enabled and checked at runtime. ClassFile.

 

 

Downward Transformation (Downcasting): The parent class object is converted down to a specific subclass object, but it is difficult to determine what type of subclass should be converted for unsafe operations.

To reuse containers, Java defines an object of the common type as the parent class of all classes and the features inherited by a single Java program. This means that the object can store instances of any classes. When using an object as a container, You need to place the object reference in it, and then you can retrieve and use them, but at the same time, the original data object will be transformed to an object, it will lose its identity. How can we change back to the original object?

The solution is parameterized type, that is, the operated data type is specified as a parameter. This type of parameter can be used to create classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods. This is a new feature introduced by JDK 5.0, referred to as generic.

If there is no generic type, parameters can be "arbitrary" by referencing the type object. The disadvantage of "arbitrary" is explicit forced type conversion, this type of conversion requires developers to make predictions about the actual parameter type. If the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running. This is a security risk.

The advantage of generics is to check the type security during compilation, and all the forced conversions are both automatic and implicit, improving the code reuse rate.

GenericRules and restrictions

1. generic type parameters can only be class types (including custom classes), not simple types.

2. The same generic type can correspond to multiple versions (because the parameter type is uncertain), and the generic instances of different versions are incompatible.

3. There can be multiple generic type parameters.

4. You can use the extends statement for generic parameter types, for example, <t extends superclass>. It is a "bounded type ".

5. The generic parameter type can also be a wildcard type. For example, class <?> Classtype = Class. forname (Java. Lang. String ).

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.