Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.

Similar presentations


Presentation on theme: " Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method."— Presentation transcript:

1

2

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.

5

6

7 Base class Derived class

8 Same

9

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%.

11

12

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

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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

38

39

40 implements

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59


Download ppt " Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method."

Similar presentations


Ads by Google