From an issue on stackoverflow, we can see that Java Dynamic binding and stackoverflowjava

Source: Internet
Author: User

From an issue on stackoverflow, we can see that Java Dynamic binding and stackoverflowjava

Let's take a look at a problem on stackoverflow, first go to the code

 1 public class Piece{ 2     public static void main (String [] args){ 3         Piece p2 = new Knight();   4         Knight p1 = new Knight(); 5         p1.capture(p2);    6         p2.capture(p1);    7     } 8     public void capture(){ 9         System.out.println("Capture");10     }11     public void capture(Piece p){12         System.out.println("I'm bored");13     }14 }15 class Knight extends Piece{16     public void capture(Piece p){       17         System.out.println("Knight is bored");18     }19     public void capture(Knight k){      20         System.out.println("Too slow buddy");21     }22 }

What is the output of this code? The answer is:

Knight is boredKnight is bored

Because I was just learning Java, I was in a class with CS61B, And when I talked about dynamic type and static type, I was a little fuzzy, so I searched for problems related to stackoverflow, as a result, I found a typical dynamic binding problem. After careful analysis, I felt that I had gained a great deal and I had a deeper understanding of inheritance.

Analysis: what is the problem? First, let's look at Row 3 and row 4. p1 must be of the Knight type. For p2, it is a reference of the Piece type and points to an object of the Knight class, in this case, we call Piece static type and Knight dynamic type.

P1.capture (p2), that is, Knight. capture (Piece). The Knight class just defines this method, that is, output "Knight is bored ".

P2.capture (p1), that is, Piece. capture (Knight). At first glance, there is no such method in Piece. Here there should be a value assignment, that is, Piece p = p1, which grants the reference of the subclass to the parent class, which is allowed. Therefore, the actual call of Piece. capture (Piece) should output "I'm bored". Why is it "Knight is bored? As mentioned above, p2 actually points to an object of the Knight class, and in the Knight class, Ovriride has this Piece. capture (Piece) method, that is, Knight. capture (Piece), so you should call this method to output "Knight is bored ".

 

Now let's comment out the Override method and see how it works?

1 public class Piece {2 public static void main (String [] args) {3 Piece p2 = new Knight (); // static Piece, dynamic Knight 4 Knight p1 = new Knight (); 5 p1.capture (p2); 6 p2.capture (p1); 7} 8 public void capture () {9 System. out. println ("Capture"); 10} 11 public void capture (Piece p) {12 System. out. println ("I'm bored"); 13} 14} 15 class Knight extends Piece {16 // public void capture (Piece p) {// override the Method 17 in Piece // System. out. println ("Knight is bored"); 18 //} 19 public void capture (Knight k) {20 System. out. println ("Too slow buddy"); 21} 22}

The output result is:

I'm boredI'm bored

Analysis: The second output has just been explained. Because there is no override method, you can directly call the Piece. capture (Piece p) method in the Piece class. So what is the first output? P1.capture (p2), that is, Knight. capture (Piece), which cannot be called here. capture (Knight k) method, because Knight k = p2, this is illegal, Java does not allow the reference of the parent class to be assigned to the reference variable of the subclass. In this case, isn't this method available in the Knight class? Do not forget that the Knight class inherits from the Piece class, that is, the Knight class can also call methods in the Piece class, that is, Piece. the capture (Piece) method, so the output is "I'm bored ".

 

Now we know that the special character of Piece p2 = new Knight () is that there is a method to find whether override exists in the subclass. If so, the override method in the subclass is called, if not, use the methods in the parent class. If it is defined as follows: Piece p2 = new Piece (), no matter whether there is an override method in the subclass, it will not call the subclass method, but directly call the parent class method. As follows:

1 public class Piece {2 public static void main (String [] args) {3 // Piece p2 = new Knight (); // static Piece, dynamic Knight 4 Piece p2 = new Piece (); 5 Knight p1 = new Knight (); 6 p1.capture (p2); 7 p2.capture (p1); 8} 9 public void capture () {10 System. out. println ("Capture"); 11} 12 public void capture (Piece p) {13 System. out. println ("I'm bored"); 14} 15 public void capture (Knight p) {16 System. out. println ("Hello"); 17} 18} 19 class Knight extends Piece {20 public void capture (Piece p) {// here override the Method 21 System in Piece. out. println ("Knight is bored"); 22} 23 public void capture (Knight k) {// here override the Method 24 System in Piece. out. println ("Too slow buddy"); 25} 26}

The output result is:

Knight is boredHello

We can see that, even in the Knight class override. capture (Knight) method. Since p2 has always been a Piece class and does not call the method in the Knight class, p2.capture (p1), that is, Piece. capture (Knight), output "Hello ". If the fourth line is changed to Piece p2 = new Knight (), the output is:

Knight is boredToo slow buddy

The second output calls the override method in the Knight class.

 

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.