From a question on StackOverflow Java dynamic binding

Source: Internet
Author: User

Let's first look at a question on StackOverflow, first on the code

1  Public classpiece{2      Public Static voidmain (String [] args) {3Piece P2 =NewKnight ();4Knight P1 =NewKnight ();5 p1.capture (p2); 6 p2.capture (p1); 7     }8      Public voidcapture () {9System.out.println ("Capture");Ten     } One      Public voidcapture (Piece p) { ASystem.out.println ("I ' m bored"); -     } - } the classKnightextendspiece{ -      Public voidcapture (Piece p) { -System.out.println ("Knight is bored"); -     } +      Public voidCapture (Knight k) { -System.out.println ("Too Slow Buddy"); +     } A}

Q. What is the output of this piece of code? The answer is

Knight is Boredknight is bored

Because I just started to learn Java, in the class with cs61b, and then the teacher talked about the dynamic type and static type, I was a little fuzzy, so on the StackOverflow search related issues, and really found a typical dynamic binding problem, After careful analysis of the feeling of harvest is very large, the understanding of inheritance more deeply.

Analysis: What's going on here? First of all, in line fourth, P1 is definitely a knight type, and for P2, it is a reference to a piece type that points to an object of the Knight class, at which point we call piece the static type, and knight to dynamic.

P1.capture (p2), also known as Knight.capture (Piece), has just defined this method in the Knight class, which is the output "Knight is bored".

P2.capture (p1), that is Piece.capture (Knight), at first glance piece there is no such method AH. Here should be an assignment, that is, piece p = p1, the reference to the class is assigned to the parent class, which is allowed. So actually call the Piece.capture (Piece), then the result should be output "I m bored" ah, how can be "Knight is bored"? As already mentioned, P2 actually points to an object of the Knight class, and in the Knight class just ovriride the Piece.capture (Piece) method, which is Knight.capture (Piece), so this method should be called, That is, output "Knight is bored".

OK now let's comment out this override method and see what happens.

1  Public classpiece{2      Public Static voidmain (String [] args) {3Piece P2 =NewKnight ();//static Piece, dynamic Knight4Knight P1 =NewKnight ();5 p1.capture (p2); 6 p2.capture (p1); 7     }8      Public voidcapture () {9System.out.println ("Capture");Ten     } One      Public voidcapture (Piece p) { ASystem.out.println ("I ' m bored"); -     } - } the classKnightextendspiece{ -     //Public void Capture (Piece p) {//here Override the method in piece . -     //System.out.println ("Knight is bored"); -     // } +      Public voidCapture (Knight k) { -System.out.println ("Too Slow Buddy"); +     } A}

The output is:

I ' m Boredi ' m bored

Analysis: The second output has just been explained because there is no Override method now, so call the Piece.capture (Piece p) method in the Piece class directly. So what's the first output going on? P1.capture (p2), which is knight.capture (Piece), this method cannot be called Knight.capture (Knight K), because Knight k = P2, which is illegal, The reference variable for a child class is not allowed in Java for a reference to the parent class. So in this case, is not this method in the Knight class? Do not forget that the Knight class is inherited on the Piece class, that is, the knight type can also invoke methods in the Piece class, that is, the Piece.capture (Piece) method, so the output is "I ' m bored".

Now we know that the special thing about Piece P2 = new Knight () is that there is a way to find out if there is an override in the subclass, and if it does, call the override method in the subclass, and if not, use the method in the parent class. If this is defined as: Piece p2 = new Piece (), then regardless of whether there is an override method in the subclass, the method of the subclass is not called, but the method of the parent class is called directly. As follows:

1  Public classpiece{2      Public Static voidmain (String [] args) {3         //Piece p2 = new Knight (); //static Piece, dynamic Knight4Piece P2 =NewPiece ();5Knight P1 =NewKnight ();6 p1.capture (p2); 7 p2.capture (p1); 8     }9      Public voidcapture () {TenSystem.out.println ("Capture"); One     } A      Public voidcapture (Piece p) { -System.out.println ("I ' m bored"); -     } the      Public voidcapture (Knight p) { -System.out.println ("Hello"); -     } - } + classKnightextendspiece{ -      Public voidCapture (Piece p) {//here Override the method in piece . +System.out.println ("Knight is bored"); A     } at      Public voidCapture (Knight k) {//here Override the method in piece . -System.out.println ("Too Slow Buddy"); -     } -}

The output is:

Knight is Boredhello

As we can see, even in the Knight class, override the. Capture (Knight) method, since P2 is always a piece class, the methods in the Knight class are not called at all, so P2.capture (p1), That is Piece.capture (Knight), output "Hello". Change the line to piece p2 = new Knight (), the output is:

Knight is Boredtoo slow buddy

The second output is called the override method in the Knight class.

From a question on StackOverflow Java dynamic binding

Related Article

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.