Explain the type conversion and dynamic binding of Java polymorphic objects in detail

Type conversion of Java polymorphic objects the object type conversion mentioned here refers to the object with inheritance relationship, not any type of object. When you cast an object that does not have an inheritance relationship, the Java runtime will throw a Java Lang.classcastexception exception.

In the inheritance chain, we call the transformation from a child class to a parent class "upward transformation", and the transformation from a parent class to a child class "downward transformation".

Many times, we define variables as the type of the parent class, but reference the object of the child class. This process is upward transformation. Program runtime calls subclass methods through dynamic binding, that is, polymorphism.

However, sometimes, in order to complete some functions that the parent class does not have, we need to convert the subclass object after upward transformation into a subclass and call the subclass method, which is downward transformation.

Note: you cannot directly convert the parent class object to a subclass type. You can only convert the upward transformed subclass object to a subclass type again. In other words, subclass objects must be transformed upward before they can be transformed downward. See the following code:

Remove the comment on line 7 and an exception will be thrown at runtime, but the compilation can pass.

Because there are risks in downward transformation, when receiving a reference from the parent class, be sure to use the instanceof operator to judge whether the object is the child class you want. See the following code:

Operation results:

Summary: the object type conversion is checked when the program is running. The upward transformation will be carried out automatically. The downward transformation object must be a subclass of the current reference type.

Java polymorphism and dynamic binding in Java, the variables of the parent class can refer to the instance of the parent class or the instance of the child class.

Please read a code first:

Operation results:

The above code defines three classes: animal, cat and dog. Cat and dog classes inherit from animal class. The type of obj variable is animal. It can point to both instances of animal class and cat and dog classes, which is correct. In other words, the variables of the parent class can refer to the instance of the parent class or the instance of the child class. Note that the reverse is wrong, because all cats are animals, but not all animals are cats.

It can be seen that OBJ can be human, cat and dog. It has different forms, which is called polymorphism. Polymorphism means that a thing has different forms or forms.

For another example, "human" also has many different expressions or implementations. TA can be drivers, teachers, doctors, etc. when you hate yourself, you will say "be a new man in the next life". Then you can become drivers, teachers and doctors in the next life. We say that "human" has polymorphism.

There are three necessary conditions for polymorphism: inheritance, rewriting, and parent variable referencing subclass objects.

When calling a method in a polymorphic way: first check whether the method exists in the parent class. If not, there will be a compilation error; If so, check whether the subclass overrides the method. If the subclass overrides the method, the method of the subclass is called, otherwise the method of the parent class is called.

As can be seen from the above examples, one advantage of polymorphism is that when there are many subclasses, there is no need to define multiple variables. You can only define a variable of parent type to refer to instances of different subclasses. Please take another look at the following example:

Operation results:

The feed method of the master class has two parameters: animal type and food type. Because it is the parent class, you can pass the instance of the child class to it, so the master class does not need multiple methods to feed different animals. Dynamic binding

In order to understand the essence of polymorphism, let's talk about the detailed process of Java calling methods.

1) The compiler looks at the declared type and method name of an object.

Suppose you call obj Func (param), obj is an object of cat class. It should be noted that there may be multiple methods with the name func but different parameter signatures. For example, there may be methods func (int) and func (string). The compiler will list all the methods named func in cat class and the methods with access property public and name func in its parent class animal one by one.

In this way, the compiler obtains a list of all candidate methods that may be called.

2) Next, the compiler checks the parameter signature provided when the method is called.

If there is a method in all methods named func that exactly matches the supplied parameter signature, select this method. This process is called overload resolution. For example, if func ("hello") is called, the compiler selects func (string) instead of func (int). Due to the existence of automatic type conversion, for example, int can be converted to double. If a method with the same signature as the calling method parameter is not found, carry out type conversion and continue to search. If there is no matching type or multiple methods match, there will be a compilation error.

In this way, the compiler obtains the method name and parameter signature to be called.

3) If the modifiers of a method are private, static, final (static and final will be explained later), or a construction method, the compiler will know exactly which method to call. We call this method static binding.

Correspondingly, the method called depends on the actual type of the object and implements dynamic binding at run time. For example, call func ("hello"), and the compiler will generate an instruction calling func (string) by dynamic binding.

4) When the program runs and the method is called with dynamic binding, the JVM will call the method of the class most suitable for the actual type of the object referenced by obj. We have assumed that the actual type of obj is cat, which is a subclass of animal. If func (string) is defined in cat, it will be called. Otherwise, it will be found in animal class and its parent class.

Every time a method is called, it needs to be searched, and the time cost is quite large. Therefore, the JVM creates a method table for each class in advance, which lists the names, parameter signatures and classes of all methods. In this way, when the method is actually called, the virtual machine only looks up this table. In the above example, the JVM searches the method table of the cat class to find a method that matches the call to func ("hello"). This method can be either cat Func (string), or animal func(String)。 Note that if you call super Func ("hello"), the compiler will search the method table of the parent class in rows.

Assuming that the animal class contains three methods: cry (), getname (), and getage (), its method table is as follows: cry () - > animal cry() getName() -> Animal. getName() getAge() -> Animal. getAge()

In fact, animal also has a default parent class object (which will be explained later) and inherits the methods of object, so the methods listed above are not complete.

Assuming that cat class overrides the cry () method in animal class and adds a new method climbtree (), its parameter list is: cry () - > cat cry() getName() -> Animal. getName() getAge() -> Animal. getAge() climbTree() -> Cat. climbTree()

When running, call obj The process of cry () method is as follows: the JVM first accesses the method table of the actual type of obj, which may be the method table of animal class or the method table of cat class and its subclasses. The JVM searches for the method matching cry () in the method table. When it finds the method, it will know which class it belongs to. The JVM calls this method.

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
分享
二维码
< <上一篇
下一篇>>