Is it possible to have an instance variable in derived class that has the same name with a private instance variable in the superclass? In other words, can I override a private instance variable or private method?
Asked
Active
Viewed 1,019 times
0
-
Why don't you try it? – Sleiman Jneidi Oct 31 '14 at 13:46
-
yes, this is possible. Why don't you just try? – L.Butz Oct 31 '14 at 13:46
-
No Way, Clear your Concepts. – Pratik Butani Oct 31 '14 at 13:47
-
possible duplicate of [Access a private variable of the super() class in Java - JChart2D](http://stackoverflow.com/questions/3603405/access-a-private-variable-of-the-super-class-in-java-jchart2d) – L.Butz Oct 31 '14 at 13:48
-
no, those are not the 'other words'. It is possible to deduct what you're asking, but the formulation is quite misleading for people who do not know the answer. – Danubian Sailor Oct 31 '14 at 14:26
3 Answers
2
Yes it is possible to declare such a variable, because the variable in the super class is private it cannot be seen in the child class and so there is no conflict.
But note that this is not the same as 'overriding', where an externally visible member is hidden by one with the same name in a child class.

codebox
- 19,927
- 9
- 63
- 81
1
You cannot override private method or variable from super class in sub class. What you can do is create a new variable or method in the sublclass with the same name.

brso05
- 13,142
- 2
- 21
- 40

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- 926
- 10
- 34
0
You can't override a private instance variable or method from a superclass but you can create a new instance variable or method in the derived class with the same name.

brso05
- 13,142
- 2
- 21
- 40