static method behaving like other method those can override

后端 未结 1 1982
不知归路
不知归路 2021-01-26 00:43

On object of child class, static methos of super class are available but when we define same method in child class, now object of child class start pointing to child class met

相关标签:
1条回答
  • 2021-01-26 01:14

    Since A extends B, an instance of A (which you create by calling new A()) will have all methods, which B has. Therefore, if you call .method() on an instance of A, the VM looks for a method() first in its own scope, i.e. the dynamic methods within A, then the dyanmic method within B, then the static methods within A and finally the static methods within B. This is possible because the VM allows accessing static methods via a this reference, although this is discouraged since it compromises readability.

    0 讨论(0)
提交回复
热议问题