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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.)