C# private (hidden) base class

12,163

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
12,163

Related videos on Youtube

David R Tribble
Author by

David R Tribble

Programming since 1978, back when programs had line numbers. B.S. in Mathematics, minor in Computer Science. C#, Java, SQL, JDBC, XML, HTML, CSS, JavaScript, TypeScript, ASP.NET, jQuery, AngularJS, Perl, JSP, JNI, sed, awk, Lisp, XEmacs, Emacs, vi, data structures, compiler design, parsing theory, lexical analysis, finite automata, language design, LR(k) grammars, lex, yacc, multiprocessing, database, digital fax, digital logic, artificial intelligence, SCCS, RCS, CVS, Subversion, Intel 8080, x86, 6502, MC 68000, IBM/370, z/390, digital logic, RTL, cryptography, encryption, AES, PKI, PGP, compression, FTP, SMTP, IPC, sockets, threads, network, client/server, Unix, POSIX, MS-DOS, Windows, Win32, TOPS-20, Apple II, TRS-80, MIDI, AI, graphics, 3D math, document storage, workflow, Unicode, EBCDIC, JCL, CICS, C++, C, PL/I, COBOL, Assembler, FORTRAN, BASIC, to name just a few. Knowledge of number theory, foundations, set theory, abstract algebra. Hobbies including photography and martial arts.

Updated on September 15, 2022

Comments

  • David R Tribble
    David R Tribble over 1 year

    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.)

  • David R Tribble
    David R Tribble almost 12 years
    Too bad. Java lets me do this.
  • David R Tribble
    David R Tribble almost 12 years
    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.
  • jeroenh
    jeroenh almost 12 years
    indeed, one should favor composition over inheritance anyway (en.wikipedia.org/wiki/Composition_over_inheritance)
  • CodesInChaos
    CodesInChaos almost 12 years
    Because it would have saved me quite a bit of boilerplate sometimes. For example when implementing the same interface in several classes.
  • David R Tribble
    David R Tribble almost 12 years
    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.
  • David R Tribble
    David R Tribble almost 9 years
    @jeroenh - 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.