Private methods in Inheritance

后端 未结 9 1753
隐瞒了意图╮
隐瞒了意图╮ 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 05:01

    When you used this line:

    Superclass obj = new Subclass();
    

    You casted Subclass into a Superclass Object, which uses only the methods of the Superclass and the same data. If you casted it back into a Subclass, you could use the Subclass methods again, like so:

    ((Subclass)obj).doSomething(); #prints "from Subclass"
    

提交回复
热议问题