Search code examples
c#abstract-classprivatepublicaccess-modifiers

c# abstract methods: internally public and virtual?


Are abstract methods internally public and virtual in c#?

All methods are, by default, private and if an abstract method is private, it will not be available to derived class, yielding the error "virtual or abstract members cannot be private"


Solution

  • I think you are asking a different question than most people think (in other words it seems like you understand what abstract means).

    You cannot declare a private abstract method - the compiler issues an error. Both of these classes will not compile:

    class Foo
    {
        private abstract void Bar();
    }
    
    class Baz
    {
        // This one is implicitly private - just like any other 
        // method declared without an access modifier
        abstract void Bah();
    }
    

    The compiler is preventing you from declaring a useless method since a private abstract member cannot be used in a derived class and has no implementation (and therefore no use) to the declaring class.

    It is important to note that the default access modifier applied to an abstract member by the compiler (if you do not specify one yourself) is still private just like it would be if the method was not abstract.