Chapter 12OOP: Polymorphism, Interfaces and Operator
Overloading
12.2 Polymorphism Examples
By example: Polymorphic Employee Inheritance Hierarchy
Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Though the earnings of each type of employee are calculated in a specific way, polymorphism allows us to process the employees “in the general.”
Two new classes — SalariedEmployee (for people paid a fixed weekly salary) and HourlyEmployee (for people paid an hourly salary and “time-and-a-half” for overtime).
By example: Polymorphic Employee Inheritance Hierarchy cont
common set of functionality for all the classes in the updated hierarchy in an “abstract” class, Employee, from which classes SalariedEmployee, HourlyEmployee and
CommissionEmployee inherit directly and class BasePlusCommissionEmployee inherits indirectly.
=> invoke each employee’s Earnings method off a base class
Employee reference, the correct earnings calculation is performed
due to C#’s polymorphic capabilities.
12.3 Demonstrating Polymorphic Behavior
Base class
Derived class
Same
12.4 Abstract Classes and Methods
Determining the Type of an Object at Execution Time
Occasionally, when performing polymorphic processing, we need to program “in the specific.”
Employee case study demonstrates that an application can determine the type of an object at execution time and act on that object accordingly. In the case study, we use these capabilities to determine whether a particular employee object is a
BasePlusCommissionEmployee. ◦ As the result employee’s base salary is increased by 10%.
12.5 Case Study: Payroll System Using Polymorphism
override/virtualpublic class Base
{public class Base
{
public string method1() { return "method1_base"; }
public virtual string method2() { return "method2_base"; }
}
public class Derived : Base
{
public static void Main(string[] args) method1_derived
{
Derived derived = new Derived();
Console.WriteLine(derived.method1());
Console.WriteLine(derived.method2());
Console.WriteLine(derived.method3());
Console.WriteLine(derived.method4());
}
public string method1() { return "method1_derived"; } //hiding
public override string method2() { return "method2_derived"; }
public string method3() { return base.method1(); }
public string method4() { return base.method2(); }
}
method1_derivedmethod2_derivedmethod1_basemethod2_base
sealed Methods When an instance method declaration includes a sealed modifier, that method is said to be a
sealed method. If an instance method declaration includes the sealed modifier, it must also include the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method.
using System;
class A
{
public virtual void F() {
Console.WriteLine("A.F");
}
public virtual void G() {
Console.WriteLine("A.G");
}
}
class B: A
{
sealed override public void F() {
Console.WriteLine("B.F");
}
override public void G() {
Console.WriteLine("B.G");
}
}
class C: B
{
override public void G() {
Console.WriteLine("C.G");
}
}
12.6 sealed Methods and Classes
• Sealed method in a base class cannot be overridden in a derived class.
• private methods implicitly sealed• static methods implicitly sealed• Class sealed cannot be a base class
12.7 Case Study: Creating and Using Interfaces
implements
12.8 Operator Overloading