Java method Call Binding

Source: Internet
Author: User

Associating a method call with the same method body is called a binding .

Binding (implemented by the compiler and the linker, if any) prior to the execution of the program is called early binding .

When the compiler has only one reference, it cannot know exactly which method to invoke, and the solution is late binding, which means binding at run time based on the type of the object. Late binding is also known as dynamic binding or runtime binding .

In addition to the static method and final (private method belongs to the final method) in Java, all other methods are late-bound. This means that in the case of the same city, we do not have to decide whether late binding should be----it will happen automatically.

There is a base class Shape, and multiple export classes-Circle, Square, Triangle , and so on, each of which has a draw (), erase () method, which can be as simple as the following statement:

New Circle ();

Here, a Circle object is created and the resulting reference is immediately assigned to Shape, which looks like an error (assigning one type to another), but in fact it is not a problem, because through inheritance,Circle is a Shape.

Suppose you call a base class method (which is already overwritten in the export class)

S.draw ();

You might think that the call is a shape 's draw (), because this is after all a shape reference, so how does the compiler know to do other things? The Circle.draw () method was called because of late binding (polymorphic).

The following example is slightly different:

 Public classshape{ Public voidDraw () {} Public voiderase () {}} Public classCircleextendsshape{ Public voidDraw () {System.out.println ("Circle.draw ()"); }     Public voidErase () {System.out.println ("Circle.erase ()"); }} Public classSquareextendsshape{ Public voidDraw () {System.out.println ("Square.draw ()"); }     Public voidErase () {System.out.println ("Square.erase ()"); }} Public classTriangleextendsshape{ Public voidDraw () {System.out.println ("Triangle.draw ()"); }     Public voidErase () {System.out.println ("Triangle.erase ()"); }} Public classrandomshapegenerator{PrivateRandom Rand =NewRandom (47);  PublicShape Next () {Switch(Rand.nextint (3))        {            default:             Case0:return NewCircle ();  Case1:return NewSquare ();  Case2:return NewTriangle (); }    }} Public classshapes{Private StaticRandomshapegenerator RSG =NewRandomshapegenerator ();  Public Static voidMain (string[] args) {Shape [] s=NewShape[9];  for(inti = 0; i < s.length; i++) s [i]=Rsg.next ();  for(Shape shp:s) {Shp.draw (); }}} Output is: Triangle.draw () Triangle.draw () Square.draw () Triangle.draw () Square.draw () Triangle.draw () Square.draw () Triangle.draw () Circle.draw ( )

The shape base class establishes a common interface for all exported classes that inherit from it----that is, all shapes can be depicted and erased. The export class provides a separate behavior for each particular type of geometry by overriding these definitions.

Randomshapegenerator is a "factory" that, when each time we call the next () method, it randomly chooses to produce a reference to the Shape object. Notice that the upward transformation occurs after the return statement. Each return statement takes a reference to a Circle,Square , or Triangle and takes it from the next with the Shape type () method is sent out. So no matter when we call the next () method, it is absolutely impossible to know exactly what the exact type is, because we can always only get a generic Shape reference.

Main () contains an array of Shape references, filled with data by calling Randomshapegenerator.next () . At this point, we only know that we have some Shape, we will not know the more specific situation for the first time (the compiler does not know). However, when we iterate over an array and call the Draw () method for each array element, the specific behavior associated with the type will magically occur correctly.

The geometry is chosen randomly to better see that the compiler does not need to get any special information to make the correct call. All calls to the draw () method are made through dynamic binding.

Java method Call Binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.