Private methods in Inheritance

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

    To understand this question you can relate private method to member variable of super class and sub class.

    So we know member variable is not going to be overridden in sub class.

    For example:

    Class A{
     int i = 10;
    }
    
    Class B extends A{
      int i = 11;
    }
    
    Class C extends A {
       int i = 12;
    }
    
    A a1 = new B();
    print(a1.i) // Will print 10
    
    A a2 = new B();
    print(a2.i) // Will print 10 
    

    Similar way when there is no inheritance reference variable super class is going to be considered.

提交回复
热议问题