开发者

Why can a public class not inherit from a less visible one? [duplicate]

开发者 https://www.devze.com 2023-02-02 03:02 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: C#: Why can't my public class extend an internal class?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

C#: Why can't my public class extend an internal class?

I apologize if this question has been asked before. I've searched SO somewhat and wasn't able to find it.

I'm just curious what the rationale behind this design was/is. Obviously I understand that private/internal members of a base type cannot, nor should they, be exposed through a derived public type. But it seems to my naive thinking that the "hidden" parts could easily remain hidden while some base functionality is still shared and a new interface is exposed publicly.

I'm thinking of 开发者_运维技巧something along these lines:

Assembly X

internal class InternalClass
{
    protected virtual void DoSomethingProtected()
    {
        // Let's say this method provides some useful functionality.
        // Its visibility is quite limited (only to derived types in
        // the same assembly), but at least it's there.
    }
}

public class PublicClass : InternalClass
{
    public void DoSomethingPublic()
    {
        // Now let's say this method is useful enough that this type
        // should be public. What's keeping us from leveraging the
        // base functionality laid out in InternalClass's implementation,
        // without exposing anything that shouldn't be exposed?
    }
}

Assembly Y

public class OtherPublicClass : PublicClass
{
    // It seems (again, to my naive mind) that this could work. This class
    // simply wouldn't be able to "see" any of the methods of InternalClass
    // from AssemblyX directly. But it could still access the public and
    // protected members of PublicClass that weren't inherited from
    // InternalClass. Does this make sense? What am I missing?
}


What you're really asking for is a restricted form of non-public inheritance. In .NET, the base class is considered part of the interface, not an implementation detail. If you want to reuse another class in your implementation, you can of course use composition instead of inheritance.

Also note that implementing an interface with lesser visibility/accessibility IS allowed. The restriction you're asking about applies to base classes only.


When a class inherits from a base class, the base members become part of the inherted class. In order to maintain this concept, the visibility scope of the base class must be equal or better than the inhertied class.


I understand internal like this.

Let me create a system.
In that system, you are studying at College-X and you have an internal exam on 2.0 that I created.
I mark that question paper as internal.

I can make the question paper

  1. public - post it in internet
  2. protected - post it in faculty mail group
  3. private - place it at a swiss vault

Suppose two students at College-Y, say Jon and Eric, with PhD in 5.0 are also in this system.
The question paper(object) is irrelevant to them.
Even if it's on internet (public), they should not be able to use it since its of no use to them.
Thats why I marked it internal initially.

Otherwise why should there be an acceess-modifier called internal?

0

精彩评论

暂无评论...
验证码 换一张
取 消