2

Suppose I have the following classes in a library

public abstract class HiddenBaseClass
{
    //stuff
}
public abstract class ClassA : HiddenBaseClass
{
    //stuff
}
public abstract class ClassB : HiddenBaseClass
{
    //stuff
}

I want to prevent HiddenBaseClass from being inherited outside the library, but I do want ClassA and ClassB to be inherited. I cannot make HiddenBaseClass an internal class, because that would mean HiddenBaseClass is less accessible than ClassA and ClassB.

Is there a way around this?

5
  • The scenario is very meaningless! Dec 26, 2020 at 18:14
  • 1
    If you can inherit from ClassA, then you also inherit from HiddenBaseClass, so your requirements are in conflict. What problems does inheriting from HiddenBaseClass cause that you want to avoid?
    – D Stanley
    Dec 26, 2020 at 18:15
  • Because I do not want to have to copy & paste code between ClassA and ClassB, but I also have no reason to design HiddenBaseClass in such a way to support being inherited by the user.
    – user10642930
    Dec 26, 2020 at 18:21
  • While it's possible to design your code like this, as per my answer, I'd suggest using composition rather than inheritance where you can. We can't really tell whether that would help here, but it's something to consider.
    – Jon Skeet
    Dec 26, 2020 at 18:22

2 Answers 2

5

Contrary to the comments on the question, I believe this scenario can make sense, if HiddenBaseClass has aspects that you need to rely on being implemented internally (because you trust the internal implementations), but expose other abstract operations for external code to implement. It may not be an appropriate design for your use case, but it's not unreasonable.

One simple way to make it impossible to inherit from it directly outside the same library is to give it an internal constructor:

public abstract class HiddenBaseClass
{
    // Only classes in the same assembly can chain to this constructor.
    internal HiddenBaseClass() {}
}

So long as all the constructors in the class are internal (or private protected, or private) that will prevent classes in other assemblies from chaining their constructors to the base class constructors, thus preventing inheritance.

3
  • He can also define an interface and mark it as internal. In that case he can keep the abstract class as public. Dec 26, 2020 at 18:24
  • 2
    @Transcendent: That wouldn't help with code reuse though. My answer does still keep the abstract class public - it's only the constructor that's internal.
    – Jon Skeet
    Dec 26, 2020 at 18:27
  • Of course Sir Jon Skeet! :) I was only trying to suggest an alternative way. I don't think there's anyone out there daring to prove you (the Chuck Norris of Programming) wrong. Dec 26, 2020 at 18:29
0

As the comments so politely pointed out, this isn't the best structure for this kind of scenario. In the end I'm going for an implementation like this:

internal sealed class HiddenBaseClass
{
    //stuff
}
public abstract class ClassA
{
    internal HiddenBaseClass { get; set; }
    //stuff that uses HiddenBaseClass
}
public abstract class ClassB
{
    internal HiddenBaseClass { get; set; }
    //more stuff
}

I apologise for asking the wrong questions yet again.

6
  • 1
    sealed classes cannot be inherited! Dec 26, 2020 at 18:38
  • I'm not inheriting the sealed class am I? Edit: oh nevermind that was an oversight, I edited it.
    – user10642930
    Dec 26, 2020 at 18:40
  • You code does not compile, did you try to test it before posting the answer? Dec 26, 2020 at 18:41
  • I accidentally inherited the class when I didn't mean to. I have edited the answer.
    – user10642930
    Dec 26, 2020 at 18:43
  • 1
    Keep you code as in the question you posted, just add this internal HiddenBaseClass() {} (as Jon Skeet's answer)
    – Ergis
    Dec 26, 2020 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy