I have to say that it is not the public modiefier’s fault. You can use private as well. All of the confusing situation is connected with getter method – the lack of it. As everyone knows instance variables haven’t polymorphic behaviour. Only instance methods can be polymorphically invoked. If you used getter instead of f.a everything would be OK.
@Shooter: you are right, private would be a problem as well, but as a field being public, it can be accessed by simply doing Foo.a.
So being public is somewhat one of the issues here. Encapsulation is simply always good idea. Nevertheless naming fields with names already taken in super class can very quickly lead to problems and all good IDEs will tell you so
I gave correct answer! What did I win?
Roadtrip to Wroclaw
I have to say that it is not the public modiefier’s fault. You can use private as well. All of the confusing situation is connected with getter method – the lack of it. As everyone knows instance variables haven’t polymorphic behaviour. Only instance methods can be polymorphically invoked. If you used getter instead of f.a everything would be OK.
@Shooter: you are right, private would be a problem as well, but as a field being public, it can be accessed by simply doing Foo.a.
So being public is somewhat one of the issues here. Encapsulation is simply always good idea. Nevertheless naming fields with names already taken in super class can very quickly lead to problems and all good IDEs will tell you so
Noone’s going to reply and discuss?
Maybe there’s nothing more to say?
When I first got to know about it I was surprised that variables are not virtual. I’m wondering – why?
Did you ever find it in JLS?