Why is the public class not inherited from the less visible?

Possible duplicate:
C #: Why can't my public class extend the inner class?

I apologize if this question was asked before. I searched SO several times and could not find it.

I'm just curious what was the key to this design. Obviously, I understand that the private / internal members of the base type cannot and should not be exposed through the derived public type. But it seems like my naive thinking is that the β€œhidden” parts can easily remain hidden, while some basic functions are still shared and the new interface is publicly revealed.

I am thinking of something like that:

Build x

internal class InternalClass { protected virtual void DoSomethingProtected() { // Let say this method provides some useful functionality. // Its visibility is quite limited (only to derived types in // the same assembly), but at least it there. } } public class PublicClass : InternalClass { public void DoSomethingPublic() { // Now let say this method is useful enough that this type // should be public. What keeping us from leveraging the // base functionality laid out in InternalClass implementation, // without exposing anything that shouldn't be exposed? } } 

Build 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? } 
+7
source share
3 answers

What you are really asking for is a limited 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 an interface with less visibility / accessibility is allowed. The restriction you are asking for applies only to base classes.

+4
source

When a class is inherited from the base class, the base members become part of the inherited class. To preserve this concept, the scope of the base class must be equal or better than the inherited class.

+2
source

I understand the inside like this.

Let me create a system.
In this system, you study at College-X, and you have an internal exam for 2.0 that I created.
I mark this question as internal.

I can make question paper

  • public - post it online
  • protected - post it to faculty group mail
  • private - put it in swiss repository

Suppose two students at college Y, say John and Eric, with a PhD of 5.0 are also in this system.
The thematic question (object) is not related to them.
Even if it is on the Internet (publicly available), they will not be able to use it because they do not need it.
That is why I marked this internal from the beginning.

Otherwise, you should specify an acceess-modifier called internal ?

+1
source

All Articles