Difference between virtual and abstract methods [duplicate]
Asked Answered
T

4

271

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?

Togo answered 6/2, 2013 at 12:9 Comment(6)
What's your concrete question, what don't you understand?Upanishad
Every thing you copied from msdn.microsoft.com/en-us/library/ms173150(v=vs.80).aspxSpatz
@DanielHilgarth updated my question.Please have a look.Togo
@Anandkumar i copied only the Abstract Classes and Class Members code portion.Have a lookTogo
#391983Poltroon
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().Sliest
S
630

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.
    }
}
Stephenstephenie answered 6/2, 2013 at 12:23 Comment(8)
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]?Mond
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 keywordStephenstephenie
How can we call the Class E VirtualMethod outside of the Class E ?Danell
@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.Stephenstephenie
if E VirtualMethod() has the implementation, which means it has a usage else could have been declared as abstract.Danell
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.Stephenstephenie
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????Holism
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.Assimilation
H
102

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".....
Happ answered 24/9, 2014 at 7:25 Comment(1)
Abstract Method must be overridden in non-abstract child class. Its not like that, you can Override it in a abstract class as well.Flexuous
O
19

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;
            }
        }
Obadiah answered 9/8, 2017 at 7:30 Comment(0)
C
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");
    }
}
Circinus answered 8/11, 2014 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.