Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 821
NoviceToDotNet Avatar asked Oct 22 '10 11:10

NoviceToDotNet


2 Answers

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.

like image 99
Andrew Hare Avatar answered Nov 15 '22 21:11

Andrew Hare


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.

like image 29
Onkelborg Avatar answered Nov 15 '22 20:11

Onkelborg