lfaraone
lfaraone

Reputation: 50672

Accessing private instance variables of parent from child class?

Let's say we have a class foo which has a private instance variable bar.

Now let us have another class, baz, which extends foo. Can non-static methods in baz access foo's variable bar if there is no accessor method defined in foo?

I'm working in Java, by the way.

Upvotes: 34

Views: 96404

Answers (11)

LulzSec
LulzSec

Reputation: 39

Without an accessor method, it's not possible. You have to use a getter setter to access private data members from the superclass.

Upvotes: 0

Rakhi jha
Rakhi jha

Reputation: 21

No,bar variable will not be accessible to baz. bar varible should be either protected or public .

Upvotes: 0

user12333979
user12333979

Reputation: 1

Private members exist(inherited) in instances of child classes .Since, object of Sub class is also an object of Super class, but it is not visible for the sub class

They are accessible indirectly through non-private methods of Super class. These methods can access and manipulate private members

Upvotes: 0

neeranzan
neeranzan

Reputation: 131

The private variable(s) of a class invariably has a scope inside that class. If it has to be shared among the subclasses, it should be declared "protected"

Upvotes: 0

Sushim Mukul Dutta
Sushim Mukul Dutta

Reputation: 777

To use a private variable of a super class in a sub class, an accessor method is required. Else use the protected modifier instead of private.

Upvotes: 2

George Armhold
George Armhold

Reputation: 31064

You cannot access private variables in descendent classes. Normally you'd want to use "protected" or "package" (the default) level access for this. However if you want to be really tricky, you can resort to using reflection and AccessibleObject to get at it. I wouldn't recommend doing that for production code unless you are really in a bind; for testing, etc., it's fine.

Upvotes: 1

Wim Coenen
Wim Coenen

Reputation: 66723

No, not according to the java language specification, 3rd edition:

6.6.8 Example: private Fields, Methods, and Constructors

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.

But regardless of this language restriction, you can access private fields through reflection:

Field privateStringField = 
   MyClass.class.getDeclaredField("privateString");
privateStringField.setAccessible(true);

String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);

Upvotes: 38

cort
cort

Reputation: 1126

...if there is no accessor method defined in foo?

You need accessors. Besides, take care of inheritance, Should that var really be in parent? Remember IS-A check..

Upvotes: 1

Sandro
Sandro

Reputation: 2259

For questions like this, where is a table found on the website here: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Basically you want public or protected variable to be declared in foo since these are the variables that subclasses inherit from their parent and therefore seen in baz.

Upvotes: 6

alphazero
alphazero

Reputation: 27234

Child classes can not access private members (which is the whole point of private access control).

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281405

No, for that you should use protected.

Upvotes: 24

Related Questions