Download presentation
Presentation is loading. Please wait.
Published byAmbrose Alexander Modified over 8 years ago
3
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).
4
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.
7
Base class Derived class
8
Same
10
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%.
13
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
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"); }
37
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
40
implements
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.