0

I know in Java, especially when you do field hiding, you are able to access grandparent's state variables:

((Grandparent) this).hiddenField

Why is the same rationale not the case for method calls? you can call super.method(), why not other ancestor's as well?

user1329572
  • 5,908
  • 5
  • 28
  • 39
Bober02
  • 14,688
  • 31
  • 87
  • 168

2 Answers2

1

Because methods are virtual, and fields are not. The goal was to call the same method regardless of the type of reference:

(Grandparent) this).method() always equals this.method().

Alexei Kaigorodov
  • 12,853
  • 1
  • 19
  • 36
1

There is no analog of field hiding for instance methods. Instance methods, unlike instance fields or static methods, are subject to dynamic dispatch and that's a whole different story. In a sense, an overridden method is just not "there" anymore, and the only exception is within the overriding method body, and there only the method being directly overridden.

Marko Topolnik
  • 188,298
  • 27
  • 302
  • 416