Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 11 Polymorphism, Interfaces & Operator Overloading.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 11 Polymorphism, Interfaces & Operator Overloading."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 11 Polymorphism, Interfaces & Operator Overloading

2  2006 Pearson Education, Inc. All rights reserved. 2 One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them. — John Ronald Reuel Tolkien General propositions do not decide concrete cases. — Oliver Wendell Holmes

3  2006 Pearson Education, Inc. All rights reserved. 3 Why art thou cast down, O my soul? — Psalms 42:5 A philosopher of imposing stature doesn’t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or is not known in the time when he lives. — Alfred North Whitehead

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  The concept of polymorphism and how it enables you to "program in the general."  To use overridden methods to effect polymorphism.  To distinguish between abstract and concrete classes.  To declare abstract methods to create abstract classes.  How polymorphism makes systems extensible and maintainable.  To determine an object's type at execution time.  To create sealed methods and classes.  To declare and implement interfaces.  To overload operators to enable them to manipulate objects.

5  2006 Pearson Education, Inc. All rights reserved. 5 11.1 Introduction 11.2 Polymorphism Examples 11.3 Demonstrating Polymorphic Behavior 11.4 Abstract Classes and Methods 11.5 Case Study: Payroll System Using Polymorphism 11.5.1 Creating Abstract Base Class Employee 11.5.2 Creating Concrete Derived Class SalariedEmployee 11.5.3 Creating Concrete Derived Class HourlyEmployee 11.5.4 Creating Concrete Derived Class CommissionEmployee 11.5.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 11.5.6 Polymorphic Processing, Operator is and Downcasting 11.5.7 Summary of the Allowed Assignments Between Base Class and Derived Class Variables

6  2006 Pearson Education, Inc. All rights reserved. 6 11.6 sealed Methods and Classes 11.7 Case Study: Creating and Using Interfaces 11.7.1 Developing an IPayable Hierarchy 11.7.2 Declaring Interface IPayable 11.7.3 Creating Class Invoice 11.7.4 Modifying Class Employee to Implement Interface IPayable 11.7.5 Modifying Class SalariedEmployee for Use in the IPayable Hierarchy 11.7.6 Using Interface IPayable to Process Invoice s and Employee s Polymorphically 11.7.7 Common Interfaces of the.NET Framework Class Library 11.8 Operator Overloading 11.9 (Optional) Software Engineering Case Study: Incorporating Inheritance and Polymorphism into the ATM System 11.10 Wrap-Up

7  2006 Pearson Education, Inc. All rights reserved. 7 Software Engineering Observation 11.1 Polymorphism promotes extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. New object types that can respond to existing method calls can be incorporated into a system without requiring modification of the base system. Only client code that instantiates new objects must be modified to accommodate new types.

8  2006 Pearson Education, Inc. All rights reserved. 8 Outline PolymorphismTest.cs (1 of 3)

9  2006 Pearson Education, Inc. All rights reserved. 9 Outline PolymorphismTest.cs (2 of 3)

10  2006 Pearson Education, Inc. All rights reserved. 10 Outline PolymorphismTest.cs (3 of 3)

11  2006 Pearson Education, Inc. All rights reserved. 11 Software Engineering Observation 11.2 An abstract class declares common attributes and behaviors of the various classes that inherit from it, either directly or indirectly, in a class hierarchy. An abstract class typically contains one or more abstract methods or properties that concrete derived classes must override. The instance variables, concrete methods and concrete properties of an abstract class are subject to the normal rules of inheritance.

12  2006 Pearson Education, Inc. All rights reserved. 12 Common Programming Error 11.1 Attempting to instantiate an object of an abstract class is a compilation error.

13  2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 11.2 Failure to implement a base class’s abstract methods and properties in a derived class is a compilation error unless the derived class is also declared abstract.

14  2006 Pearson Education, Inc. All rights reserved. 14 Software Engineering Observation 11.3 A derived class can inherit “interface” or “implementation” from a base class. Hierarchies designed for implementation inheritance tend to have their functionality high in the hierarchy—each new derived class inherits one or more methods that were implemented in a base class, and the derived class uses the base class implementations. Hierarchies designed for interface inheritance tend to have their functionality lower in the hierarchy—a base class specifies one or more abstract methods that must be declared for each concrete class in the hierarchy, and the individual derived classes override these methods to provide derived-class-specific implementations.

15  2006 Pearson Education, Inc. All rights reserved. 15 Fig. 11.2 | Employee hierarchy UML class diagram.

16  2006 Pearson Education, Inc. All rights reserved. 16 Fig. 11.3 | Polymorphic interface for the Employee hierarchy classes.

17  2006 Pearson Education, Inc. All rights reserved. 17 Outline Employee.cs (1 of 2)

18  2006 Pearson Education, Inc. All rights reserved. 18 Outline Employee.cs (2 of 2)

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline SalariedEmployee.cs (1 of 2)

20  2006 Pearson Education, Inc. All rights reserved. 20 Outline SalariedEmployee.cs (2 of 2)

21  2006 Pearson Education, Inc. All rights reserved. 21 Outline HourlyEmployee.cs (1 of 3)

22  2006 Pearson Education, Inc. All rights reserved. 22 Outline HourlyEmployee.cs (2 of 3)

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline HourlyEmployee.cs (3 of 3)

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline CommissionEmployee.cs (1 of 2)

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline CommissionEmployee.cs (2 of 2)

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline BasePlusCommission Employee.cs (1 of 2)

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline BasePlusCommission Employee.cs (2 of 2)

28  2006 Pearson Education, Inc. All rights reserved. 28 Outline PayrollSystemTest. cs (1 of 5)

29  2006 Pearson Education, Inc. All rights reserved. 29 Outline PayrollSystemTest. cs (2 of 5)

30  2006 Pearson Education, Inc. All rights reserved. 30 Outline PayrollSystemTest. cs (3 of 5)

31  2006 Pearson Education, Inc. All rights reserved. 31 Outline PayrollSystemTest. cs (4 of 5)

32  2006 Pearson Education, Inc. All rights reserved. 32 Outline PayrollSystemTest. cs (5 of 5)

33  2006 Pearson Education, Inc. All rights reserved. 33 Common Programming Error 11.3 Assigning a base class variable to a derived class variable (without an explicit downcast) is a compilation error.

34  2006 Pearson Education, Inc. All rights reserved. 34 Software Engineering Observation 11.4 If at execution time the reference to a derived class object has been assigned to a variable of one of its direct or indirect base classes, it is acceptable to cast the reference stored in that base class variable back to a reference of the derived class type. Before performing such a cast, use the is operator to ensure that the object is indeed an object of an appropriate derived class type.

35  2006 Pearson Education, Inc. All rights reserved. 35 Common Programming Error 11.4 When downcasting an object, an InvalidCastException (of namespace System ) occurs if at execution time the object does not have an is-a relationship with the type specified in the cast operator. An object can be cast only to its own type or to the type of one of its base classes.

36  2006 Pearson Education, Inc. All rights reserved. 36 Performance Tip 11.1 The compiler can decide to inline a sealed method call and will do so for small, simple sealed methods. Inlining does not violate encapsulation or information hiding, but does improve performance because it eliminates the overhead of making a method call.

37  2006 Pearson Education, Inc. All rights reserved. 37 Common Programming Error 11.5 Attempting to declare a derived class of a sealed class is a compilation error.

38  2006 Pearson Education, Inc. All rights reserved. 38 Software Engineering Observation 11.5 In the FCL, the vast majority of classes are not declared sealed. This enables inheritance and polymorphism—the fundamental capabilities of object-oriented programming.

39  2006 Pearson Education, Inc. All rights reserved. 39 Common Programming Error 11.6 It is a compilation error to declare an interface member public or abstract explicitly, because they are redundant in interface member declarations. It is also a compilation error to specify any implementation details, such as concrete method declarations, in an interface.

40  2006 Pearson Education, Inc. All rights reserved. 40 Common Programming Error 11.7 Failing to declare any member of an interface in a class that implements the interface results in a compilation error.

41  2006 Pearson Education, Inc. All rights reserved. 41 Good Programming Practice 11.1 By convention, the name of an interface begins with " I ". This helps distinguish interfaces from classes, improving code readability.

42  2006 Pearson Education, Inc. All rights reserved. 42 Good Programming Practice 11.2 When declaring a method in an interface, choose a method name that describes the method’s purpose in a general manner, because the method may be implemented by a broad range of unrelated classes.

43  2006 Pearson Education, Inc. All rights reserved. 43 Fig. 11.10 | IPayable interface and class hierarchy UML class diagram.

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline IPayable.cs

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline Invoice.cs (1 of 4)

46  2006 Pearson Education, Inc. All rights reserved. 46 Outline Invoice.cs (2 of 4)

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline Invoice.cs (3 of 4)

48  2006 Pearson Education, Inc. All rights reserved. 48 Outline Invoice.cs (4 of 4)

49  2006 Pearson Education, Inc. All rights reserved. 49 Outline Employee.cs (1 of 2)

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline Employee.cs (2 of 2)

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline SalariedEmployee.cs (1 of 2)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline SalariedEmployee.cs (2 of 2)

53  2006 Pearson Education, Inc. All rights reserved. 53 Software Engineering Observation 11.6 Inheritance and interfaces are similar in their implementation of the is-a relationship. An object of a class that implements an interface may be thought of as an object of that interface type. An object of any derived classes of a class that implements an interface also can be thought of as an object of the interface type.

54  2006 Pearson Education, Inc. All rights reserved. 54 Software Engineering Observation 11.7 The is-a relationship that exists between base classes and derived classes, and between interfaces and the classes that implement them, holds when passing an object to a method. When a method parameter receives a variable of a base class or interface type, the method polymorphically processes the object received as an argument.

55  2006 Pearson Education, Inc. All rights reserved. 55 Software Engineering Observation 11.8 All methods of class object can be called by using a reference of an interface type—the reference refers to an object, and all objects inherit the methods of class object.

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline PayableInterfaceTe st.cs (1 of 2)

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline PayableInterfaceTe st.cs (2 of 2)

58  2006 Pearson Education, Inc. All rights reserved. 58 Fig. 11.16 | Common interfaces of the.Net Framework Class Library. (Part 1 of 2)

59  2006 Pearson Education, Inc. All rights reserved. 59 Fig. 11.16 | Common interfaces of the.Net Framework Class Library. (Part 2 of 2)

60  2006 Pearson Education, Inc. All rights reserved. 60 Software Engineering Observation 11.9 Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls.

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline ComplexNumber.cs (1 of 3)

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline ComplexNumber.cs (2 of 3)

63  2006 Pearson Education, Inc. All rights reserved. 63 Outline ComplexNumber.cs (3 of 3)

64  2006 Pearson Education, Inc. All rights reserved. 64 Software Engineering Observation 11.10 Overload operators to perform the same function or similar functions on class objects as the operators perform on objects of simple types. Avoid non-intuitive use of operators.

65  2006 Pearson Education, Inc. All rights reserved. 65 Software Engineering Observation 11.11 At least one argument of an overloaded operator method must be a reference to an object of the class in which the operator is overloaded. This prevents programmers from changing how operators work on simple types.

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline Operator Overloading.cs (1 of 2)

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline Operator Overloading.cs (2 of 2)

68  2006 Pearson Education, Inc. All rights reserved. 68 Fig. 11.19 | Attributes and operations of classes BalanceInquiry, Withdrawal and Deposit.

69  2006 Pearson Education, Inc. All rights reserved. 69 Fig. 11.20 | Class diagram modeling the generalization (i.e., inheritance) relationship between the base class Transaction and its derived classes BalanceInquiry, Withdrawal and Deposit.

70  2006 Pearson Education, Inc. All rights reserved. 70 Fig. 11.21 | Class diagram of the ATM system (incorporating inheritance). Note that abstract class name Transaction appears in italics.

71  2006 Pearson Education, Inc. All rights reserved. 71 Fig. 11.22 | Class diagram after incorporating inheritance into the system.

72  2006 Pearson Education, Inc. All rights reserved. 72 Software Engineering Observation 11.12 A complete class diagram shows all the associations among classes, and all the attributes and operations for each class. When the number of class attributes, operations and associations is substantial (as in Figs. 11.21 and 11.22), a good practice that promotes readability is to divide this information between two class diagrams—one focusing on associations and the other on attributes and operations. When examining classes modeled in this fashion, it is crucial to consider both class diagrams to get a complete picture of the classes. For example, one must refer to Fig. 11.21 to observe the inheritance relationship between Transaction and its derived classes; that relationship is omitted from Fig. 11.22.

73  2006 Pearson Education, Inc. All rights reserved. 73 Outline Withdrawal.cs

74  2006 Pearson Education, Inc. All rights reserved. 74 Outline Withdrawal.cs

75  2006 Pearson Education, Inc. All rights reserved. 75 Outline Transaction.cs (1 of 2)

76  2006 Pearson Education, Inc. All rights reserved. 76 Outline Transaction.cs (2 of 2)


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 11 Polymorphism, Interfaces & Operator Overloading."

Similar presentations


Ads by Google