After googling it, I found Marco had the same question:
public class MainProgram { public static void main(String[] args) { Beta b = new Beta(); // I want to invoke Alpha.foo() but // on the Beta instance. // I want to cancel the dynamic binding that // makes Beta.foo() get called instead // b.super.foo() <--- obviously doesn't compile } } class Alpha { public void foo() { System.out.println("inside Alpha.foo()"); } } class Beta extends Alpha { public void foo() { System.out.println("inside Beta.foo()"); } }
As discussed in this thread[1], "It is Beta's decision whether it wants to use Alpha's foo() or its own, not the calling code's. Allowing code outside the Beta class to influence the binding of its methods breaks the concepts of polymorphism and encapsulation."
In another discussion thread[2], "You can't call the super method in other objects - that would violate encapsulation. The whole point is that the object controls what its overridden methods do."
That is, there is no way to call an overridden method directly in Java.
沒有留言:
張貼留言