Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a difference between abstract and virtual? [duplicate]

Tags:

c#

both abstract and virtual are going to be override in child class than whats a difference.

is it Virtual method have body and abstract is just a signature ????

like image 635
BreakHead Avatar asked Mar 16 '10 13:03

BreakHead


2 Answers

is it Virtual method have body and abstract is just a signature ????

Exactly. The point is that virtual methods can be overridden in derived classes, while abstract methods must be overridden. Likewise, a class that has at least one abstract method must itself be abstract, i.e. it cannot be instantiated directly since its implementation is (partially) missing.

Finally, every abstract method is also virtual by implication. virtual basically just means that the method is dispatched at runtime to the correct class, and so it can be overridden to implement runtime polymorphism.

like image 144
Konrad Rudolph Avatar answered Oct 21 '22 13:10

Konrad Rudolph


Abstract means you MUST override it. Virtual means you CAN override it. More or less.

like image 25
Etienne de Martel Avatar answered Oct 21 '22 11:10

Etienne de Martel