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.
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
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()
public abstract class BaseEmployee { public string FirstName { get; set; } public string LastName { get; set; } public virtual string GetName() { return this.FirstName + " " + this.LastName; } public abstract float GetSalary(); }public class DeveloperEmployees : BaseEmployee { public int MonthlySalary { get; set; } public override float GetSalary() { return this.MonthlySalary * 12; } }
public abstract class BaseEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual string GetName()
return this.FirstName + " " + this.LastName;
}
public abstract float GetSalary();
public class DeveloperEmployees : BaseEmployee
public int MonthlySalary { get; set; }
public override float GetSalary()
return this.MonthlySalary * 12;
Abstarct MethodsAbstract Method resides in abstract class and it has no body.Abstract Method must be overridden in non-abstract child class.Virtual MethodsVirtual 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”…..
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.