0

Why can't we inherit private member of parent class to the derived class

22nd Mar 2021, 2:14 PM
kreddyt
kreddyt - avatar
11 Antworten
+ 4
if you want to access parent class member from child class but not from outside, you should use 'protected' instead...
22nd Mar 2021, 2:30 PM
visph
visph - avatar
+ 3
Can u clarify it with code
22nd Mar 2021, 2:25 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 3
You can indirectly using get and set methods you just cant access the private field directly
22nd Mar 2021, 2:35 PM
D_Stark
D_Stark - avatar
+ 1
visph yea got it!
22nd Mar 2021, 2:32 PM
kreddyt
kreddyt - avatar
+ 1
Karthik Reddy Thotamgari You cant directly access a private member in main or sub class.
22nd Mar 2021, 2:57 PM
D_Stark
D_Stark - avatar
+ 1
Karthik Reddy Thotamgari not sure what you mean by methods are callable by anyone not just derived this sounds like static methods to me. can you post the link to stackflow it's not always correct. visph am I missing somthing here?
22nd Mar 2021, 3:11 PM
D_Stark
D_Stark - avatar
+ 1
D_Stark you're surely more experiemented than me at java... I guess Karthik Reddy Thotamgari want to be able to access directly parent class member from child class, but without allowing access from outside... by using set/get on parent child, the property can be accessed from outside (even if it is protected in a certain way)... or can we declare setter/getter as protected/private instead of public?
22nd Mar 2021, 3:24 PM
visph
visph - avatar
0
D_Stark yea that's to access from main I'm asking abt inheriting from the parent class to child class of private member. :)
22nd Mar 2021, 2:46 PM
kreddyt
kreddyt - avatar
0
D_Stark If you inherit privately, then the derived class does NOT have access to the Base's private members. Because the getters and setters are public -- they're callable by anyone, not just derived classes. They are included, but not inherited -stackoverflow
22nd Mar 2021, 2:47 PM
kreddyt
kreddyt - avatar
0
visph yeah if a variable is private then it's only accessible inside that class theres no way to get to it only by getters and setters inherited by the sub class as these are public this means those methods are accessible on the derived object in any class for example in the main method{ new derivedObject().publicSuperSetter(10).getPublicSuperPrivateValue(); works fine in any class };
22nd Mar 2021, 3:31 PM
D_Stark
D_Stark - avatar
0
I think OP want only the child class able to access some parent member from code inside it, not from outside (from instances)
22nd Mar 2021, 3:34 PM
visph
visph - avatar