mahesh shivaswamy
Difference between Abstract Methods and Virtual Methods?
By mahesh shivaswamy in .NET on Jun 27 2019
  • Ehsan Sajjad
    Aug, 2019 30

    They both have different purpose depending on the use case and scenario.

    In situation where we want to enforce that the derived or child classes should provided implementation of a particular method we make it abstract and then the base or parent class will not provide it’s implementation it will just provide the signatures of the method.

    While when we need some default behaviour of a method we define it virtual in the parent class and implement it’s default behavior and child class may provide it’s own implementation or may not.

    • 2
  • Sudheshwer  Rai
    Apr, 2020 14

    Abstract Method:- 1-You can declare this method only under the abstract class.
    2-This method will get body once you will inherit this abstract method abstract class with non-abstract class.
    3-We use abstract method when we have required same method with different logic under the method body.
    4-We also get benefit like we are not duplicating the code.

    Virtual Method-1-We can implement this method under the abstract as well as non-abstract class.
    2-You can access or call this method by base as well as inherited class.
    3-Once you will inherit the class where this method has been implemented then you will have to use override keyword to use this method and you can change the logic under the body of this method.

    Visit - www.sudheshwertech.com or you can provide the reference regarding dot net training. Thanks

    • 0
  • Mehul Rudatala
    Nov, 2019 25

    Abstract methods allow you to the declaration only, you can not define the body part in the base class, and you must have to override the abstract method in the child method. eg. GetSalary()

    Virtual methods allow you to the declaration in the base class, and you can override it in the child class if you want. eg. GetName()

    1. public abstract class BaseEmployee
    2. {
    3. public string FirstName { get; set; }
    4. public string LastName { get; set; }
    5. public virtual string GetName()
    6. {
    7. return this.FirstName + " " + this.LastName;
    8. }
    9. public abstract float GetSalary();
    10. }
    11. public class DeveloperEmployees : BaseEmployee
    12. {
    13. public int MonthlySalary { get; set; }
    14. public override float GetSalary()
    15. {
    16. return this.MonthlySalary * 12;
    17. }
    18. }

    • 0
  • Bidyasagar Mishra
    Sep, 2019 2

    Abstarct Methods
    Abstract Method resides in abstract class and it has no body.
    Abstract Method must be overridden in non-abstract child class.
    Virtual Methods
    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”…..

    • 0
  • mahesh shivaswamy
    Jun, 2019 27

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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS