Simple payroll application that polymorphically calculates the weekly pay of several different types...

59
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading

Transcript of Simple payroll application that polymorphically calculates the weekly pay of several different types...

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

Chapter 12OOP: Polymorphism, Interfaces and Operator

Overloading

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

12.2 Polymorphism Examples

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

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

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

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.

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

12.3 Demonstrating Polymorphic Behavior

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

Base class

Derived class

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

Same

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

12.4 Abstract Classes and Methods

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

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

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

12.5 Case Study: Payroll System Using Polymorphism

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

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

Page 14: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 15: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 16: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 17: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 18: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 19: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 20: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 21: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 22: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 23: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 24: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 25: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 26: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 27: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 28: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 29: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 30: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 31: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 32: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 33: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 34: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 35: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 36: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.

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");

}

}

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

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

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

12.7 Case Study: Creating and Using Interfaces

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

implements

Page 41: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 42: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 43: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 44: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 45: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 46: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 47: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 48: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 49: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 50: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 51: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 52: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 53: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 54: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.

12.8 Operator Overloading

Page 55: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 56: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 57: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 58: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Page 59: Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.