Reputation: 60184
Why declaration of a field in the subclass with the same name as the one in the superclass in not recommended? It is stated here. What this hiding can lead to? Thank you.
Upvotes: 0
Views: 79
Reputation: 1500385
Well, there are two issues here:
The first point is easily alleviated by simply keeping fields private, which is generally a good idea anyway. However, the second point is arguably more important. If you've got two fields both called name
(for example) in the same object, even if no single piece of code is aware of both fields, that suggests that either there are genuinely two kinds of name within that object, in which case it would be helpful for at least one of the field names to be more specific, or you've got two fields representing the same piece of state, which is very fragile, as well as inefficient. (It's all too easy to change one but not the other, for example.)
Upvotes: 3