Reputation: 61
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?
Upvotes: 0
Views: 1233
Reputation: 936
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.
Upvotes: 1
Reputation: 13222
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.
Upvotes: 0
Reputation: 20254
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.
Upvotes: 2