Difference between Abstract and virtual method

Posted by Akiii under C# on 1/12/2012 | Points: 10 | Views : 29113 | Status : [Member] | Replies : 8
What is the difference abstract methods vs virtual methods in C# ?

Replies with examples are appreciated !

Thanks and Regards
Akiii




Responses

Posted by: Hmanjarawala on: 1/12/2012 [Member] Bronze | Points: 25

Up
0
Down
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

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 1/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi, thank you for your reply,
If the virtual method is not compulsory to implement in the derived class then how we will call the base class method ? We have to type-cast the derived class reference variables to the base class ?
On the contrary, i want to have a default implementation in the base class ! I don't want anybody to override that method !

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 1/12/2012 [Member] Bronze | Points: 25

Up
0
Down
as you know virtual method either be public or be protected.
in both case if don't override it, default implementation that was written in base will work for derived also.
you need not to type case derived class object with base class objects. you can call that method from derived class's objectalso.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 1/12/2012 [Member] Bronze | Points: 25

Up
0
Down
So you are saying that directly if we create the object of a derived class and call the base class virtual method, it will work with default implementation ?

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ivlnr on: 1/12/2012 [Member] Starter | Points: 25

Up
0
Down
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.

abstract == MustOverride
virtual == Overridable


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 1/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi @Ivlnr,
Thanks for ur answer.

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 1/14/2012 [Member] Bronze | Points: 25

Up
0
Down
yes akiii

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 1/14/2012 [Member] Bronze | Points: 25

Up
0
Down
Thanks Himanshu for ur help....

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response