"U-turn" On the Importance of polymorphism mechanism and its implementation

Reprinted from: https://hesey.wang/2010/12/significance-and-implementation-of-polymorphism.html, thanks for sharing

What is a multi-state mechanism?

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.

On Meaning and multi-state mechanism

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

Here I would like to mention sub-type (Subtype) word and subclass (Subclass) difference, simply, 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:

  • Compile-time polymorphism (also known as static polymorphism)

  • Runtime polymorphism (also known as dynamic polymorphism)

Overload (overload) compile-time polymorphism is an example of 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, that is, which is uncertain whether the specific method calls at compile time , has been delayed in order to determine when to run. This is why sometimes polymorphic method is also known as delaying reason.

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:

  • Subclass inherits the parent class (extends)

  • 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 the 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 =new ArrayList<>();
//而不是
ArrayList<> list =new ArrayList<>();

//先使用父类类型或接口类型,例如某方法应该写成:
public void doSomething(List list);
//而不是
public void 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 pass called dynamic binding (also called late binding) automatic implementation mechanism of this process is transparent to the programmer.

Third, the use of polymorphic

Polymorphic biggest use I think that for the reuse of design and architecture , and further, the "design mode" advocated an interface for programming instead of the typical examples 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, JVM) implementation specification as an example.

In the JVM execution of the Java bytecode, type information is stored in the process area , generally 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 ( called method 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. Method table first method is stored in the Object class, followed by the parent class of the class, is the last class of the method itself. The key here is that place, if a subclass rewrite the parent class, then those of the same name subclass and superclass method of sharing a table entry, it has 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).

Binding method 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.

  • The method of the virtual machine reference zone obtained by the object entry type information, the class method table lookup.

  • When the subclass object parent class type declaration, formally called the parent class method, this time from the virtual method table real opportunity class (although the statement is the parent class, but in fact this type of information is stored in subclass information) in the name of the method to find the corresponding pointer (used here, "Find" is actually unsuitable, mentioned earlier, is a fixed offset process, it is only offset can be obtained according to 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, the 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.

Guess you like

Origin www.cnblogs.com/antonzhao/p/12446904.html