How does Java Determine methods call at runtime in polymorphism?

后端 未结 2 1739
清酒与你
清酒与你 2021-01-05 15:06

While the main principle of polymorphism is decoupling \"what from who\" in term of types, but what confuses me how does method-call mechanism finds out and cal

2条回答
  •  时光取名叫无心
    2021-01-05 15:22

    I think @JBNizet figured out the solution already in the comments (and my guess turned out to be wrong). But since he doesn't post it as an answer, I'll do it:

    The main method isn't supposed to show any dynamic behavior because it is always ever going to call that one single method TestRide.ride(Cycle c). There is no other possible method, so nothing to figure out.

    The dynamic method call is inside that method TestRide.ride(Cycle c). And now that you posted that code, indeed we see a dynamic method dispatch using invokevirtual. So, after all, no surprises. The dynamic method dispatch is there.

提交回复
热议问题