Java code snippet output explanation required

后端 未结 4 1359
北恋
北恋 2020-12-03 02:03

My code is:

class Foo {
  public int a=3;
  public void addFive() {
    a+=5;
    System.out.print(\"f \");
  }
}

class Bar extends Foo {
  public int a=8;
         


        
4条回答
  •  無奈伤痛
    2020-12-03 02:24

    Since you are doing f.a you will get the value of a from the class Foo. if you had called a method to get the value, e.g getA() then you would have gotten the value from the class Bar.

提交回复
热议问题