Overriding versus Overloading - Overriding
(Page 4 of 4 )
As mentioned earlier, overriding has to do with parent and child classes. If you are not satisfied with the implementation of a method in the parent class, you can keep the same declaration (signature and return type), but provide a different implementation. For example, a Rectangle class inherits the drawing functionality from a Shape class, but it overrides this functionality in order to be able to draw a rectangle.
We have been using overriding all along in our examples. In C#, everything is an object, so when we create a class ( Employee , Fraction …) that does not specifically inherit from another class, it automatically inherits the functionality of an Object. Notice that in our previous examples, we were able to use the ToString() method to print an object as a string. The ToString() method is defined in the Object class. Since the Object class is too general, this method cannot give us anything useful, so we override it.
Ponder the following example:
namespace OverridingExample
{
class Program
{
static void Main( string [] args)
{
Employee e = new Employee ( "Jim" , "Tester" , 38, 15.95);
Console .WriteLine(e);
Manager m = new Manager ( "Jennifer" , "Jones" , 40, 25, 350);
Console .WriteLine(m);
}
}
class Employee
{
protected string firstName;
protected string lastName;
protected int hoursWorked;
protected double ratePerHour;
public Employee( string f_name, string l_name,
int hours, double rate)
{
firstName = f_name;
lastName = l_name;
hoursWorked = hours;
ratePerHour = rate;
}
public virtual double CalculatePay()
{
return hoursWorked * ratePerHour;
}
public override string ToString()
{
return string .Format( "Name: {0} {1}nPay: {2}" ,
firstName, lastName, CalculatePay());
}
}
class Manager : Employee //Manager inherits from Employee
{
private double bonus;
public Manager( string f_name, string l_name,
int hours, double rate, double bonus)
: base (f_name, l_name, hours, rate)
{
this .bonus = bonus;
}
public override double CalculatePay()
{
return base .CalculatePay() + bonus;
}
}
}
In the Employee class, we are overriding the ToString() method in order to print the Employee in a nicely formatted string (notice the keyword override in the method declaration). The Employee class did not specifically inherit from any class. So by default, it inherits from the Object class, and we are able to override the ToString() method. The CalculatePay() method calculates the Employee ’s pay (rate * hoursWorked).
In the Manager class, on the other hand, we did not override the ToString() method because the one inherited from the Employee class is sufficient. But a Manager’s pay is different from a regular Employee’s pay. Based on that, we can override the CalculatePay() method to account for the bonus. To make this work, however, we need to declare the CalculatePay() method in the Employee class as virtual (which means it is able to be overridden). Now, when we have a reference pointing to an Employee class, it will call that Employee’s CalculatePay() method. When that reference is pointing to a Manager class, it will call the Manager’s Calculatepay() method.
Conclusion
Overriding and overloading are very important features in an object-oriented language like C#. When used correctly, they can lead to very powerful and readable code. When we have methods with similar functionality, it is not necessary to give them different names when we can overload them instead. And when we are not satisfied with the functionality provided in a parent class, we can override the method to meet our needs.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |