java - when we are calling the overriden method addFive() tha b is printed as Bar object is created but why it is calling "a" in Boo Class.Output is b3 -
this question has reply here:
java code snippet output explanation required 4 answersclass boo { public int = 3; public void addfive() { += 5; system.out.print("f "); } } class bar extends boo { public int = 8; public void addfive() { this.a += 5; system.out.print("b " ); } public static void main(string[] args) { boo f = new bar(); f.addfive(); system.out.println(f.a); }
you don't override instance fields, hide them. so, when access instance field on boo
reference, 1 declared in boo
class only.
and when increment a
in bar
constructor:
this.a += 5;
it incrementing a
declared in bar
, since hiding field a
declared in boo
.
java
No comments:
Post a Comment