Java object type conversion and polymorphism (example explanation)
Object type conversion
It is divided into upward transformation and downward transformation (forced object transformation). Upward transformation is the process of transforming a child object to a parent object, for example, from a cat to an animal; Downward transformation is implemented by forced transformation, and the parent object is forced to be converted into a child object. This is similar to the conversion of basic data types. Byte will be automatically converted to int (up conversion) when necessary, and int can be forcibly converted to byte (down conversion).
For object transformation, members unique to child objects will not be accessible after upward transformation. It means that when an animal is needed, the cat can be passed on as an animal, because the cat inherits from the animal and the cat has all the attributes of the animal. However, after the upward transformation, the cat is no longer a cat, but regarded as an animal, and its own unique attributes and methods are invisible. In other words, after the upward transformation, only the content in the parent object can be recognized.
You can judge whether the object pointed to by the reference variable belongs to a class by "referencing the variable instanceof class name", that is, "whether the object is a class". For example, declare the reference "cat C" of a cat object, and then "C instanceof animal" means "is object c an animal?" All objects that instanceof returns true can be converted to class objects, but some may need to be forced.
Upward transformation can be carried out automatically, which is logical. Dogs inherit automatic objects and are themselves an animal. Therefore, when animals are needed, losing a dog in the past will automatically transform upward into animals. But at this time, the dog is no longer a dog, but an animal, so the unique members of the dog are no longer visible.
The method of coercion is the same as that of basic data type coercion, which is to add the target type before the object to be converted, for example, to convert animal a to dog D: dog d = (dog) a.
The following is an example of object type conversion, which well analyzes whether it can be transformed, whether some members can be accessed after transformation, and so on.
For the above a = new dog ("yellowdog", yellow), a is an animal type, but at this time it points to a dog object. That is, it is a dog, so it is also an animal class, so a instanceof animal); And a instanceof dog; All are true, which is determined by its "pointer". However, because its type is animal, which determines what data can be stored. It is invisible to existing data that does not conform to the type. Therefore, the animal type determines that it can only see the animal part of the dog object.
As shown below:
Since it can be transformed upward, with the logical judgment of instanceof, it can achieve good scalability. For example, the sing (animal a) method of an animal class needs an animal class and can give it a dog D. at this time, it will transition upward (just like the double type is given an int data). Although it has been transformed, the actual reference of dog D is still a dog object. Therefore, if (a instanceof dog) is judged to be true, the statement that can reflect the particularity of the dog sing () method will be called. If a cat is passed, if judges and calls a statement that reflects the particularity of the cat sing () method. In this way, whenever you want to add an animal, you only need to add an IF statement.
See the following example:
If there is no object transformation, sing () should be defined once in dog and once in cat. To add an animal class, you also need to define sing () in the animal class. It's much more convenient now. Just modify the if statement inside the sing () method.
Note that the above sing () method does not belong to the methods of animal or other subclasses, but is independently defined and called in other classes.
polymorphic
Although upward transformation improves scalability to a certain extent, the degree of improvement is not too high. Based on the upward transformation, Java's polymorphic implementation is more extensible and convenient.
Polymorphism is also called dynamic binding or late binding. It is a binding during execution, not a binding during compilation (this is a static binding or early binding).
The principle of polymorphism is that when a method is rewritten after the transformation, the parent class method is called, but in fact, the subclass rewrite method is invoked dynamically. In fact, the parent method is bound during compilation, but the corresponding method of the subclass is dynamically transferred during execution.
For example, the sing () method of the animal class, cat and dog classes override the sing () method. When an animal object is needed and a cat class is passed, the sing () method of cat is called. The logic of dynamic binding is similar to the following code:
Here is an example of polymorphism
The compiled execution result is:
In the above example, the construction method of Lady class and the code of calling sing () method are:
If the pet of the constructed lady object is cat Object C, the C will first be transformed upward into an animal class, that is, although the pet attribute of Lady points to the "cat C" object, it can only see the animal part of the parent object. Then mypetsing (PET. Sing();) Method naturally calls the sing () method of the animal class. The above processes are considered by the compiler as well as static binding or pre binding processes.
However, after compilation, although the pet attribute can only see the animal part, it is actually pet Sing() is converted to c.sing(). This is equivalent to an object type cast, cat petx = (CAT) pet. This is the process of dynamic binding or late binding, also known as polymorphism.
In fact, after the object is new, the methods involved are placed in a method list in the code segment memory area. This list contains the methods of subclasses and parent classes, but sometimes invisible methods cannot be called. When the program is executed, the internal mechanism can search the method most suitable for the environment from the method list and execute it.
The key points of realizing polymorphism are:
(1). Define a parent class reference f and point it to a child class object, that is, make an upward transformation;
(2). Override the method of the parent class and use the parent class reference f to reference the method. This allows you to program against the parent class.
As in the above example, pet is defined as the Animal class instead of the specific subclass, and the pet. is called in the method. sing()。 In this way, there is no need to consider whether pet is cat / dog. When adding bird class for function expansion, there is no need to modify the code of Lady class at all.
For another example, the parent class animal, the child class dog, and the method sing ().
The above article on Java object type conversion and polymorphism (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.