Search This Blog

Tuesday 12 August 2014

Difference between an abstract method & virtual method

1.Abstract method are those which are not defined. Means if your method doesnt have its body it is abstract.
 
On the other hand, if you have already defined the body and want the classes that derives it can override its member, if they wish, you define it as virtual. 
 
The VB.NET equivalent to these keywords gives a clear knowledge on this :
 
abstract == MustOverride
virtual == Overridable


2.An Abstract method must be override in child classes where as virtual method is not compulsory to override.An abstract method doesn't have implementation detail where as virtual method has it. only abstract class can haveabstract method, any class can have virtual method

3.Abstract method are by default empty.
Abstract method must be override in all derived classes.
A class in which Abstract method have must be abstract.
Eg- Abstract void Getdata();
It must be end with (;) in Abstract class. 
 
Virtual method may or may not have body by default.
Virtual method may or may not be override it depends on user.
Eg- Virtual void Getdata() {..........}



AbstractVirtualNo Keyword
Can have implementation? NoYesYes
Can override?MustCan but not a mustYou can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete class?overrideoverrideNo keyword needed
If an object is created of the base class type, which method will be executed?Concrete implementationThe parent implementation will be called only if no implementation is provided in the concrete classParent implementation
If an object is created of the concrete class type, which method will be executed?Concrete implementationConcrete implementationThe parent implementation will be called only if no implementation is provided in the concrete class

No comments:

Post a Comment