Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every abstract function virtual in C#, in general?

Tags:

I was looking at Stack Overflow question What is the difference between abstract function and virtual function?, and I was wondering whether every abstract function should be considered to be a virtual function in C# or in general?

I was a bit puzzled by the "you must override/you may override" responses to that question. Not being a C# programmer, I tend to think that abstract functions are a compile-time concept only, and that abstract functions are virtual functions by definition since you must provide at least one but can provide multiple implementations further down the hierarchy.

Virtual functions have a compile-time dimension too, in that you cannot override a non-virtual function, but they are mostly a runtime concept since it is "just" the selection of the correct method implementation based on the actual receiver.

like image 556
eljenso Avatar asked Dec 24 '08 14:12

eljenso


People also ask

Is abstract method virtual?

Yes, abstract methods are virtual by definition; they must be overridable in order to actually be overridden by subclasses: When an instance method declaration includes an abstract modifier, that method is said to be an abstract method.

Can abstract class have non virtual function?

Abstract classes (apart from pure virtual functions) can have member variables, non-virtual functions, regular virtual functions, static functions, etc.

Is a virtual function an abstract function?

Any class that has at least one pure virtual function is known as an abstract class. An abstract class cannot be instantiated (i.e. you cannot build an object of this class type).

Is virtual function same as abstract class?

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.


1 Answers

Yes. From section 10.6.6 of the C# 3.0 spec:

When an instance method declaration includes an abstract modifier, that method is said to be an abstract method. Although an abstract method is implicitly also a virtual method, it cannot have the modifier virtual.

like image 185
Jon Skeet Avatar answered Sep 26 '22 04:09

Jon Skeet