257

Here is some 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 anyone explain the above code with respect to the differences between abstract and virtual methods?

one noa
  • 345
  • 1
  • 3
  • 10
iJade
  • 23,144
  • 56
  • 154
  • 243
  • What's your concrete question, what don't you understand? – Daniel Hilgarth Feb 06 '13 at 12:12
  • Every thing you copied from http://msdn.microsoft.com/en-us/library/ms173150(v=vs.80).aspx – andy Feb 06 '13 at 12:12
  • @DanielHilgarth updated my question.Please have a look. – iJade Feb 06 '13 at 12:13
  • @Anandkumar i copied only the Abstract Classes and Class Members code portion.Have a look – iJade Feb 06 '13 at 12:15
  • http://stackoverflow.com/questions/391483/what-is-the-difference-between-an-abstract-function-and-a-virtual-function – Y2theZ Feb 06 '13 at 12:17
  • Overriding Class-D's virtual `DoWork()` method is **_optional_** for **_all_** it's immediate-children/subclasses. The code above adds an intermediary abstract Class-E to **_force_** the overriding of Class-D's `DoWork()` method for all subclasses of Class-E - essentially preventing all of Class-E's children from using Grandpa-Class-D's `DoWork()`. In other words: the implementation of Class-D's `DoWork()` is **_hidden_** from Class-F; and it is now compulsory for Class-F (and all it's sibling classes) to **_each_** form their own custom implementation of `DoWork()`. – MikeTeeVee Mar 30 '23 at 14:23

4 Answers4

596

Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method.

So, abstract methods have no actual code in them, and (non-abstract) subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN 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.
    }
}
Dennisch
  • 6,888
  • 1
  • 17
  • 32
  • 11
    Excellently put! One question though...Do you have to explicitly state `override` for the abstract method in the child class [because it is required it be overwritten]? – J.S. Orris Feb 28 '15 at 23:54
  • 43
    For abstract methods you have to explicibly state it, yes. For virtual methods it is more complicated. If you don't state the `override` keyword there, the original method will become hidden. If this is your intention, you can use `new` keyword to get rid of the warning, but cases where you want to hide methods are pretty rare. So unless you specifically want to use method hiding, you should always use the `override` keyword – Dennisch Mar 03 '15 at 12:47
  • How can we call the Class E VirtualMethod outside of the Class E ? – Jaydeep Shil Aug 28 '19 at 04:35
  • @Jaydeep Shil You can't, and you shouldn't. If D overrides the virtual method, it means that that code should be used from the outside. – Dennisch Sep 11 '19 at 15:49
  • if E VirtualMethod() has the implementation, which means it has a usage else could have been declared as abstract. – Jaydeep Shil Sep 12 '19 at 05:36
  • That's true, but if D overrides the method, it means outside classes working with D should use that implementation. If it's not overridden, the method in E will be used regardless. – Dennisch Sep 17 '19 at 08:40
  • **The actual question is class D virtual method can be abstract method of abstract class E?? And, abstracted virtual method of E can be overrided in class F?? Possible????** – Vintage Coder Oct 17 '21 at 10:10
  • 1
    Virtual method 1. Virtual methods have an implementation 2. Optional to Override (Can Be) 3. Class need not to be virtual 4.Virtual Method can be in abstract and non-abstract class both. Abstract Method 1. Must be Override By Inherited class.(Must be) 2.Don't have Implementation. 3. Abstract method must be in Abstract class 4. Can't be instantiate without inheritance. – Ahmed Zamil Jun 03 '22 at 00:36
95

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

Abstract Method

  • Abstract Method resides in abstract class and it has no body.
  • Abstract Method must be overridden in non-abstract child class.

Virtual Method

  • Virtual Method can reside in abstract and non-abstract class.
  • It is not necessary to override virtual method in derived but it can be.
  • Virtual method must have body ....can be overridden by "override keyword".....
Community
  • 1
  • 1
aizaz
  • 951
  • 6
  • 2
  • 5
    *Abstract Method must be overridden in non-abstract child class.* Its not like that, you can Override it in a abstract class as well. – Shaminder Singh Jul 17 '17 at 07:33
17

Abstract Method:

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

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

  • An abstract method must be over ride in the derived class.

Virtual Method:

  • Virtual methods can be over ride in the derived class but not mandatory.
  • Virtual methods must have the method body/implementation along with the 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;
            }
        }
Mahib
  • 3,977
  • 5
  • 53
  • 62
Zubair Saif
  • 1,106
  • 1
  • 14
  • 29
6

an abstract method must be call override in derived class other wise it will give compile-time error and in virtual you may or may not override it's depend if it's 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");
    }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
user4230113
  • 61
  • 1
  • 1