Possible? Can you change the access of anything to anything else?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Nope.
A typical way to deal with this is to throw a
NotImplementedException
or something to that effect.No, you can hide a public member with a private method in a subclass, but you cannot override a public member with a private one in a subclass. And, in actually, it's not just a public/private thing, this applies to narrowing the access in general.
Revised: By hiding with a more restrictive access - in this case private access - you will still see the base class member from a base-class or sub-class reference, but it would defer to the new method when available from the new access level.
So in general, when you hide, the hide takes precedence when visible at its access level. Otherwise the original method is the one used.
So
SubClass.B()
ONLY hides the base class methods when it is accessible. That is, if you callSubClass.B()
inside ofSubClass
then it will take the hidden form ofB()
, but sinceB()
is private, it is not visible to classes outside of itself, and thus they STILL seeBaseClass.B()
.The long and the short of it is:
1) You cannot override a method to be more restrictive (access wise). 2) You can hide a method with a more restrictive one, but it will only have an effect where that new access type is visible, otherwise the base one stands.
You cannot narrow or widen the visibility of an overriden member. However, you can define another method with the
new
keyword, which can give you a new method with the same name, but which is incompatible in terms of polymorphism.