0
public class A {
       public String s="A";
       public static int i=1;
   }
public class B extends A {
      public String s="B";
      public static int i=2;
public static void main(String [] args) {
              A a= new B();
              B b= new B();
              System.out.print(a.s+a.i);
              System.out.print(b.s+b.i);
}
}

Why the output is "A1B2" instead of "A1B1"? Doesn't fields can't be overridden, and also static fields can't?

avivlevi
  • 13
  • 4

1 Answers1

1

It seems that you're confused with the concept of Overriding.

in Java, as far as class variables are concerned, you do not override them, you hide them.

Overriding is for instance methods. Hiding is for instance variables.

Both Hiding and Overriding are different.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • 1
    +1 just because this answer is totally correct and probably only was voted -1 by the question author, because he didn't understand or want to hear the answer... – Michael K Nov 24 '17 at 09:37