Inheritance private fields. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Inheritance private fields.

Hey, When a parent class has a private field and i extend this class, does the sub class inherit this private field because if i try and give the sub class the same variable name "private String name;" and call it, it says that the sub class cannot access private field in parent 🤔 how does sub class even know it parent has a name if its private 😅? Thanks 😉

23rd Mar 2019, 7:32 PM
D_Stark
D_Stark - avatar
16 Answers
+ 14
D_Stark The issue in your code is you are attempting to access private fields from outside the object instance, not from within the object instance. See my modified version showing this working with the private name field in both parent and child classes. https://code.sololearn.com/cacqZEkRRzXb/?ref=app
23rd Mar 2019, 8:02 PM
David Carroll
David Carroll - avatar
+ 11
Class members delcared as private are not accessible to sub-classes. The class members would need to be declared as protected or public to be accessible by sub-classes.
23rd Mar 2019, 7:38 PM
David Carroll
David Carroll - avatar
+ 10
David Carroll Thanks friend, i understand that the sub class cant access private fields of its parent class its when the sub uses the same field name im not sure what its doing 🤔 please see this example. Thanks https://code.sololearn.com/cJ5Zeq8TOucS/?ref=app
23rd Mar 2019, 7:43 PM
D_Stark
D_Stark - avatar
+ 9
David Carroll Bartas Dausynas Sorry guys i was confusing myself i have just realised my big mistake iforgot about getters and setters without realising lol...i think i need a break 😅🔫
23rd Mar 2019, 9:47 PM
D_Stark
D_Stark - avatar
+ 8
Bartas Dausynas It's great you provided a C++ perspective. Either way, hopefully you learned something new about Java. Truth-be-told, I'm relatively stronger in C# and Javascript. However, I enjoy answering questions about C++, Java, Python, and anything really because doing so forces me to validate my understanding and strengthen my awareness of commonalities and differences across the different languages and tech stacks. 🤓 So... by all means, feel free to jump out of your comfort zone and make an attempt to apply your knowledge in C++ to help others while learning about different languages. 👍
23rd Mar 2019, 9:40 PM
David Carroll
David Carroll - avatar
+ 7
Bartas Dausynas It's cool how similar our code samples are for this question. Granted, I started with a copy of D_Stark's code. But still, we both introduced the public getter. 🤓
23rd Mar 2019, 9:46 PM
David Carroll
David Carroll - avatar
+ 6
Basil David Carroll Bartas Dausynas I think its the diffrence between these 2 instances which has confused me. Parent x = new Sub(); Sub y = new Sub(); Thanks
23rd Mar 2019, 8:01 PM
D_Stark
D_Stark - avatar
+ 6
D_Stark This is a good reminder how sometimes we can overlook something we normally wouldn't have and how great it is to have other sets of eyes to help clarify the oversight. I did enjoy the dialog with other great people in the community. 👌
23rd Mar 2019, 9:50 PM
David Carroll
David Carroll - avatar
+ 6
David Carroll i definitly overlooked this one my brain wouldent except that your version had the parts i was missing 😆 i wont be making that mistake again 😁 ty
23rd Mar 2019, 9:57 PM
D_Stark
D_Stark - avatar
+ 6
Basil Ah yes... This is the preferred approach by utilizing encapsulation to access the private members. If this was your original meaning, then I now understand and concur. 👍
23rd Mar 2019, 10:03 PM
David Carroll
David Carroll - avatar
+ 6
DecodesWorm Olamilekan I believe you are describing a getter which others have already referred to.
25th Mar 2019, 1:42 AM
Sonic
Sonic - avatar
+ 6
Sonic I think people see the question and just answer without reviewing the other answers. Or, they just want to share their knowledge even if it's already been said several times already. Who knows? 🤷‍♂️
25th Mar 2019, 3:35 AM
David Carroll
David Carroll - avatar
+ 5
Bartas Dausynas Sorry, I misunderstood your original explanation. Yes... you and I share the same understanding. The original question was based on compile errors resulting from a separate issue in the code. So that caused some confusion for us both. 😉 I've revised my previous comment and only direct my explanation to Basil for clarification. The original code attempted something similar to the following: class A private String _name class B extends A private String _name class Program void Main() A a = new A() A b = new B() B c = new B() a._name //Cannot access _name b._name // here because they are c._name // declared private.
23rd Mar 2019, 8:56 PM
David Carroll
David Carroll - avatar
+ 3
Basil The only cases I'm aware of where private is accessible outside the immediate class is in Python. But that's because it's not really private in the way static typed languages are. Otherwise, private is accessible only within the class where the member is declared and not accessible by sub-classes. [Edited: Removed reference to Bartas Dausynas due to my misunderstanding of his explanation.]
23rd Mar 2019, 8:37 PM
David Carroll
David Carroll - avatar
+ 2
Did u know you can access the private field of the patent class? Incase you don't know ;declare a public method inside the parent class to access the private field then call the public method from the subclass. Well its been long I wrote code in Java but in Javascript accessing such private field you will use self::field.
24th Mar 2019, 11:45 AM
Decodeworms Olamilekan
Decodeworms Olamilekan - avatar
0
A better way is to create a public function in the same class as the private one and access the private function so that you can access the private one using the public one.
25th Mar 2019, 3:27 AM
Anshumaan Mishra