A detailed explanation of Java’s upward and downward transformation

A detailed explanation of Java's upward and downward transformation

Transformation is based on inheritance. Inheritance is a mechanism of code reuse in object-oriented language. Through inheritance, subclasses can reuse the functions of the parent class. If the parent class cannot meet the needs of the current subclass, subclasses can rewrite the methods in the parent class to expand.

Upward Transformation: the transformation of an object referenced by a subclass into a parent type is called upward transformation. Generally speaking, it is to turn a child object into a parent object. Here, the parent class object can be an interface

Downward Transformation: the transformation of an object referenced by a parent class into a subclass type is called downward transformation.

The former is an upward transformation, and the animal dog reference points to new dog(); The subclass object is regarded as the parent object and can only call the members of the parent class. If the subclass overrides the method of the parent class, it points to the method overridden by the subclass according to this reference (this method is override). This calling process is called "dynamic binding".

Issues needing attention in Transformation:

During the upward transformation, the parent class points to the child class reference object, and other methods shared with the parent class object will be lost, that is, during the transformation process, the new methods of the child class will be lost. During compilation, the system will provide an error that the method cannot be found.

Examples are as follows:

In the process of downward transformation, there are two situations:

Scenario 1: if the parent class references an object that points to a child class object, it is safe in the process of downward transformation. That is, there will be no errors in compilation.

Case 2: if the object referenced by the parent class is the parent class itself, it is unsafe in the process of downward transformation, and there will be no compilation error, but there will be a java.lang.classcastexception error at runtime. It can use instanceof to avoid such errors.

Examples are as follows:

Summary:

1. A parent class reference can point to a child class object, and a child class reference cannot point to a parent class object.

2. Assigning a subclass object directly to a parent class reference is called upcasting upward transformation. Upward transformation does not require forced transformation.

For example, father = new son();

3. Assigning a parent class reference to a subclass object to a subclass reference is called downcasting, which requires forced transformation.

For example, father is a parent class reference to a subclass object. Assign father to the subclass reference son, that is, son son = (son) father;

The (son) in front of father must be added for forced conversion.

4. Upcasting will lose subclass specific methods, but subclass overrides the methods of parent class. Subclass methods are valid

5. The function of upward transformation is to reduce duplicate code. The parent class is a parameter, and sometimes the subclass is used as a parameter, which is the use of upward transformation. This makes the code concise. It embodies the abstract programming idea of Java.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>