| |
|
What is the virtual keyword used for?
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 SetBasic(float money) //This method may be overriden
{ Basic += money; }
}
public class Manager : Employee
{
public override void SetBasic(float money) //This method is being overriden
{
float managerIncentive = 10000;
base.SetSalary(money + managerIncentive); //Calling base class method
}
}
OOPs
Class
Encapsulation
Inheritance
Class Member
Polymorphism
Property Event
Access Modifier
Overloading
Shared
Virtual
Overridable Overrides Mustoverride
Shadows
Constructor
Static Constructor
Serialization
Delegate
Abstract
Interface
Multiple Inheritance
| |