We know that tigers are animals. If the animal class is the parent class of the tiger class, it should be noted that when a tiger is an animal, the tiger will lose its unique attributes and functions. From the perspective of human thinking, saying "tiger is an animal" belongs to the upward thinking method, which is similar to the upward transformation object in the Java language.
Assume that the Animal class is the parent class of the Tiger class. When using a subclass to create an object and put the reference of this object into the object of the parent class, for example:
Animala;a=newTiger();
or
Animala;Tigerb=newTiger();a=b;
At this time, calling object a an upward-transformed object of object b is like saying "a tiger is an animal". The subclass is responsible for creating the entity of the up-transformed object, but the up-transformed object will lose some attributes and functions of the original object.
The upward transformation object has the following characteristics:
(1) The up-transformed object cannot operate the newly added member variables of the subclass, and cannot call the new methods of the subclass.
(2) The up-transformed object can access member variables inherited or hidden by the subclass, and can also call methods inherited by the subclass or instance methods overridden by the subclass. The upcast object operates methods inherited by the subclass or instance methods overridden by the subclass, and its function is equivalent to calling these methods on the subclass object. Therefore, if a subclass overrides an instance method of the parent class, when the object's upcast object calls this instance method, the instance method overridden by the subclass must be called.
Notice:
(1) Do not confuse objects created by the parent class with up-type objects of the subclass object.
(2) The upcast object of the object can be forced to be converted into a subclass object. At this time, the subclass object has all the attributes and functions of the subclass.
(3) You cannot assign references to objects created by the parent class to objects declared by the subclass. For example, you cannot say "The person is Chinese."