What is the difference between virtual method and abstract method? [Resolved]

Posted by Shreedar under C# on 8/23/2015 | Points: 10 | Views : 281 | Status : [Member] | Replies : 1
Why we should go for virtual method instead of abstract method.
What is the use by declaring a method as virtual?

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com



Responses

Posted by: Sheonarayan on: 8/23/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Virtual method is like a normal method that had declaration as well as its implementation and derived class (child class) has an option either to use that method directly or override this method with the new implementation.

Abstract method has only definition (like interface methods) and it forces derived class (child class) to implement this method by writing override keyword.


public abstract class A
{
public abstract int AbstractMethod(int i);

public virtual int VirtualMethod(int i)
{
return i;
}
}

public class B : A
{
// Mandatory
public override int AbstractMethod(int i)
{
return i;
}

// optional - can be ignored
public override int VirtualMethod(int i)
{
return i;
}
}

Hope this is clear now.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Login to post response