Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an abstract method and a virtual method?

What is the difference between an abstract method and a virtual method? In which cases is it recommended to use abstract or virtual methods? Which one is the best approach?

like image 531
Moran Helman Avatar asked Dec 24 '08 14:12

Moran Helman


People also ask

What is difference between abstract method and virtual method in C#?

The C# programming language provides support for both virtual and abstract methods, each of which has distinct advantages. You use virtual methods to implement late binding, whereas abstract methods enable you to force the subclasses of the type to have the method explicitly overridden.

What is the difference between virtual and abstract class?

The basic difference between a virtual and abstratc class is that methods in virtual class CAN be overridden in derived classes, while abstract class methods MUST be overridden. Hope this helps! virtual classes do not require any methods to be overriden and can be constructed as is without the need of extending.

Is abstract method a virtual method?

An abstract method is implicitly a virtual method. Abstract method declarations are only permitted in abstract classes. public abstract void MyMethod(); The implementation is provided by a method override, which is a member of a non-abstract class.


1 Answers

An abstract function cannot have functionality. You're basically saying, any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class.

A virtual function, is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.

like image 99
BFree Avatar answered Oct 07 '22 10:10

BFree