Hidden fields though inheritance

前端 未结 3 2022
别那么骄傲
别那么骄傲 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:55

    Because declaring a variable doesn't inherit. You have two copies of x in the class, one in parent namespace, one in child namespace.

提交回复
热议问题