Private methods in Inheritance

后端 未结 9 1791
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 04:19

Here\'s an interesting code snippet:

public class Superclass {

    public static void main (String[] args){
        Superclass obj = new Subclass();
                


        
9条回答
  •  猫巷女王i
    2020-11-29 04:55

    It works because you are casting to a Superclass from within a method of the Superclass. In that context, Superclass.doSomething is available to the compiler.

    If you were to change your super and subclasses to two different arbitrary classes A and B, not related to the class containing the main method, and try the same code, the compiler would complain about not having access to the method.

提交回复
热议问题