The difference between virtual and abstract methods

Here is the code from MSDN :

// compile with: /target:library public class D { public virtual void DoWork(int i) { // Original implementation. } } public abstract class E : D { public abstract override void DoWork(int i); } public class F : E { public override void DoWork(int i) { // New implementation. } } 

Can someone explain the above code regarding the differences between abstract and virtual methods?

+49
c # msdn
Feb 06 '13 at 12:09
source share
4 answers

Virtual methods have an implementation and provide derived classes with the ability to override them. Abstract methods do not provide an implementation and force derived classes to override a method.

So, abstract methods do not have real code in them, and HAVE TO subclasses override the method. Virtual methods can have code, which is usually the default implementation, and any subclasses MAY override the method using the override modifier and provide a custom implementation.

 public abstract class E { public abstract void AbstractMethod(int i); public virtual void VirtualMethod(int i) { // Default implementation which can be overridden by subclasses. } } public class D : E { public override void AbstractMethod(int i) { // You HAVE to override this method } public override void VirtualMethod(int i) { // You are allowed to override this method. } } 
+135
Feb 06 '13 at 12:23
source share

First of all, you should know the difference between a virtual and an abstract method.

Abstract method

  • The abstract method is in an abstract class and has no body.
  • The abstract method must be overridden in a non-abstract child class.

Virtual method

  • A virtual method can reside in an abstract and non-abstract class.
  • There is no need to redefine the virtual method in a derivative, but it can be.
  • The virtual method must have a body .... can be overridden by the override keyword .....
+32
Sep 24 '14 at 7:25
source share

the abstract method must be overridden by a call in the derived class, otherwise it will give a compile-time error and in the virtual you can or cannot override it, if it is good enough, use it

Example:

 abstract class twodshape { public abstract void area(); // no body in base class } class twodshape2 : twodshape { public virtual double area() { Console.WriteLine("AREA() may be or may not be override"); } } 
+2
Nov 08 '14 at 12:37
source share

Abstract Method:

  • If the abstract method is defined in the class, then the class should declare as an abstract class.

  • An abstract method should contain only the definition of the method, should not contain the body of the method / implementation.

  • A derived class must describe an abstract method.

Virtual method:

  • Virtual methods may move in a derived class, but not required.
  • Virtual methods must have a body / method implementation with a definition.

Example:

 public abstract class baseclass { public abstract decimal getarea(decimal Radius); public virtual decimal interestpermonth(decimal amount) { return amount*12/100; } public virtual decimal totalamount(decimal Amount,decimal principleAmount) { return Amount + principleAmount; } } public class derivedclass:baseclass { public override decimal getarea(decimal Radius) { return 2 * (22 / 7) * Radius; } public override decimal interestpermonth(decimal amount) { return amount * 14 / 100; } } 
+2
Aug 09 '17 at 7:30
source share



All Articles