Virtual - If a base class method is to be overriden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding).
Note that the class member method may be overriden even if the virtual keyword is not used, but its usage makes the code more transparent & meaningful. In VB.NET, we may use the Overridable keyword for this purpose.
When the override keyword is used to override the virtual method, in a scenario where the base class method is required in a child class along with the overriden method, then the base keyword may be used to access the parent class member. The following code example will make the usage more clear.
public class Employee
{
public virtual void myfunction(float money) //This method may be overriden
{ Basic += money; }
}
public class Manager : Employee
{
public override void myfunction(float money) //This method is being overriden
{
float incentive = 10000;
base.SetSalary(money + incentive); //Calling base class method
}
}
No comments:
Post a Comment