0
Why can't we inherit private member of parent class to the derived class
11 Antworten
+ 4
if you want to access parent class member from child class but not from outside, you should use 'protected' instead...
+ 3
Can u clarify it with code
+ 3
You can indirectly using get and set methods you just cant access the private field directly
+ 1
visph yea got it!
+ 1
Karthik Reddy Thotamgari You cant directly access a private member in main or sub class.
+ 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?
+ 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?
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. :)
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
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
};
0
I think OP want only the child class able to access some parent member from code inside it, not from outside (from instances)