SITUATION: Say I have a class A,a class B which extends A and a class C which extends B.class A has a method hello() which is overridden in class B.
EVENT: Now class C calls hello().
QUESTIONS: Which version will be called?i guess it will be of the class just above in the hierarchy, i.e. B. In any case, how to access the different versions of hello() from C?
RELATED QUESTIONS: By using super we can access the version just above in the hierarchy but how to access the ones higher up the hierarchy? For example, how to access A’s hello() from C?
You can’t. Only code in
Bcan usesuperto callA.hello(); and if A, B, and C were all to implementhello(), it would be impossible for any code in C to accessA.hello().In general, code in any class can call its own version of a method, and the version of its immediate superclass, and that’s it. There’s no way to call any others without the cooperation of the superclass.