Overriding in C#
Extend
or replace functionality in the base class
It
is required to provide an implementation that has the same meaning as the
original method, but has an implementation that is specific to the class. Virtual
methods typically provide a default implementation that
inheriting
classes are expected to replace with their own code.
To
override a method in a subclass, you use the override
keyword, as the following
code
example shows
public
Class Employee
{
protected
int EmployeeNumber;
protected
string EmployeeFirstName;
protected
string EmployeeLastname;
protected
float Salary;
Protected
virtual void WorkAssigned()
{
}
}
//Program
Manager class inheriting from Employee
class
Public
Class TechnicalManager:Employee
{
Protected
Override void WorkAssigned()
{
}
}
Hiding Methods
in C#
You
can define methods in an inherited class that have the same name as methods
in
a base class, even if they are not marked as virtual, abstract, or override.
However,
this means that there is no relationship between your method and the
original
method, and new method hides the original method.
Code
Public Class Employee
{
protected void
ActOn()
{
...
}
}
class Manager : Employee
{
public new void
ActOn()
{
// Hiding he ActOn method in the base class
//Code goes here
}
}
No comments:
Post a Comment