Java polymorphism (Dynamic binding)
@author Ixenos
Binding
The invocation of a method is associated with the body of a method, called a (method call) Binding:
1. Pre-binding: Binding before program execution (implemented by compiler and linker);
2. late binding : At run time based on the type of object binding (also known as dynamic binding or runtime binding );
A) Implementation conditions: Can judge the type of the object at run time, thus associating the corresponding body, calling its method
b) The compiler has been unaware of the true object type, only as the type of the reference variable and knows that there is an inheritance relationship
c) In addition to the static method and the final method (private method belongs to the final method) in Java is the pre-binding, All other methods are late-bound
Dynamic bindings Implement polymorphism:
Polymorphic Effects:
1. Eliminating the coupling between types allows us to write program code that deals only with the base class (e.g. list<string> = new arraylist<> ();)
2. To make the program extensible, we can inherit new data types from a common base class to add new functionality without changing the method that corresponds to the accepted parameters, only communicating with the base class interface
Dynamic binding:
1. Suppose B extends a, shut up a = new B () is transformed upward, then the compiler thinks this is a reasonable covariance, compiled through! But at this point the compiler only know that B is a subclass of a, unable to know the specific type of B!
2. At runtime, the late-binding mechanism of Java determines that the runtime type of the object new B () is B, so the method's invocation policy is to invoke the appropriate method from class B (except static, final method)
3. Dynamic binding (late-bound or run-time binding) is often referred to as polymorphic
Polymorphic defects
when the private method is overwritten :
1 Public classpri{2 Private voidF () {System.out.println ("I ' M private f ()"); }3 4 Public Static voidMain (string[] args) {5pri pri =NewPub ();//Dynamic call to Pub class method6 pri.f ();7 }8 }9 Ten classpub{ One Public voidF () {System.out.println ("I ' M public f ()"); } A } - - the---------------------------------- -Output: I ' M private f ()View Code
We expect the output to be public f (), but the private method is considered the final method, which is masked for the exported class (subclass), so there are no overrides and overloads!
At this point, the F () method in the pub class is a completely new method, and two methods exist in different classes
Conclusion: Although only non-private methods can be overridden, it is also important to note that this behavior attempts to override the private method, after all, the compiler does not error (because it is simply creating a new method) that causes the method not to be executed in the original way
non-applicable polymorphic range : Domain, static method and final method
Only the invocation of a normal method can be polymorphic
Domain: When a sub object is transformed into a super reference, any domain access operations are parsed by the compiler and therefore not polymorphic At this point, Super.field and Sub.field are allocated different storage space, when the sub contains two fields, his own and from Super, if Super.field is private, then sub is invisible, if public and the subclass domain overrides the parent class domain, the sub's default domain is is a subclass domain, to call the parent class field to use Super.field (field to replace with the parent class field reference variable)
static method: A static method is associated with a class and not with an object
Pure inheritance and expansion interfaces
Pure inheritance: The Export class only overrides the base class's method, the export class only has the same interface as the base class, at which point the export class can completely replace the base class, the base class can accept any information sent to the export class, we just need to transform from the export class, never need to know the exact type of the object being processed, This is handled by polymorphic (dynamic binding)
1 shape{2 Draw () {}3 erase () {}4 }5 6Circleextendsshape{7 Draw () {}8 erase () {}9 }Ten OneSquareextendsshape{ A Draw () {} - erase () {} - } the -Triangleextendsshape{ - Draw () {} - erase () {} +}View Code
Extension interface: Extends the functionality of the exported class, however, the extended portion of the interface in the exported class cannot be accessed by the base class, so once the exported class object is transformed upward, the new methods cannot be called
1 useful{2 Draw () {}3 erase () {}4 }5 6Moreusefulextendsuseful{7 Draw () {}8 erase () {}9 Ten //Expansion Interface One Freak () {} A FlyMe () {} -}View Code
Downward transformation and Rtti
Upward transformation: The upward transformation is secure because the base class does not have an interface that is larger than the exported class
Downward transition: The downward transition is unsafe, and the runtime type recognizes the RTTI (run-time type identification) to ensure a downward transition is correct
1 useful{2 Draw () {}3 erase () {}4 }5 6Moreusefulextendsuseful{7 Draw () {}8 erase () {}9 Ten //Expansion Interface One Freak () {} A FlyMe () {} - } - the Public classrtti{ - Public Static voidMian (string[] args) { -useful[] x = {NewUseful (),Newmoreuseful ()}; - + //( (moreuseful) x[0]). Freak ();//ERROR down transformation failed because the X[0] object does not have an extension interface in the Moreuseful class - +((moreuseful) x[1]). Freak ();//down Transformation Success/rtti A at - - - - - in Rtti Behavior Example
An object referenced by a base class, and if you want to access an extension interface for a particular exported class object, you can try a downward transformation, but the base class object itself must be an export class type or its subtype for the transformation to succeed
Java polymorphism (Dynamic binding)