Private methods in Inheritance

后端 未结 9 1765
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  一向
    一向 (楼主)
    2020-11-29 04:59

    Since the reference type of the object obj is SuperClass, a call to doSomething() tries to access the private method defined in SuperClass itself (private methods cannot be overridden). As doSomething() is accessible within SuperClass, the main method can call doSomething() without giving any error/s.

    Hope this helps! :-)

提交回复
热议问题