1. Definition
If Class B is a subclass of Class A or an indirect subclass, when you create object B with Class B and assign a reference to object B to class A, such as:
A a;
a = new B();
OR
A a;
B b = new B();
a = b;
It is said that Class A object A is the upper transformation object of subclass B object B.
2. Nature
The entity of the upper transformation A of object B is created with subclass B, but the upper transformation object loses some of the properties and functions of sub-class B. The above transformation objects have the following characteristics:
- On a transformed object cannot manipulate the newly added member variables of the subclass, you cannot use the new method of the subclass. That is to lose some properties and functionality for sub-class B, these properties and functions are new.
- On a transformed object you can manipulate member variables that are inherited or hidden by subclasses, or you can use a subclass-inherited or overridden method. That is, the upper transformation object can manipulate the original properties and functions of the parent class, regardless of whether the methods are overridden.
- When you invoke a method on a transformed object, you invoke a method that inherits and is overridden by the child class. Not the new method, nor the original method of the parent class.
- You can then cast the object's upper transformation object to a subclass object, and the cast object has all the properties and functions of the subclass.
3. For example
Such as:
OutputStream output;
output = new FileOutputStream(filename);
And according to the Java API
java.lang.Object
java.io.OutputStream
java.io.FileOutputStream
At this point, output is the object of transformation. What's so special about it? That is, output can use FileOutputStream to inherit from the parent class OutputStream and the overridden method, rather than using subclasses to fileoutputstream their own new method. Finally, this method is very commonly used oh ... When the parent class has many subclasses, it can implement the polymorphism of the method. ^_^
Top-transforming objects on objects in Java