Hidden fields though inheritance

前端 未结 3 2020
别那么骄傲
别那么骄傲 2020-12-19 02:06

In the following code example:

class Parent { 
    int x =5;
    public Integer aMethod(){

        System.out.print(\"Parent.aMthod \");
        return x;
          


        
3条回答
  •  星月不相逢
    2020-12-19 02:53

    Operations are always on Objects. In both cases, p.aMethod() and c.aMethod(), object is child that is why it printed 6 in both cases. When you directly access variable directly it reads variable associated with left side.

提交回复
热议问题