user4147874
user4147874

Reputation: 61

Instance variable in derived class has the same name with private instance variable of superclass?

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

Answers (3)

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

brso05
brso05

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

codebox
codebox

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

Related Questions