Consider these classes:
class Parent {
int a;
}
class Child extends Parent {
int a; // error?
}
Should the declaration of a in Child not give compilation error due to multiple declarations of int a?
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.
child.ashadows (or hides)parent.a.It’s legal Java, but should be avoided. I’d expect your IDE to have an option to give you a warning about this.
Note, however, that this is only an issue because you’re already exposed a variable to the world. If you make sure that all your variables are private to start with (separating out the API of methods from the implementation of fields) then it doesn’t matter if both the parent and the child have the same field names – the child wouldn’t be able to see the parent’s fields anyway. It could cause confusion if you move a method from the child to the parent, and it’s not generally great for readability, but it’s better than the hiding situation.