7

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?
}
Community
  • 1
  • 1
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • @dan > bad time to ask this great question. most of the C# gods will be on vacation – naveen Dec 31 '10 at 17:37
  • @naveen: Ha, then perhaps I'll give it a bump sometime in 2011 ;) – Dan Tao Dec 31 '10 at 17:39
  • 1
    @Dan, take a look at this one. http://stackoverflow.com/questions/3626958/c-why-cant-my-public-class-extend-an-internal-class – Anthony Pegram Dec 31 '10 at 21:37
  • @Anthony: That is indeed the same question. What do you think, should we close this one? The only possibly valuable distinction I notice is that I've given a moderately detailed description of alternative behavior I might expect if the language were different (which I embarrassingly didn't realize apparently describes how some other languages *already* work). – Dan Tao Dec 31 '10 at 21:48
  • @winwaed: This is a weird question, but... was my original wording wrong? I'm not disputing the edit so much as just being curious. – Dan Tao Dec 31 '10 at 21:53
  • @Dan, the questions are the same, so closing as a duplicate seems a reasonable choice. The man with the ability to find the "why" of the decision not to do it with C#/.NET responded to the other question, and maybe he'll blog about it if a further discussion is desired/warranted. – Anthony Pegram Dec 31 '10 at 21:53
  • @Anthony: Yeah, you're right. Incidentally, it seems a bit strange to me that I would need 5 votes to close this if I'm the OP. – Dan Tao Dec 31 '10 at 21:58
  • @Dan just minor grammar - don't worry about it, your meaning was clear – winwaed Jan 01 '11 at 02:05
  • @Dan: Your original title was correct. The derived class inherits the base class, the base class is inherited from by the derived class. The only thing I can say about Dan's usage is that maybe he was thinking of you, the programmer, as the subject of the verb, which is a valid usage of "derive" but not "inherit". – Ben Voigt Jan 01 '11 at 03:28

3 Answers3

4

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

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.

k rey
  • 611
  • 4
  • 11
1

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?

naveen
  • 53,448
  • 46
  • 161
  • 251