Reputation: 9043
I am learning C# and Java and have a question in regards to inheritance.
Is it possible to override private members in a superclass (base class)? In my view, it would not be correct as the access modifier would prevent the member from being accessed.
Upvotes: 11
Views: 27240
Reputation: 442
The answer is NO! You may want to look into using the 'protected' keyword.
See also, 'internal protected' for non-methods, if working with multiple assemblies.
See also, 'private protected' for abstract methods within your base class for example.
Then in your derived class, you can hide those methods from that of which is outside your derived class.
There is a way to do it via reflection or expression trees, but its explicit and it requires some work.
You can do this similarly with properties, fields, ect... as shown here.
BTW, whomever keeps using the word 'final' is a JAVA nerd. There is no 'final' keyword in C#, so your best option is to use 'sealed'.
Upvotes: 0
Reputation: 80
Use protected
instead privated
, protected make visible a method only for his childs
Upvotes: 4
Reputation: 72294
No, you can't override private
elements, they're effectively final (because they're never visible from a subclass to be overriden.)
You can declare private
elements with the same name in the subclass, but that's not overriding the one in the superclass - it's just another private
method with the same name as the one in the superclass.
Upvotes: 5
Reputation: 583
This is not silly question but it gives another concept of hiding variable.
Field in Java are only hidden and not actually overridden (that doesn't mean that we'll get a compilet time error while trying this, instead they are not overridden in its true sense). Overriding means the member should be invoked based on the run time type of the object and not based on the declared type. But binding for fields in Java is always static and hence it's based on the declared type of the object reference only. Read more about Static Binding in article - Dynamic Binding vs Static Binding >>
In case of methods, only those methods are overriden which are inherited and hence static methods are also not overridden but hidden only and they follow Static Binding only. private members (methods or fields both) are neither hidden nor overridden. They also follow Static Binding and they can not be accessed directly from any other class (including sub classes) except the class which have them. Remember, Hidden doesn't mean here we can't access the members from the subclass. So, don't confuse with being not accessible (in case of private members - fields or methods) and being hidden.
Upvotes: 6
Reputation: 2790
if the both, superclass and subclass in the same package you can just override methods which are not marked as PRIVATE and FINAL, if they are in the different package then you can just override methods which are PUBLIC or PROTECTED(Also they shouldn`t be FINAL).
Upvotes: 2
Reputation: 8245
Technically, you are not overriding it.
If a subclass has a member with a name, and the superclass has a member with the same name that is marked as private, they are two different members.
Upvotes: 4
Reputation: 240900
private
methods of a class are not visible in its child class so they won't get inherited.
Upvotes: 5