Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do subclasses inherit private fields of their parent class? [discussion] #375

Open
clementyee303 opened this issue Apr 26, 2021 · 2 comments

Comments

@clementyee303
Copy link

So I came across this thread on StackOverflow which I found interesting. Perhaps someone would like to share their thoughts on the semantics of this question?

TLDR - Subclasses do not inherit private members of their parent class. By that definition, the subclasses themselves do not inherit the private fields of their parent class. However, the objects of subclasses do contain the private fields.

@cf6600
Copy link

cf6600 commented Apr 26, 2021

Confusing as the meaning of "inheriting" fields of their parent class is quite blur to me. I would like to think all subclasses should have the fields of their parent classes since these fields need to be instantiated with every instance of the subclass - object created. However, I think saying that they inherit the private fields is wrong, since I cannot access the field in my subclass if it is private in my parent class(without a getter method).

@deniselam10
Copy link

Thank you for starting this very insightful discussion! My perception of private fields is so that we would not be able to access this field from objects outside of the class, and we access that variable through a method instead (bringing in the concept of immutability). As such, i think that it is important for the subclasses to not inherit these private fields. That being said, they must also be able to access it since it is obviously a subclass of the parent class, thus the fact that they contain the private fields seem understandable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants