Alex Rivera | Logout

Does C# have the notion of private and protected inheritance?

Asked 2008-08-28T19:00:57.550
50

Does C# have the notion of private / protected inheritance, and if not, why?

C++


class Foo : private Bar {
 public:
   ...
 }; 

C#


public abstract NServlet class : private System.Web.UI.Page
{
    // error "type expected"
}

I am implementing a "servlet like" concept in an .aspx page and I don't want the concrete class to have the ability to see the internals of the System.Web.UI.Page base.

Edit
Report

2 Answers

23

C# allows public inheritance only. C++ allowed all three kinds. Public inheritance implied an "IS-A" type of relationship, and private inheritance implied a "Is-Implemented-In-Terms-Of" kind of relationship. Since layering (or composition) accomplished this in an arguably simpler fashion, private inheritance was only used when absolutely required by protected members or virtual functions required it - according to Scott Meyers in Effective C++, Item 42.

My guess would be that the authors of C# did not feel this additional method of implementing one class in terms of another was necessary.

answered 2008-08-28T19:30:30.787
-4

No it doesn't. What would the benefit be of allowing this type of restriction?

answered 2008-08-28T19:11:40.083

Your Answer