8

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"

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

4 Answers4

13

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 2
    If the question is "are abstract methods internally public and virtual in c#?" Then the answer is "no", they are "private" by default, and therefore will produce a compilation error if no access is given. C# does not sugar coat everything and make us work sometimes, but even then the compiler error messages do a pretty good job of holding our hand. – Les Oct 22 '10 at 12:09
5

Abstract is just a way to say: "I am here, but no one has told me what I'm going to do yet." And since no one has implemented that member yet someone must do that. To do that you have to inherit that class, and override that member.

To be able to override something it has to be declared either abstract or virtual, and must at least be accessible to the inheritor, i.e. must be marked protected, internal or public.

d219
  • 2,707
  • 5
  • 31
  • 36
Onkelborg
  • 3,927
  • 1
  • 19
  • 22
2

Abstract methods cannot be private and are virtual. They must be at least protected.

jaryd
  • 861
  • 12
  • 21
2

By virtue of Jon Skeet's argument here (What are the Default Access Modifiers in C#?)

The default access for everything in C# is "the most restricted access you could declare for that member"

It must be "protected"

As pointed out by Pieter default is always private, so:

abstract class Foo
{
    abstract void Bar(); 
} 

Gives compiler error

virtual or abstract members cannot be private

Community
  • 1
  • 1
veggerby
  • 8,940
  • 2
  • 34
  • 43
  • 2
    The default modified for abstract methods is also private, just like any other method. But, because this is illegal, leaving the modifier out gives a compilation error. – Pieter van Ginkel Oct 22 '10 at 12:01
  • Oh dear, that is so true: abstract class Foo { abstract void Bar(); } really doesn't compile "virtual or abstract members cannot be private" – veggerby Oct 22 '10 at 12:48