An object reference variable can reference an object instance of its own type or an object of its subclasses and indirect subclasses.
Class A inherits from Class B, so the object reference variable of Class A can reference the object of Class B, but even if the object reference to Class A refers to an object of Class B, when an object reference to Class A invokes a member variable or method of the same name in Class B, the system still uses the object reference of Class A instead of the object it points to. Otherwise, a forced type conversion is required.
forcing a type conversion is simply a reference type, and will not interfere with the object that really points to it. in this sentence, it can also lead to another meaning, that is, for the compilation, as long as the converted reference type and the converted target type is derived or derived from the relationship, the compilation is passed; at the same time, the compilation system does not know what the specific object the reference refers to, and it can only judge the conversion with no hope by reference type. at run time, you will know if you can make a reference to a forced type conversion.
A reference assignment can directly assign a subclass reference to a parent class reference, and if you want to assign a parent class reference to a child class reference, you must make a forced type conversion.
For reference comparisons,there are corresponding requirements in Java:
- References of the same type can be compared;
- Different types of references are to be compared, where the types have derived relationships;
- The comparison of references is to compare whether two references point to the same object.