Java implementation mechanism polymorphism

Is the parent class or interface defined reference variable can point to a subclass or instance of an object implementation class, and method of procedure calls only dynamic binding at runtime, the specific methods to achieve the object variable points is a reference, that is, memory is the method of operation of that object, rather than a reference type variable method defined.

In object-oriented programming ( Object-Oriented Programming , OOP), the multi-state mechanism is undoubtedly its most unique feature can even be said, do not use multi-state programming can not be called OOP. This is why some people say, the use of object-oriented programming and object-oriented programming language are two different things.

Polymorphism does not have a strict definition, the definition on Wikipedia to it under the more relaxed:

Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability of one type, A, to appear as and be used like another type, B.

A sub-type and sub-type

I would like to mention here all of a sudden Type ( Subtype ) word and subclass ( Subclass difference) is, simply put, as long as the class A extends use of keywords to achieve the inheritance of class B, then we can say Class A class B is a subclass of subclasses is the word on a grammatical level, as long as inheritance syntax, there is a subclass relationship.

Subtypes have more stringent requirements than the sub-class, which requires not only inheritance syntax, and to ask if there is a subclass of rewriting the parent class method (override), then rewrite the content must comply with the parent class original semantics, it is called after the same effect and action should be implemented in the direction of the parent class.

Comparison of the two is like to emphasize one point: only ensure subclass is a subtype polymorphism makes sense.

Second, the polymorphic mechanism

Polymorphism essentially in two ways:

1, compile-time polymorphism (also known as static polymorphism)

2, run-time polymorphism (also known as dynamic polymorphism)

Overload (overload) is an example of compile-time polymorphism, compile-time polymorphism has been determined at compile time, runtime runtime method of determining the call.

We usually refer to polymorphic refers polymorphism is running, is not sure exactly which specific method calls at compile time, it has been delayed in order to determine when to run. This is why sometimes polymorphic method is also known as reason for the delay method.

Behavior on Wikipedia polymorphism is described as:

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior.

The following brief mechanisms polymorphism (hereinafter referred to as polymorphism) runtime.

Polymorphic usually two methods:

1, subclass inherits the parent class (extends)

2, class implements the interface (the implements)

Either way, the core of the place is to rewrite or to realize the parent class method to interface methods to achieve different effects at run-time execution.

To use polymorphism, when declaring the object should follow a rule: Always parent class type or interface type declaration, to create the actual type. For example, suppose we want to create an ArrayList object, the statement should be used like this statement:

List list =newArrayList();

Instead of

ArrayList list =newArrayList();

Also normally always should be preferred parent class type or interface type parameters when defining a method, for example, a method should be written:

publicvoid doSomething(List list);

Instead of

publicvoid doSomething(ArrayList list);

The greatest advantage of this statement is that the flexibility of the structure: If one day I think the characteristics of the ArrayList can not meet my requirements, I want to be able to replace it with a LinkedList, you only need to put new ArrayList () changed to new objects created in place LinkedList can, other code not and will not change.

The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).

Virtual opportunity to call dynamic approach in the implementation of the program of real class, it will automatically be achieved by a mechanism known as dynamic binding (also called late binding), this process is transparent to the programmer.

Third, the use of polymorphic

Polymorphic biggest use I think that the reuse of design and architecture, and further, the " design mode " in advocating for programming interface rather than the typical example for the realization of programming is the full use of polymorphism. When the interface definition function definitions and components, may be left after the implementation flow into. An interface can simultaneously have multiple implementations, and even can use a variety of implements the interface (such as ArrayList and decisions on different characteristics to achieve exactly what LinkedList uses) while in a design.

Achieve four, polymorphism

Here from the perspective of the virtual machine to run briefly describes the implementation of the principle of multi-state, where the Java Virtual Machine ( Java Virtual Machine , the JVM) implementation specification as an example.

In the JVM execution of the Java bytecode, type information is stored in the method area usually call a method to optimize the speed of the object, the type of information a method of increasing the area of ​​the pointer, the pointer points to a table entry records such methods ( the method is called table), the table is a pointer to each of the corresponding method.

Constructor table is as follows:

Since Java's single inheritance, a class can only inherit from a parent class, but all classes they inherit from the Object class. The method of the first table stored in the Object class method, followed by the parent class of the class, is the last class of the method itself. The key areas here is that if a subclass rewrite the parent class, then those of the same name subclass and superclass method of sharing a table entry, have been recognized as the parent class.

Note that only non-private instance methods will appear, and the static method does not appear here, the reason is easy to understand: the static method has nothing to do with the object, the method can refer directly to the address, rather than an instance method requires indirect reference.

More in-depth talk, static method is called virtual machine instructions invokestatic, private methods and constructors are called by invokespecial instruction, only the method is called invokevirtual and invokeinterface instructions appear in the method table.

Since the alignment characteristics to the above process (parent Object - - subclass), so that the offset table method is always fixed. For example, for any class, the offset table method which method always equals a constant value, all the subclasses inherit a method table of the parent class, the offset method is also defined in the parent class Total is a fixed value.

As mentioned above, in the method table entry is a pointer to the class of the corresponding method, where a multi-start state:

Suppose Class A Class B is a subclass of A and method B, a method of rewriting (), then the method of Table B, the pointer pointing method is the method of method B method entry.

And for A is, method a method of its method table will point to its own method rather than the method of its parent (which is already guaranteed class loader when the class load, while the JVM always guaranteed from Object reference point to the correct type information).

The method of binding a pointer offset is fixed, and the pointer always points to the actual domain class method, we can easily find polymorphism mechanism here:

Method call, in fact, must first complete example of a method of symbol references are resolved, the result is that the reference symbol is interpreted as an offset table method. Virtual Machine reference method for obtaining information through the zone type object method table entry, query class, subclass object when the parent class type declaration, formally called the parent class method, this time from the virtual machine method practical classes table (although the parent class is declared, but in fact, the type of information stored here is information subclass) in the name of the method to find the corresponding pointer (used here, "Find" is actually unsuitable, the previously mentioned , the offset method is fixed, it can only be obtained in accordance with the offset pointer), and further can be directed to a method in the actual class.

Our story is not over, in fact, the above procedure is only for internal use inheritance mechanism polymorphic, another way to achieve polymorphism: implement the interface contrast is even more complicated because, Java's single inheritance guarantee a linear relationship between the classes, and can simultaneously achieve a plurality of interfaces, so that even with difficult to accurately obtain the offset pointer method. Therefore, in the JVM, polymorphic example of a method call actually two instructions:

invokevirtual instruction for calling a method declared class;

invokeinterface Instructions for invoking methods declared interface.

When invokeinterface instruction to call a method, you can not use a fixed offset recourse but honestly one by one to find (of course, the actual implementation is not necessarily the case, JVM specification does not specify exactly how to achieve this look, a different JVM implementation can have different optimization algorithms to improve search efficiency). We can see, in terms of performance, calling interface reference methods almost always slower than the method call class references. It also tells us, in between classes and interfaces interface as the preferred design is not always right, of course, the design problem is not within the scope of this article to explore, but apparently specific analysis of specific issues is still regarded as a better choice.

 

 

 

Personal opinion: multi-state multi-state mechanisms, including static (compile-time polymorphism) and dynamic polymorphism (polymorphic runtime), such as heavy-duty static polymorphism, dynamic polymorphism is not sure which method to call at compile time, it was in determined at runtime. Implementation of dynamic polymorphism comprises subclass inherits the class and parent class implements the interface. When the transformation of the plurality of sub-classes (not to say that do not know), the object is out with a method corresponding subclass, this implementation is related to the JVM.

Guess you like

Origin www.cnblogs.com/zmy-520131499/p/11128789.html