Download presentation
Presentation is loading. Please wait.
Published byAlvin Davidson Modified over 6 years ago
1
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
2
12.2 Polymorphism Examples
3
By example: Polymorphic Employee Inheritance Hierarchy
Simple payroll application that polymorphically and dynamically 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).
4
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.
5
12.3 Demonstrating Polymorphic Behavior
7
Base class Derived class
8
Same
9
12.4 Abstract Classes and Methods
10
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%.
11
12.5 Case Study: Payroll System Using Polymorphism
13
override/virtual method1_derived method2_derived method1_base
public 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_derived method2_derived method1_base method2_base
36
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 Console.WriteLine("C.G");
37
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
38
12.7 Case Study: Creating and Using Interfaces
40
implements
54
12.8 Operator Overloading
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.