locked
Public class derived from internal abstract class? RRS feed

  • Question

  • I'd like to have an internal abstract base class, and then use it to create some derived classes that are made public. But the C# language does not seem to allow this - I get

     

    Error 2 Inconsistent accessibility: base class 'Merlinia.Common.MiniMessaging' is less accessible than class 'Merlinia.Common.MiniMessagingSL' 

     

    Is there some other way of doing this? I would prefer that the base class is not visible to users of the assembly, but that the derived classes are.

     

    Thanks.

     

    Rennie

    Saturday, March 8, 2008 2:25 PM

Answers

  •  BinaryCoder wrote:

    Making the constructors protected will prevent external users from making new classes that derive from the abstract class.


    Sorry, but this is not correct. Any class inheriting from the abstract class automatically has full access to protected methods. In fact only the abstract class itself and those classes inheriting from it have access to protected members, including constructors.

    The correct approach to ensuring that only your classes can inherit from this abstract class is to define a internal constructor (which is probably what BinaryCoder was thinking of). Classes outside of the abstract class' assembly cannot access internal constructors*, so cannot inherit from that class.

    * This isn't completely true as friend assembly classes can, but that isn't an issue here.
    Saturday, March 8, 2008 7:21 PM

All replies

  • You really can't do what you are describing.  However, you can make sure that your base class does not have Public constructors:  this will go a long way in preventing your base class from being misused.

     

     

    Saturday, March 8, 2008 3:59 PM
  • An abstract class cannot be istantiated so misuse is not a concern in this case.

    If it's just a matter of avoiding cluttering the intellisense, you might just put the base class in a separate namespace. Users will not see the class listed unless they explicitly import the namespace or fully qualify the class name.

     

    HTH

    --mc

    Saturday, March 8, 2008 5:33 PM
  • > An abstract class cannot be istantiated so misuse is not a concern in this case.

     

    Making the constructors friend will prevent external users from making new classes that derive from the abstract class.

     

    EDIT:  Corrected to say "friend" instead of "protected".  Thank you.

    Saturday, March 8, 2008 5:52 PM
  •  BinaryCoder wrote:

    Making the constructors protected will prevent external users from making new classes that derive from the abstract class.


    Sorry, but this is not correct. Any class inheriting from the abstract class automatically has full access to protected methods. In fact only the abstract class itself and those classes inheriting from it have access to protected members, including constructors.

    The correct approach to ensuring that only your classes can inherit from this abstract class is to define a internal constructor (which is probably what BinaryCoder was thinking of). Classes outside of the abstract class' assembly cannot access internal constructors*, so cannot inherit from that class.

    * This isn't completely true as friend assembly classes can, but that isn't an issue here.
    Saturday, March 8, 2008 7:21 PM
  • Thanks to all who answered. DavidArno's answer seems to fill the bill.

     

    Rennie

    Sunday, March 9, 2008 4:08 PM