java - Accessing private instance variables of parent from child class? -
Assume that we have a class foo , which has a private instance variable bar Is .
We now have another class, baz , which increases foo . There can be non-static methods in baz access foo variable bar , if an accelerator method is defined in foo is not ?
I am working along the way in Java.
No, not accordingly:
6.6. 8 Example: Private Sector, Methods, and Constructors
A private class member or producer is only accessible within the body of the top level class (§7.6) which is attached to the announcement of the member Does it or the constructor have not inherited it by subclasses.
But regardless of the restriction of this language, you can :
area privateStringField = MyClass class.getDeclaredField ("PrivateString"); PrivateStringField.setAccessible (true); String field value = (string) privateStringField.get (private object); System.out.println ("fieldValue =" + Field Value);
Comments
Post a Comment