C# private (hidden) base class
Asked Answered
G

4

18

Is it possible to make a C# base class accessible only within the library assembly it's compiled into, while making other subclasses that inherit from it public?

For example:

using System.IO;

class BaseOutput: Stream           // Hidden base class
{
    protected BaseOutput(Stream o)
    { ... }

    ...lots of common methods...
}

public class MyOutput: BaseOutput  // Public subclass
{
    public BaseOutput(Stream o):
        base(o)
    { ... }

    public override int Write(int b)
    { ... }
}

Here I'd like the BaseOutput class to be inaccessible to clients of my library, but allow the subclass MyOutput to be completely public. I know that C# does not allow base classes to have more restrictive access than subclasses, but is there some other legal way of achieving the same effect?

UPDATE

My solution for this particular library is to make the base class public and abstract, and to document it with "Do not use this base class directly". I also make the constructor of the base class internal, which effectively prevents outside clients from using or inheriting the class.

(It's a shame, because other O-O languages let me have hidden base classes.)

Gallbladder answered 1/6, 2012 at 22:31 Comment(3)
why don't you make the constructor of BaseOutput internal so no external code can inherit from it?Cooley
@mikez: Yes, that's what I did, see the update above.Gallbladder
public class with internal constructor maybe a better option (sample in my answer)Pyrene
P
15

Unfortunately not. You can't derive a public class from an internal or private class.

You need to either expose the base class, or you need to declare all the methods for all of your similar classes. If you go the route where you declare all methods again, it's probably useful to create a helper class, which has the actual implementation of them. Still it's quite a bit of boilerplate.

Priddy answered 1/6, 2012 at 22:32 Comment(3)
Too bad. Java lets me do this.Gallbladder
Because it would have saved me quite a bit of boilerplate sometimes. For example when implementing the same interface in several classes.Priddy
The whole point is to put as many common methods and variables into the shared base class, so that you don't have to write every single one of them in every subclass. That's one of the more common uses of base classes, i.e., sharing implementation code instead of duplicating code unnecessarily.Gallbladder
W
5

Consider a pattern such as a Facade. That's what they're there for. I don't think you can achieve what you require with straight inheritance.

Weintraub answered 1/6, 2012 at 22:33 Comment(3)
I don't want to have to create a lot of forwarding methods. The whole point behind the hidden base class is to avoid replicating a lot of (common) methods.Gallbladder
indeed, one should favor composition over inheritance anyway (en.wikipedia.org/wiki/Composition_over_inheritance)Theme
@Theme - That is the exact opposite of my point. I want the base class to implement most of the methods and variables, and I want each derived subclass to implement as little as possible.Gallbladder
P
1

Depending on what "lot of common methods" are doing you may achieve some of it with internal extension methods:

internal static class MyStreamExtensions
{
   internal static int UsefulOne(this Stream stream)
   {
     return 42; 
   }
}   

Another approach is to make constructor internal to prevent unintentional derivation from that class:

public class BaseOutput: Stream
{
    internal BaseOutput(Stream o)
    { ... }

    ...lots of common methods...
}

This will make code more understandable compared to "not-really-visible" intermediate class in hierarchy.

Pyrene answered 1/6, 2012 at 23:3 Comment(0)
P
0

I think this will work: Make the base class public, and make all of the methods protected. It helps keep them from being available and negates the need to re-implement or wrappers.

Perpendicular answered 13/3, 2023 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.