Java polymorphism definition and usage examples

This article describes the definition and usage of Java polymorphism with examples. Share with you for your reference, as follows:

Polymorphism is achieved by:

1 interface and implement the interface and cover several different classes of the same method in the interface

The parent class and inherits the parent class and overrides the implementation of several different subclasses of the same method in the parent class

1、 Basic concepts

Polymorphism: Send a message to an object and let the object decide what behavior to respond to. Dynamic method calls are implemented by assigning subclass object references to superclass object reference variables.

This mechanism of Java follows a principle: when a superclass object references a variable and a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method to call, but the called method must be defined in the superclass, that is, the method overridden by the subclass.

If a is a reference to class A, a can point to an instance of class A, or to a subclass of class A.

If a is a reference to interface a, then a must point to an instance of a class that implements interface a.

2、 Java polymorphism implementation mechanism

Sun's current JVM implementation mechanism, the reference of a class instance is a pointer to a handle, which is a pair of pointers:

One pointer points to a table. In fact, the table also has two pointers (one pointer points to a method table containing an object, and the other points to a class object, indicating the type of the object);

Another pointer points to a piece of memory space allocated from the Java heap for.

3、 Summary

1. Dynamic method calls are implemented by assigning subclass object references to superclass object reference variables.

analysis:

1. Why can an object instance of a subclass type override a superclass reference?

Automatic upward transformation. Through this statement, the compiler automatically moves the subclass instance upward to become the general type baseClass;

2. Will A. play () execute methods defined by subclasses or superclasses?

Subclass. At run time, the corresponding method will be obtained by referring to the actual type according to a this object. That's why there's polymorphism. The object reference of a base class is given different subclass object references. When this method is executed, it will show different behavior.

When A1 = C2, there are still two handles, A1 and C2, but A1 and C2 have the same data memory block and different function tables.

2. You cannot assign a parent object reference to a child object reference variable

In Java, upward transformation is automatic, but downward transformation is not. We need to define and enforce it ourselves.

c1=(DerivedC)a2; Forced transformation, that is, downward transformation

3. Remember a very simple and complex rule that a type reference can only refer to the methods and variables contained in the reference type itself.

You may say that this rule is wrong, because when the parent class reference points to the child class object, the last execution is the method of the child class.

In fact, this is not contradictory because late binding is adopted, and subclass methods are called according to the type during dynamic operation. If this method of the subclass is not defined in parent class, an error will occur.

For example, in addition to inheriting the functions defined in baseClass, the derivedc class also adds several functions (such as myfun())

analysis:

When you use a parent class reference to point to a child class, the JVM has used the type information generated by the compiler to adjust the conversion.

Here you can understand that it is equivalent to setting the functions not contained in the parent class to be invisible from the virtual function table. Note that it is possible that some function addresses in the virtual function table have been rewritten in the subclass, so the virtual function item address in the object virtual function table has been set to the address of the method body completed in the subclass.

4. Comparison of polymorphism between Java and C + +

The solution of JVM's polymorphism support is almost the same as that in C + +, but many compilers in C + + put type information and virtual function information in a virtual function table, but use some technology to distinguish them.

Java separates type information and function information. After inheritance in Java, subclasses will reset their virtual function table. The items in this virtual function table are composed of two parts. Virtual functions inherited from the parent class and the subclass's own virtual functions.

Virtual function call is called indirectly through virtual function table, so it can realize polymorphism. All functions in Java, except those declared final, are bound later.

IV A behavior, different objects, they are embodied in different ways,

For example, method overloading and method overriding

At this time, it is the same as running, but different (this is an example of method coverage)

This example is a method overload with the same method name and different parameter tables

OK, it's not enough to understand these. People are running, for example

In this way, I instantiate a man object and declare a human reference to point to the man object

It means that man is regarded as human

For example, when you go to the zoo, you see an animal and don't know what it is, "what animal is this?" "This is a giant panda“

These two sentences are the best proof, because you don't know that it is a giant panda, but you know that its parent is an animal. Therefore, it's reasonable for you to treat this giant panda object as its parent In this way, pay attention to new man(); The does instantiate the man object, so ahuman The output of the run () method is "men are running". If you write some unique methods under the subclass man, such as eat (), but human does not have this method, when calling the eat method, you must pay attention to the forced type conversion ((man) ahuman). Eat (), so as to

The situation is similar for interfaces

Most of the above methods implement polymorphism by overriding the parent class with a subclass Here is another way to implement polymorphism -- C overriding the parent class method

1. There is no multi inheritance in Java. A class can have a parent class. The performance of inheritance is polymorphism. A parent class can have multiple subclasses, and the method of the parent class (such as method print ()) can be overridden in the subclass, so that the rewritten code in each subclass is different, and the natural expression is different. In this way, when using the variables of the parent class to refer to different subclasses, the results and manifestations obtained when calling the same method print () are different, which is polymorphism. The same message (that is, calling the same method) will have different results. For example:

The same method is called and different results appear! This is the expression of polymorphism!

For more Java related content, interested readers can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

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