15
1 1 1992-2007 Pearson Education, Inc. All rights reserved. 10 10 Object-Oriented Programming: Polymorphism 2 1992-2007 Pearson Education Inc. All rights reserved. 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 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 Why art thou cast down, O my soul? Psalms 42:5 3 1992-2007 Pearson Education, Inc. All rights reserved. OBJECTIVES In this chapter you will learn: The concept of polymorphism. 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 declare and implement interfaces. 4 1992-2007 Pearson Education, Inc. All rights reserved. 10.1 Introduction 10.2 Polymorphism Examples 10.3 Demonstrating Polymorphic Behavior 10.4 Abstract Classes and Methods 10.5 Case Study: Payroll System Using Polymorphism 10.5.1 Creating Abstract Superclass 10.5.2 Creating Concrete Subclass 10.5.3 Creating Concrete Subclass 10.5.4 Creating Concrete Subclass 10.5.5 Creating Indirect Concrete Subclass 10.5.6 Demonstrating Polymorphic Processing, Operator and Downcasting 10.5.7 Summary of the Allowed Assignments Between Superclass and Subclass Variables 10.6 Methods and Classes 5 1992-2007 Pearson Education, Inc. All rights reserved. 10.7 Case Study: Creating and Using Interfaces 10.7.1 Developing a Hierarchy 10.7.2 Declaring Interface 10.7.3 Creating Class 10.7.4 Modifying Class to Implement Interface 10.7.5 Modifying Class for Use in the Hierarchy 10.7.6 Using Interface to Process and Polymorphically 10.7.7 Declaring Constants with Interfaces 10.7.8 Common Interfaces of the Java API 10.8 (Optional) GUI and Graphics Case Study: Drawing with Polymorphism 10.9 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System 10.10 Wrap-Up 6 1992-2007 Pearson Education, Inc. All rights reserved. 10.1 Introduction Polymorphism Enables “programming in the general” The same invocation can produce “many forms” of results Interfaces Implemented by classes to assign common functionality to possibly unrelated classes

One Ring to rule them all, One Ring to find them, One Ring

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: One Ring to rule them all, One Ring to find them, One Ring

1

1

1992-2007 Pearson Education, Inc. All rights reserved.

1010Object-Oriented

Programming: Polymorphism

2

1992-2007 Pearson Education Inc. All rights reserved.

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

A philosopher of imposing stature doesn’t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned bywhat is or is not known in the time when he lives.

— Alfred North Whitehead

Why art thou cast down, O my soul?— Psalms 42:5

3

1992-2007 Pearson Education, Inc. All rights reserved.

OBJECTIVESIn this chapter you will learn:� The concept of polymorphism.� 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 declare and implement interfaces.

4

1992-2007 Pearson Education, Inc. All rights reserved.

10.1 Introduction 10.2 Polymorphism Examples 10.3 Demonstrating Polymorphic Behavior 10.4 Abstract Classes and Methods 10.5 Case Study: Payroll System Using Polymorphism

10.5.1 Creating Abstract Superclass ��������������������������������10.5.2 Creating Concrete Subclass ����������������������������������������������������10.5.3 Creating Concrete Subclass ������������ ������������ ������������ ������������10.5.4 Creating Concrete Subclass ������������������������������������������������������������������������10.5.5 Creating Indirect Concrete Subclass

����������������������������������������������������������������������������������������������������10.5.6 Demonstrating Polymorphic Processing,

Operator ������������������������������������ and Downcasting10.5.7 Summary of the Allowed Assignments

Between Superclass and Subclass Variables 10.6 ���������������� Methods and Classes

5

1992-2007 Pearson Education, Inc. All rights reserved.

10.7 Case Study: Creating and Using Interfaces10.7.1 Developing a �������������������� Hierarchy 10.7.2 Declaring Interface ��������������������10.7.3 Creating Class ����������������������������10.7.4 Modifying Class �������������������������������� to Implement

Interface ��������������������10.7.5 Modifying Class ���������������������������������������������������� for

Use in the �������������������� Hierarchy10.7.6 Using Interface �������������������� to Process

�������������������������������� and ������������������������������������Polymorphically

10.7.7 Declaring Constants with Interfaces 10.7.8 Common Interfaces of the Java API

10.8 (Optional) GUI and Graphics Case Study: Drawing with Polymorphism

10.9 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System

10.10 Wrap-Up

6

1992-2007 Pearson Education, Inc. All rights reserved.

10.1 Introduction

• Polymorphism– Enables “programming in the general”– The same invocation can produce “many forms” of results

• Interfaces– Implemented by classes to assign common functionality to

possibly unrelated classes

Page 2: One Ring to rule them all, One Ring to find them, One Ring

2

7

1992-2007 Pearson Education, Inc. All rights reserved.

10.2 Polymorphism Examples

• Polymorphism– When a program invokes a method through a superclass

variable, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable

– The same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked

– Facilitates adding new classes to a system with minimal modifications to the system’s code

8

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.1

Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics. Programmers can command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy).

9

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.2

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.

10

1992-2007 Pearson Education, Inc. All rights reserved.

10.3 Demonstrating Polymorphic Behavior

• A superclass reference can be aimed at a subclass object

– This is possible because a subclass object is a superclassobject as well

– When invoking a method from that reference, the type of the actual referenced object, not the type of the reference, determines which method is called

• A subclass reference can be aimed at a superclassobject only if the object is downcasted

11

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(1 of 2)

1 ��� �!��"#�"$���������������������� �!��"#�"$���������������������� �!��"#�"$���������������������� �!��"#�"$�������������������

2 ���%���!���!�����������������������������������������������%���!���!�����������������������������������������������%���!���!�����������������������������������������������%���!���!��������������������������������������������

3 ������������������������������������������������������������������������

4

5 ������������������������������������������������������������������������������������������������������������������������

6 &&&&����

7 ������������������������������������������������������������������������������������'�����!�!�()�*�����'�����!�!�()�*�����'�����!�!�()�*�����'�����!�!�()�*�����

8 ���&���&���&���&����

9 ������������������������������������������!��������������������������������������!��������������������������������������!��������������������������������������!������������������������������������������������������������������������������������

10 ������������������������������������������+��������������������,�������������������+��������������������,�������������������+��������������������,�������������������+��������������������,���-��-��-��-�������������������+'�������������������+'�������������������+'�������������������+'����

11 ������������������������������������.���..���..���..���./�/�/�/�.0����..0����..0����..0����./�/�/�/�.111.111.111.11122221111111122221111.1111.1111.1111./�/�/�/�"####"####"####"####/�/�/�/��#3�#3�#3�#3�*4�*4�*4�*4����������������������������������������������������������������������������

12

13 ������������������������������!�������������������!�������������������!�������������������!�����������������������������������������������������������������������������������������������������������������������������

14 �������������������������������������������������5���������������������������,�������������������������5���������������������������,�������������������������5���������������������������,�������������������������5���������������������������,����

15 ��������������������������������������-��-��-��-��������������������������5'��������������������������5'��������������������������5'��������������������������5'����������������������������������������������������������������������������������������

16 ������������������������������������.���..���..���..���./�/�/�/�.6�-��..6�-��..6�-��..6�-��./�/�/�/�.+++.+++.+++.+++2222++++++++2222++++.++++.++++.++++./�/�/�/�7###7###7###7###/�/�/�/��#5�#5�#5�#5/�/�/�/�+##+##+##+##�*4�*4�*4�*4������������������������

17

18 �������������������������������������8��������!������������������������!��������������������8��������!������������������������!��������������������8��������!������������������������!��������������������8��������!������������������������!���������������

19 ����������������������'�����������������������'�����������������������'�����������������������'�.9��9�$.9��9�$.9��9�$.9��9�$::::����::::�9��9��9��9�::::����::::�.�.�.�./�/�/�/�����

20 ������������������������������������.����������������������+;��������!�-���������������������..����������������������+;��������!�-���������������������..����������������������+;��������!�-���������������������..����������������������+;��������!�-���������������������.////����

21 ������������������������������������.������������������..������������������..������������������..������������������./�/�/�/�����������������������������������������������������������������������������������������������!'*��!'*��!'*��!'*�*4�*4�*4�*4����

22

23 �������������������������������8��������!�����������������������!���������������������8��������!�����������������������!���������������������8��������!�����������������������!���������������������8��������!�����������������������!������������������������������������������

24 ����������������������'�����������������������'�����������������������'�����������������������'�.9��9�$.9��9�$.9��9�$.9��9�$::::����::::�9��9��9��9�::::����::::�.�.�.�./�/�/�/�����

25 ������������������������������������.�����������������������������5;��������!�-�����������..�����������������������������5;��������!�-�����������..�����������������������������5;��������!�-�����������..�����������������������������5;��������!�-�����������.////����

26 ������������������������������������.�����������������.�����������������.�����������������.�������������������������.��������.��������.��������./�/�/�/�����

27 ��������������������������������������������������������������������!'*��������������������������������!'*��������������������������������!'*��������������������������������!'*�*4�*4�*4�*4����

28

Typical reference assignments

12

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(2 of 2)

29 �������������������������������8��������!�����������������������!����������������������8��������!�����������������������!����������������������8��������!�����������������������!����������������������8��������!�����������������������!���������������

30 ������������������������������������������+�������������������1�,������������������+�������������������1�,������������������+�������������������1�,������������������+�������������������1�,����

31 �������������������������������������������������������������4�������������������������4�������������������������4�������������������������4������������������������������������������������

32 ����������������������'�����������������������'�����������������������'�����������������������'�.9��9�$.9��9�$.9��9�$.9��9�$::::����::::�9��9��9��9�::::�.�.�.�./�/�/�/�����

33 ������������������������������������.����������.����������.����������.�����������������������������5;��������!�-������������.�������������������5;��������!�-������������.�������������������5;��������!�-������������.�������������������5;��������!�-������������.////����

34 ������������������������������������.�������������������������..�������������������������..�������������������������..�������������������������./�/�/�/�������������������1�������!'*������������������1�������!'*������������������1�������!'*������������������1�������!'*�*4�*4�*4�*4����

35 ���<����<����<����<�����������������������������������������

36 <�<�<�<�������������������������������������������������������������������������������������������������������������

����������������������+;��������!�-����������������������������������������������+;��������!�-����������������������������������������������+;��������!�-����������������������������������������������+;��������!�-������������������������������������������������������������������$������$������$������$���� �������������������$�����0�����������������������$�����0�����������������������$�����0�����������������������$�����0���������������������������$�111�������������������$�111�������������������$�111�������������������$�11122221111111122221111111111111111����!��������$�"####�##!��������$�"####�##!��������$�"####�##!��������$�"####�##�����������������$�#�#3�������������$�#�#3�������������$�#�#3�������������$�#�#3���� �����������������������������5;��������!�-����������������������������������������������������5;��������!�-����������������������������������������������������5;��������!�-����������������������������������������������������5;��������!�-�����������������������������������������$��������������$��������������$��������������$���� ������������2222�������������������������$�������������������������$�������������������������$�������������������������$�����6�-�������6�-�������6�-�������6�-�������������������������$�+++�������������������$�+++�������������������$�+++�������������������$�+++2222++++++++2222++++++++++++++++����!��������$�7###�##!��������$�7###�##!��������$�7###�##!��������$�7###�##�����������������$�#�#5�������������$�#�#5�������������$�#�#5�������������$�#�#5�����������$�+##�##�������$�+##�##�������$�+##�##�������$�+##�##���� �����������������������������5;��������!�-�����������������������������������������������������5;��������!�-�����������������������������������������������������5;��������!�-�����������������������������������������������������5;��������!�-������������������������������������������$��������������$��������������$��������������$���� ������������2222�������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-����������������������������������������������$�+++������������$�+++������������$�+++������������$�+++2222++++++++2222++++++++++++++++����!��������$�7###�##!��������$�7###�##!��������$�7###�##!��������$�7###�##�����������������$�#�#5�������������$�#�#5�������������$�#�#5�������������$�#�#5�����������$�+##�##�������$�+##�##�������$�+##�##�������$�+##�##����

Assign a reference to a basePlusCommissionEmployee object to a CommissionEmployee3 variable

Polymorphically call basePlusCommissionEmployee’stoString method

Page 3: One Ring to rule them all, One Ring to find them, One Ring

3

13

1992-2007 Pearson Education, Inc. All rights reserved.

10.4 Abstract Classes and Methods

• Abstract classes– Classes that are too general to create real objects– Used only as abstract superclasses for concrete subclasses

and to declare reference variables– Many inheritance hierarchies have abstract superclasses

occupying the top few levels– Keyword abstract

• Use to declare a class abstract• Also use to declare a method abstract

– Abstract classes normally contain one or more abstract methods

– All concrete subclasses must override all inherited abstract methods

14

1992-2007 Pearson Education, Inc. All rights reserved.

10.4 Abstract Classes and Methods (Cont.)

•Iterator class– Traverses all the objects in a collection, such as an array– Often used in polymorphic programming to traverse a

collection that contains references to objects from various levels of a hierarchy

15

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.3

An abstract class declares common attributes and behaviors of the various classes in a class hierarchy. An abstract class typically contains one or more abstract methods that subclasses must override if the subclasses are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.

16

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.1

Attempting to instantiate an object of an abstract class is a compilation error.

17

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.2

Failure to implement a superclass’s abstract methods in a subclass is a compilation error unless the subclass is also declared ��������������������.

18

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.2 | �������������������������������� hierarchy UML class diagram.

Page 4: One Ring to rule them all, One Ring to find them, One Ring

4

19

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.4

A subclass can inherit “interface” or “implementation” from a superclass. Hierarchies designed for ��������������������������������������������������������������������������������������������tend to have their functionality high in the hierarchy—each new subclass inherits one or more methods that were implemented in a superclass, and the subclass uses the superclassimplementations. (cont…)

20

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.4

Hierarchies designed for ���������������������������������������������������������������� tend to have their functionality lower in the hierarchy—a superclass specifies one or more abstract methods that must be declared for each concrete class in the hierarchy, and the individual subclasses override these methods to provide subclass-specific implementations.

21

1992-2007 Pearson Education, Inc. All rights reserved.

10.5.1 Creating Abstract SuperclassEmployee

•abstract superclass Employee– earnings is declared abstract

• No implementation can be given for earnings in the Employee abstract class

– An array of Employee variables will store references to subclass objects• earnings method calls from these variables will call the

appropriate version of the earnings method

22

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.3 | Polymorphic interface for the �������������������������������� hierarchy classes.

23

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(1 of 3)

1 ��� �!��"#�5$��������������� �!��"#�5$��������������� �!��"#�5$��������������� �!��"#�5$������������

2 ������������������������������������������������������������������������������������������������������������

3

4 ����������������������������������������������������������������������������������������������������������������

5 &&&&����

6 �������������������������������������!�����=��4�����!�����=��4�����!�����=��4�����!�����=��4����

7 �������������������������������������!����=��4�����!����=��4�����!����=��4�����!����=��4����

8 �������������������������������������!�������������=����4�����!�������������=����4�����!�������������=����4�����!�������������=����4����

9

10 ����������������������������������������2222!�������!�������!�������!���������������������������������������

11 ���������������������������������������������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*����

12 ���&���&���&���&����

13 ����������=���,�����4����������=���,�����4����������=���,�����4����������=���,�����4����

14 ���������=���,����4���������=���,����4���������=���,����4���������=���,����4����

15 ������������������=�����,����4������������������=�����,����4������������������=�����,����4������������������=�����,����4����

16 ���<����<����<����<���������������������������������������������2222!������������������������!������������������������!������������������������!������������������������ 17

Declare abstract class Employee

Attributes common to all employees

24

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(2 of 3)

18 ������������������������������������������������������������������������

19 ������������������������������������������������������������ ���=��'�����!������*���� ���=��'�����!������*���� ���=��'�����!������*���� ���=��'�����!������*����

20 ���&���&���&���&����

21 ����������=���,�����4����������=���,�����4����������=���,�����4����������=���,�����4����

22 ���<���<���<���<������������������ ���=�������������������� ���=�������������������� ���=�������������������� ���=��

23 24 ����������������������������������������������������������������������������

25 �����������������������������������������!�!�� ���=��'*�����!�!�� ���=��'*�����!�!�� ���=��'*�����!�!�� ���=��'*����

26 ���&���&���&���&����

27 ������������������������������������������������������������=��4=��4=��4=��4����

28 ���<����<����<����<���������������!�� ���=����������������!�� ���=����������������!�� ���=����������������!�� ���=��

29 30 ��������������������������������������������������������������������

31 ������������������������������������������������������������6��=��'�����!�����*����6��=��'�����!�����*����6��=��'�����!�����*����6��=��'�����!�����*����

32 ���&���&���&���&����

33 ���������=���,����4���������=���,����4���������=���,����4���������=���,����4����

34 ���<����<����<����<������������������6��=�������������������6��=�������������������6��=�������������������6��=��

35 36 ������������������������������������������������������������������������

37 �����������������������������������������!�!��6�����!�!��6�����!�!��6�����!�!��6��=��'*��=��'*��=��'*��=��'*����

38 ���&���&���&���&����

39 ��������������=��4��������������=��4��������������=��4��������������=��4����

40 ���<����<����<����<���������������!��6��=����������������!��6��=����������������!��6��=����������������!��6��=��

41

Page 5: One Ring to rule them all, One Ring to find them, One Ring

5

25

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(3 of 3)

42 �������������������������������������������������������������������������������������������������������������������� 43 ������������������������������������������������������������������������=����'�����!�����*����������������=����'�����!�����*����������������=����'�����!�����*����������������=����'�����!�����*����

44 ���&���&���&���&����

45 ������������������=�����,����4�������������������=�����,����4�������������������=�����,����4�������������������=�����,����4�����������������������������������������������������������������

46 ���<����<����<����<������������������������������=���������������������������������=���������������������������������=���������������������������������=����

47

48 ������������������������������������������������������������������������������������������������������������������������

49 �����������������������������������������!�!��������������=����'*�����!�!��������������=����'*�����!�!��������������=����'*�����!�!��������������=����'*����

50 ���&���&���&���&����

51 �����������������������������������������������������=����4�������������=����4�������������=����4�������������=����4����

52 ���<����<����<����<���������������!��������������=������������������!��������������=������������������!��������������=������������������!��������������=����

53

54 ���������������!����������������������������������������������!����������������������������������������������!����������������������������������������������!�������������������������������

55 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*����

56 ���&���&���&���&����

57 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.9��9�.9��9�.9��9�.9��9�::::��������������������$�9�.��������������������$�9�.��������������������$�9�.��������������������$�9�./�/�/�/�����

58 ���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4����

59 ���<����<����<����<���������������������!��������������������!��������������������!��������������������!

60

61 ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

62 ���������������������������������������������������������������������������������������������!�'*4������!�'*4������!�'*4������!�'*4���������������������������������������������������������������������������������������������

63 <�<�<�<���������������������������������������������������������������������������������������������������������

abstract method earningshas no implementation

26

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������

������������

(1 of 2)

1 ��� �!��"#�7$�������������������� �!��"#�7$�������������������� �!��"#�7$�������������������� �!��"#�7$�����������������

2 �����������������������>��������������������������������������>��������������������������������������>��������������������������������������>���������������

3 4 ���������������������������������������������������������������������������������������������������������>������>������>������>�������������������������������������������������

5 &&&&����

6 �������������������������������������������������������������-��8�����4�-��8�����4�-��8�����4�-��8�����4����

7 8 ������������������������������������2222!���������������!���������������!���������������!���������������

9 �����������������������������������������������������������������������������������������'�����!�����/�����!����/�����!����/��'�����!�����/�����!����/�����!����/��'�����!�����/�����!����/�����!����/��'�����!�����/�����!����/�����!����/�����

10 �����������������������������������������������������*�����*�����*�����*����

11 ���&���&���&���&����

12 ����������������������������������������'�����/����/�����*4�'�����/����/�����*4�'�����/����/�����*4�'�����/����/�����*4�����������������������������������������������������������������������������������������������������������������

13 ���������?��8�����'�����*4����������?��8�����'�����*4����������?��8�����'�����*4����������?��8�����'�����*4�������������������������������������������������������������������������������������

14 ���<���<���<���<��������������������������������������������2222!�����������!�����������!�����������!�����������������������������������������������������������������������������������

15 16 ����������������������������������������������������

17 ������������������������������������������������������������?��8�����'�����?��8�����'�����?��8�����'�����?��8�����'������������������������������*�����*�����*�����*����

18 ���&���&���&���&����

19 ������-��8������,�����@�������-��8������,�����@�������-��8������,�����@�������-��8������,�����@�#�##�##�##�#�A��A��A��A�#�##�##�##�#�$����4�$����4�$����4�$����4����

20 ���<����<����<����<������������������?��8����������������������?��8����������������������?��8����������������������?��8�����

21

Class SalariedEmployeeextends class Employee

Call superclass constructor

Validate and set weekly salary value

Call setWeeklySalary method

27

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������

������������

(2 of 2)

22 ��������������������������������������������������������

23 �����������������������������������������������������������������!��?��8�����'*�!��?��8�����'*�!��?��8�����'*�!��?��8�����'*����

24 ���&���&���&���&����

25 �����������������������������������������-��8�����4�-��8�����4�-��8�����4�-��8�����4����

26 ���<����<����<����<���������������!��?��8�������������������!��?��8�������������������!��?��8�������������������!��?��8�����

27 28 ���������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!�������������

29 ���������������������������������������������������������������������������������!�'*�!�'*�!�'*�!�'*������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

30 ������������&&&&��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

31 �����������������������������������������!��?��8�����'*4�!��?��8�����'*4�!��?��8�����'*4�!��?��8�����'*4��������������������������������������������������������������������������������������������������������������������������������������������������������������������

32 ������������<�<�<�<�������������������!�������������������!�������������������!�������������������!���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

33 34 ������������������������!������������������������������������������������!������������������������������������������������!������������������������������������������������!������������������������������������������������

35 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*��������������������������������������������������������������������������������������������������������������������������������������������������������

36 ������������&&&&����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

37 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.��������������$�9�.��������������$�9�.��������������$�9�.��������������$�9�::::�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�.////����

38 �����������������������������������������������������������!'*/��������!'*/��������!'*/��������!'*/�.-��8������..-��8������..-��8������..-��8������./�!��?��8�����'*�*4/�!��?��8�����'*�*4/�!��?��8�����'*�*4/�!��?��8�����'*�*4����

39 ������������<�<�<�<���������������������!��������������������!��������������������!��������������������!����������������������������������������������������������������������������������������������������������������������������������������������������

40 <�<�<�<�����������������������������������������������������������������������������������������������������

Override earnings method so SalariedEmployee can be concrete

Override toString method

Call superclass’s version of toString

28

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������ ������������ ������������ ������������

������������

(1 of 2)

1 ��� �!��"#�3$� ������������������ �!��"#�3$� ������������������ �!��"#�3$� ������������������ �!��"#�3$� ���������������

2 ��� �������������������>������������������ �������������������>������������������ �������������������>������������������ �������������������>���������������

3 4 ��������������������������������������������� �������������� �������������� �������������� ��������������>������>������>������>�������������������������������������������������

5 &&&&����

6 �������������������������������������������������������������-!�4��-!�4��-!�4��-!�4����-!�����������-!�����������-!�����������-!��������

7 �����������������������������������������������������������������4������4������4������4���������-�8������-��8��������-�8������-��8��������-�8������-��8��������-�8������-��8

8 9 ����������������������������������������2222!���������������!���������������!���������������!���������������

10 ������������������������������������� ������������'�����!�����/�����!����/�����!����/� ������������'�����!�����/�����!����/�����!����/� ������������'�����!�����/�����!����/�����!����/� ������������'�����!�����/�����!����/�����!����/����

11 ������������������������������������������������������?!�/�������?!�/�������?!�/�������?!�/������������������������������?�8���*�����?�8���*�����?�8���*�����?�8���*����

12 ���&���&���&���&����

13 ����������������������������������������'�����/����/�����*4'�����/����/�����*4'�����/����/�����*4'�����/����/�����*4����

14 ���������?!�'������?!��*4����������?!�'������?!��*4����������?!�'������?!��*4����������?!�'������?!��*4�����������������-!�����������������-!�����������������-!�����������������-!�

15 ��������� ���'�����?�8���*4���������� ���'�����?�8���*4���������� ���'�����?�8���*4���������� ���'�����?�8���*4����������������-�8�����������������-�8�����������������-�8�����������������-�8��

16 ���<����<����<����<���������������������������������������������2222!������ ����������������������!������ ����������������������!������ ����������������������!������ ����������������������

17 18 ����������-!�����������-!�����������-!�����������-!�

19 ������������������������������������������������������������?!�'�����?!�'�����?!�'�����?!�'�������������������������������?!��*������?!��*������?!��*������?!��*����

20 ���&���&���&���&����

21 ������-!��,�'������?!��@�������-!��,�'������?!��@�������-!��,�'������?!��@�������-!��,�'������?!��@�#�##�##�##�#�*�A��*�A��*�A��*�A�#�##�##�##�#�$�������$�������$�������$������?!�4?!�4?!�4?!�4����

22 ���<����<����<����<������������������?!������������������?!������������������?!������������������?!�

23 24 �����������-!������������-!������������-!������������-!�

25 �����������������������������������������������������������������!��?!�'*�!��?!�'*�!��?!�'*�!��?!�'*����

26 ���&���&���&���&����

27 �����������������������������������������-!�4�-!�4�-!�4�-!�4����

28 ���<����<����<����<���������������!��?!���������������!��?!���������������!��?!���������������!��?!� 29

Class HourlyEmployeeextends class Employee

Call superclass constructor

Validate and set hourly wage value

29

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������ ������������ ������������ ������������

������������

(2 of 2)

30 ���������������-�8�����������������-�8�����������������-�8�����������������-�8��

31 ������������������������������������������������������������ ���'����� ���'����� ���'����� ���'������������������������������?�8���*�����?�8���*�����?�8���*�����?�8���*����

32 ���&���&���&���&����

33 �����������,�'�'�����?�8���C,������������,�'�'�����?�8���C,������������,�'�'�����?�8���C,������������,�'�'�����?�8���C,�#�##�##�##�#�*�DD�'�����?�8���@,��*�DD�'�����?�8���@,��*�DD�'�����?�8���@,��*�DD�'�����?�8���@,�"3E�#"3E�#"3E�#"3E�#�*�*�A�*�*�A�*�*�A�*�*�A����

34 �������������?�8���$��������������?�8���$��������������?�8���$��������������?�8���$�#�##�##�##�#4444����

35 ���<����<����<����<������������������ �������������������� �������������������� �������������������� ���

36 37 �������������������������������������������������������-�8�����-�8�����-�8�����-�8��

38 �����������������������������������������������������������������!�� ���'*�!�� ���'*�!�� ���'*�!�� ���'*����

39 ���&���&���&���&����

40 ���������������������������������������������4�����4�����4�����4����

41 ���<���<���<���<���������������!�� ������������������!�� ������������������!�� ������������������!�� ���

42 43 ���������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!�������������

44 ���������������������������������������������������������������������!�'*�����!�'*�����!�'*�����!�'*������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

45 ������������&&&&��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

46 ���������������������������������'�!�� ���'*�@,��'�!�� ���'*�@,��'�!�� ���'*�@,��'�!�� ���'*�@,�5#5#5#5#�*��*��*��*�����������������������������������������������������������������������������������������������������������������������������������������������������������������

47 �����������������������������������������������������!��?!�'*�F�!�� ���'*4�!��?!�'*�F�!�� ���'*4�!��?!�'*�F�!�� ���'*4�!��?!�'*�F�!�� ���'*4������������������������������������������������������������������������������������������������������������������������������������

48 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

49 ��������������������������������������������������������5#5#5#5#�F�!��?!�'*�G�'�!������'*��F�!��?!�'*�G�'�!������'*��F�!��?!�'*�G�'�!������'*��F�!��?!�'*�G�'�!������'*�2222����5#5#5#5#�*�F�!��?!�'*�F��*�F�!��?!�'*�F��*�F�!��?!�'*�F��*�F�!��?!�'*�F�"�7"�7"�7"�74444����

50 ������������<�<�<�<�������������������!�������������������!�������������������!�������������������!���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

51 52 ������������������������!���������������!���������������!���������������!���������������� �������������������������������� �������������������������������� �������������������������������� ���������������������������������������������������������������������������

53 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

54 ������������&&&&����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

55 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.������������.������������.������������.��������������$�9���$�9���$�9���$�9�::::�9�$�B9/�1�4�9�$�9/�1�.�9�$�B9/�1�4�9�$�9/�1�.�9�$�B9/�1�4�9�$�9/�1�.�9�$�B9/�1�4�9�$�9/�1�.////����

56 �����������������������������������������������������������!'*/��������!'*/��������!'*/��������!'*/�.������-!�..������-!�..������-!�..������-!�./�!��?!�'*//�!��?!�'*//�!��?!�'*//�!��?!�'*/����������������������������������������������������������������������������������������

57 ������������������������������������.�����-�8��..�����-�8��..�����-�8��..�����-�8��./�!�� ���'*�*4/�!�� ���'*�*4/�!�� ���'*�*4/�!�� ���'*�*4������������������������������������������������������������������������������������������������������������������������������������������������

58 ������������<�<�<�<���������������������!��������������������!��������������������!��������������������!����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

59 <�<�<�<������������� ������������������������ ������������������������ ������������������������ ������������

Validate and set hours worked value

Override earnings method so HourlyEmployee can be concrete

Override toString method

Call superclass’s toString method

30

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������������������������������

(1 of 3)

1 ��� �!��"#�H$������������������������� �!��"#�H$������������������������� �!��"#�H$������������������������� �!��"#�H$����������������������

2 ����������������������������>�������������������������������������������>�������������������������������������������>�������������������������������������������>���������������

3 4 �����������������������������������������������������������������������������������������������������������������������������>������>������>������>�������������������������������������������������

5 &&&&����

6 �������������������������������������������������������������!�������4��!�������4��!�������4��!�������4����!����-��8����������!����-��8����������!����-��8����������!����-��8�������

7 �����������������������������������������������������������������������I��4������������I��4������������I��4������������I��4���������������������������������������!���������������!���������������!���������������!�

8 9 ����������������������������������������2222!���������������!���������������!���������������!���������������

10 �������������������������������������������������������'�����!�����/�����!����/�����!����/��������������������'�����!�����/�����!����/�����!����/��������������������'�����!�����/�����!����/�����!����/��������������������'�����!�����/�����!����/�����!����/�����

11 �����������������������������������������������������/������/������/������/�����������������������������*����*����*����*����

12 ���&���&���&���&����

13 ����������������������������������������'�����/����/�����*4'�����/����/�����*4'�����/����/�����*4'�����/����/�����*4����

14 ���������J�������'������*4���������J�������'������*4���������J�������'������*4���������J�������'������*4����

15 �������������������I��'����*4�������������������I��'����*4�������������������I��'����*4�������������������I��'����*4����

16 ���<���<���<���<������������������������������������������������2222!����������������������������������!����������������������������������!����������������������������������!����������������������������������

17 18 ��������������������������������������������������������������������������������������������

19 ����������������������������������������������������������������������I��'���������������I��'���������������I��'���������������I��'�����������������������������*����*����*����*����

20 ���&���&���&���&����

21 ����������������I���,�'����C�����������������I���,�'����C�����������������I���,�'����C�����������������I���,�'����C�#�##�##�##�#�DD����@��DD����@��DD����@��DD����@�"�#"�#"�#"�#����*�A����$�*�A����$�*�A����$�*�A����$�#�##�##�##�#4444����

22 ���<����<����<����<����������������������������I�����������������������������I�����������������������������I�����������������������������I��

23

Class CommissionEmployeeextends class Employee

Call superclass constructor

Validate and set commission rate value

Page 6: One Ring to rule them all, One Ring to find them, One Ring

6

31

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������������������������������

(2 of 3)

24 ������������������������������������������������������������������������������������������������

25 �����������������������������������������������������������������!������������I��'*�!������������I��'*�!������������I��'*�!������������I��'*����

26 ���&���&���&���&����

27 ���������������������������������������������������I��4�����������I��4�����������I��4�����������I��4����

28 ���<���<���<���<���������������!������������I�����������������!������������I�����������������!������������I�����������������!������������I��

29

30 ����������!������������������������!������������������������!������������������������!��������������

31 ������������������������������������������������������������J�������'�����J�������'�����J�������'�����J�������'�������������������������������*������*������*������*����

32 ���&���&���&���&����

33 ������!��������,�'������@�������!��������,�'������@�������!��������,�'������@�������!��������,�'������@�#�##�##�##�#�*�A��*�A��*�A��*�A�#�##�##�##�#�$�����4�$�����4�$�����4�$�����4����

34 ���<���<���<���<������������������J�������������������������J�������������������������J�������������������������J�������

35

36 �����������!�������������������������!�������������������������!�������������������������!��������������

37 �����������������������������������������������������������������!��J�������'*�!��J�������'*�!��J�������'*�!��J�������'*����

38 ���&���&���&���&����

39 �����������������������������������������!�������4�!�������4�!�������4�!�������4����

40 ���<����<����<����<���������������!��J���������������������!��J���������������������!��J���������������������!��J�������

41

Validate and set the gross sales value

32

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������������������������������

(3 of 3)

42 ���������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!����������������������������!�4�������������������������!�������������

43 ���������������������������������������������������������������������!�'*�����!�'*�����!�'*�����!�'*������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

44 ������������&&&&��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

45 �����������������������������������������!��������!��������!��������!������������I��'*�F�!��J�������'*4�����I��'*�F�!��J�������'*4�����I��'*�F�!��J�������'*4�����I��'*�F�!��J�������'*4������������������������������������������������������������������������������������

46 ������������<�<�<�<�������������������!�������������������!�������������������!�������������������!���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

47

48 ������������������������!�����������������������������������������������������!�����������������������������������������������������!�����������������������������������������������������!�����������������������������������������

49 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*����������������������������������������������������������������������������������������������������������������������������������������������������

50 ������������&&&&������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

51 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.9�$�9�.9�$�9�.9�$�9�.9�$�9�::::�9�$�B9/�1�4�9�$�9�1�.�9�$�B9/�1�4�9�$�9�1�.�9�$�B9/�1�4�9�$�9�1�.�9�$�B9/�1�4�9�$�9�1�.////��������������������

52 ������������������������������������.�������������������..�������������������..�������������������..�������������������./�/�/�/������������������������!'*/�������!'*/�������!'*/�������!'*/������������������������������������������������������������

53 ������������������������������������.!��������..!��������..!��������..!��������./�!��J/�!��J/�!��J/�!��J�������'*/�������'*/�������'*/�������'*/������������������������������������������������������������������������������������������������

54 ������������������������������������.�������������..�������������..�������������..�������������./�!������������I��'*�*4/�!������������I��'*�*4/�!������������I��'*�*4/�!������������I��'*�*4��������������������������������������������������������

55 ������������<<<<���������������������!���������������������!���������������������!���������������������!������������������������������������������������������������������������������������������������������������������������������������������������

56 <�<�<�<�������������������������������������������������������������������������������������������������������������������������

Override earnings method so CommissionEmployee can be concrete

Override toString method

Call superclass’s toString method

33

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������������������������������������������������������������������

(1 of 2)

1 ��� �!��"#�E$�������������������������������� �!��"#�E$�������������������������������� �!��"#�E$�������������������������������� �!��"#�E$�����������������������������

2 �����������������������������������>������������������������������������������������������������>������������������������������������������������������������>������������������������������������������������������������>�������������������������

3 4 ���������������������������������������������������������������������������������������������������������������������������������������������������������>������>������>������>�����������������������������������������������������������������������������������������

5 &&&&����

6 �������������������������������������������������������������������4��������4��������4��������4���������������-����������������-����������������-����������������-��8888

7 8 ��������>��������>��������>��������>2222!���������������!���������������!���������������!���������������

9 ��������������������������������������������������������������'�����!�����/�����!����/���������������������������'�����!�����/�����!����/���������������������������'�����!�����/�����!����/���������������������������'�����!�����/�����!����/�����

10 ����������!����/�����������!����/�����������!����/�����������!����/������������������������������/������/������/������/����������������������������/����/����/����/������������������������������*�����*�����*�����*����

11 ���&���&���&���&����

12 ����������������������������������������'�����/����/����/�����/����*4'�����/����/����/�����/����*4'�����/����/����/�����/����*4'�����/����/����/�����/����*4����

13 ������������������������������������������������'�����*4�����'�����*4�����'�����*4�����'�����*4�����������������������������������������������������������������������������������������������������

14 ���<����<����<����<����������>���������>���������>���������>2222!�����������������������������������������!�����������������������������������������!�����������������������������������������!�����������������������������������������

15 16 ��������������������������������������������������������������������

17 ������������������������������������������������������������������'�����������'�����������'�����������'������������������������������*�����*�����*�����*����

18 ���&���&���&���&����

19 �������������,�'�����@��������������,�'�����@��������������,�'�����@��������������,�'�����@�#�#�#�#�#�#�#�#�****�A��A��A��A�#�##�##�##�#�$����4��$����4��$����4��$����4�������������������������2222��!������!������!������!����

20 ���<����<����<����<��������������������������������������������������������������������������������������������� 21

Class BasePlusCommissionEmployeeextends class CommissionEmployee

Call superclass constructor

Validate and set base salary value

34

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������������������������������������������������������������������

(2 of 2)

22 ������������������������������������������������������������������������

23 �����������������������������������������������������������������!��������'*�!��������'*�!��������'*�!��������'*����

24 ���&���&���&���&����

25 �����������������������������������������������4�������4�������4�������4����

26 ���<����<����<����<���������������!����������������������!����������������������!����������������������!��������

27 28 ���������������������������!�4�������������������!��������������������������������������!�4�������������������!��������������������������������������!�4�������������������!��������������������������������������!�4�������������������!�����������������������

29 ���������������������������������������������������������������������������������!�'*�!�'*�!�'*�!�'*����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

30 ������������&&&&������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

31 �����������������������������������������!��������'*�G��!��������'*�G��!��������'*�G��!��������'*�G����������������������!�'*4�����!�'*4�����!�'*4�����!�'*4����������������������������������������������������������������������������������������������������

32 ������������<�<�<�<�������������������!�������������������!�������������������!�������������������!�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

33 34 ������������������������!������������������������������������������������������������!������������������������������������������������������������!������������������������������������������������������������!������������������������������������������������

35 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

36 ������������&&&&��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

37 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.9��9�4�9�$�B9/�1�..9��9�4�9�$�B9/�1�..9��9�4�9�$�B9/�1�..9��9�4�9�$�B9/�1�.////������������������������������������������������������������������������������������������������

38 ������������������������������������.���.���.���.���2222�����.�����.�����.�����./�/�/�/������������������������!'*/�������!'*/�������!'*/�������!'*/��������������������������������������������������������������������������������������������������������������������

39 ������������������������������������.�������..�������..�������..�������./�!��������'*�*4/�!��������'*�*4/�!��������'*�*4/�!��������'*�*4������������������������������������������������������������������������������������������������������������������������

40 ������������<�<�<�<������������������������������������!���������������!���������������!���������������!������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

41 <�<�<�<�����������������������������������������������������������������������������������������������������������������������������������������������������

Override earnings method

Call superclass’s earnings method

Override toString method

Call superclass’s toString method

35

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(1 of 5)

1 ��� �!��"#�K$���������������������� �!��"#�K$���������������������� �!��"#�K$���������������������� �!��"#�K$�������������������

2 ��������������������������!����������������������������!����������������������������!����������������������������!��

3 4 ��������������������������������������������������������������������������������������������������������������������

5 &&&&����

6 ������������������������������������������������������������������������������������'�����!�!�()�*�����'�����!�!�()�*�����'�����!�!�()�*�����'�����!�!�()�*�����

7 ���&���&���&���&����

8 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

9 ����������������������������������������������������,����������������������������,����������������������������,����������������������������,����������������������������������������������������������������������������������������������������������������������������������������

10 ��������������������������������������-��-��-��-��������������'���������������'���������������'���������������'�.0���..0���..0���..0���./�/�/�/�.�����..�����..�����..�����./�/�/�/�."""."""."""."""2222""""""""2222"""".""""."""".""""./�/�/�/�E##�##E##�##E##�##E##�##�*4�*4�*4�*4������������

11 ������������������������ ���������������������������, ���������������������������, ���������������������������, ���������������������������,��������������������������������������������������������������������������������������������������������������������������������������������������������

12 ��������������������������������������-��-��-��-� ������������'�� ������������'�� ������������'�� ������������'�.L��..L��..L��..L��./�/�/�/�.����..����..����..����./�/�/�/�.111.111.111.11122221111111122221111.1111.1111.1111./�/�/�/�"3�H7"3�H7"3�H7"3�H7/�/�/�/�5#5#5#5#�*4�*4�*4�*4����

13 ��������������������������������������������������������������,��������������������������������������,��������������������������������������,��������������������������������������,������������������������������������������������������������������������������������������������������������������������

14 ��������������������������������������-��-��-��-�������������������'�������������������'�������������������'�������������������'����������������������������������������������������������������������������������������������������������������������������������������������������������������������������

15 ������������������������������������.���..���..���..���./�/�/�/�.0��.0��.0��.0����.��.��.��./�/�/�/�.+++.+++.+++.+++2222++++++++2222++++.++++.++++.++++./�/�/�/�"####"####"####"####/�/�/�/��#3�#3�#3�#3�*4�*4�*4�*4����������������������������������������������������������������������������������������

16 ����������������������������������������������������������������������������,����������������������������������������������������,����������������������������������������������������,����������������������������������������������������,��������������������������������������������������������

17 ��������������������������������������-��-��-��-��������������������������'��������������������������'��������������������������'��������������������������'��������������������������������������������������������������������������������������������������������������������������������������������

18 ������������������������������������.���..���..���..���./�/�/�/�.6�-��..6�-��..6�-��..6�-��./�/�/�/�.555.555.555.55522225555555522225555555555555.5.5.5./�/�/�/�7###7###7###7###/�/�/�/��#5�#5�#5�#5/�/�/�/�+##+##+##+##�*4�*4�*4�*4������������������������������������������������������������������������

19 20 �����������������������'������������������������'������������������������'������������������������'�.������������������������������$.������������������������������$.������������������������������$.������������������������������$::::�.�.�.�.�*4�*4�*4�*4����

21

36

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(2 of 5)

22 ����������������������'�����������������������'�����������������������'�����������������������'�.9�.9�.9�.9�::::�9�$�B9/�1��9�$�B9/�1��9�$�B9/�1��9�$�B9/�1�::::����::::�.�.�.�./�/�/�/�����

23 ����������������������/�����������������������/�����������������������/�����������������������/�.����..����..����..����./�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4����

24 ����������������������'�����������������������'�����������������������'�����������������������'�.9�.9�.9�.9�::::�9�$�B9/�1��9�$�B9/�1��9�$�B9/�1��9�$�B9/�1�::::����::::�.�.�.�.////����

25 ����������������������/�����������������������/�����������������������/�����������������������/�.����..����..����..����./�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4����

26 �������������������������������������'������������������'������������������'������������������'�.9�.9�.9�.9�::::�9�$�B9/�1��9�$�B9/�1��9�$�B9/�1��9�$�B9/�1�::::����::::�.�.�.�.////����

27 ���������������������������/����������������������������/����������������������������/����������������������������/�.����..����..����..����./������������������������!�'*�*4/������������������������!�'*�*4/������������������������!�'*�*4/������������������������!�'*�*4����

28 ����������������������'�����������������������'�����������������������'�����������������������'�.9�.9�.9�.9�::::�9�$�B9/�1��9�$�B9/�1��9�$�B9/�1��9�$�B9/�1�::::����::::�.�.�.�./�/�/�/�����

29 ����������������������������������/�����������������������������������/�����������������������������������/�����������������������������������/�����

30 ������������������������������������.����..����..����..����./�����������������/�����������������/�����������������/�������������������������������!�'*�*4��������������!�'*�*4��������������!�'*�*4��������������!�'*�*4����

31 32 ��������������������������������������������������������������������2222������������������������������������������������������������������������

33 ������������������������������������������()�,�������������������()�,�������������������()�,�������������������()�,���-��-��-��-���������(����������(����������(����������(�5555�)4�)4�)4�)4��������

34 35 ����������������������������������M����-�����������������������M����-�����������������������M����-�����������������������M����-�����������������������������������������������������

36 ���������������������������������(����������(����������(����������(�####�)�,��������������4�)�,��������������4�)�,��������������4�)�,��������������4��������������������������������������������

37 ���������������������������������(����������(����������(����������(�""""�)�,��������������4�)�,��������������4�)�,��������������4�)�,��������������4����������������������������������������������������

38 ���������������������������������(����������(����������(����������(�1111�)�,�������������������4�)�,�������������������4�)�,�������������������4�)�,�������������������4������������������������������������

39 ���������������������������������(����������(����������(����������(�++++�)�,��������������������������4�)�,��������������������������4�)�,��������������������������4�)�,��������������������������4����

40 41 �����������������������'������������������������'������������������������'������������������������'�.��������������������������������$.��������������������������������$.��������������������������������$.��������������������������������$::::�.�.�.�.�*4�*4�*4�*4����

42 43 ������������!��������������������������������������������������!��������������������������������������������������!��������������������������������������������������!������������������������������������������

44 ���������������������������������'������������������������$�����������*��'������������������������$�����������*��'������������������������$�����������*��'������������������������$�����������*�����

45 ������&������&������&������&����

46 ��������������������������'���������������*4���������������������������'���������������*4���������������������������'���������������*4���������������������������'���������������*4��������8���������!�������8���������!�������8���������!�������8���������! 47

Assigning subclass objects to supercalss variables

Implicitly and polymorphically call toString

Page 7: One Ring to rule them all, One Ring to find them, One Ring

7

37

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(3 of 5)

48 ���������������������-����������������������������������������������������������������-����������������������������������������������������������������-����������������������������������������������������������������-�������������������������������������������

49 ���������������������������������������������'��'��'��'������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������*��*��*��*�����

50 ���������&���������&���������&���������&����

51 �����������������-������������������������������������������-������������������������������������������-������������������������������������������-�������������������������

52 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

53 �����������������������������������������������,������������������������������������������������,������������������������������������������������,������������������������������������������������,�����

54 ������������������������������������������������������������'���������������������������*��������������'���������������������������*��������������'���������������������������*��������������'���������������������������*��������������4444����

55 56 �����������������������������������������������������������������������������������,����������!��������'*4�����������,����������!��������'*4�����������,����������!��������'*4�����������,����������!��������'*4����

57 ������������������������������'�������������������������������'�������������������������������'�������������������������������'�"�"#�"�"#�"�"#�"�"#�F�����������*4F�����������*4F�����������*4F�����������*4����

58 ����������������������������'�����������������������������'�����������������������������'�����������������������������'�����

59 ������������������������������������������������������������.��-���������-����"#99����������$�B9/�1�.��-���������-����"#99����������$�B9/�1�.��-���������-����"#99����������$�B9/�1�.��-���������-����"#99����������$�B9/�1�::::�.�.�.�.////����

60 ������������������������!��������'*�*4������������������������!��������'*�*4������������������������!��������'*�*4������������������������!��������'*�*4����

61 ���������<���������<���������<���������<����������������������������������������

62 63 �������������������������'��������������������������'��������������������������'��������������������������'�����

64 ������������������������������������������������.�����B9/�1�.�����B9/�1�.�����B9/�1�.�����B9/�1�::::����::::�.�.�.�./�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4/�������������������!�'*�*4����

65 ������<�������<�������<�������<�������������������������������������

66 67 ���������������������������!�������������������������������������������!�������������������������������������������!�������������������������������������������!����������������������������������������

68 ���������������������������������'��'��'��'����������������,����,����,����,�####4���@��������������!��4��GG�*4���@��������������!��4��GG�*4���@��������������!��4��GG�*4���@��������������!��4��GG�*����������������������������

69 ����������������������������������������������������'�����������������'�����������������'�����������������'�.���������9.���������9.���������9.���������9������9�������9�������9�������9�::::�.�.�.�./��//��//��//��/��������

70 ���������������������������������������������������������(���)�!������'*�!��=��'*�*4���������(���)�!������'*�!��=��'*�*4���������(���)�!������'*�!��=��'*�*4���������(���)�!������'*�!��=��'*�*4����������������������������

71 ���<���<���<���<��������������������������������������������

72 <<<<����������������������������������������������������������������������������������������������������������������

If the currentEmployee variable points to a BasePlusCommissionEmployee object

Downcast currentEmployee to a BasePlusCommissionEmployeereference

Give BasePlusCommissionEmployeesa 10% base salary bonus

Polymorphically call earnings method

Call getClass and getName methods to display each Employee subclass object’s class name

38

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(4 of 5)

������������������������������$������������������������������$������������������������������$������������������������������$���� ��������������$�0�����������������������$�0�����������������������$�0�����������������������$�0��������������������������������$�"""�������������������$�"""�������������������$�"""�������������������$�"""2222""""""""2222""""""""""""""""����-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##��������$�BE##�##����$�BE##�##����$�BE##�##����$�BE##�##���� ��������������$�L���������������������$�L���������������������$�L���������������������$�L������������������������������$�111�������������������$�111�������������������$�111�������������������$�11122221111111122221111111111111111����������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##��������$�B����$�B����$�B����$�B3H#�##3H#�##3H#�##3H#�##���� �������������������$�����0�����������������������$�����0�����������������������$�����0�����������������������$�����0���������������������������$�+++�������������������$�+++�������������������$�+++�������������������$�+++2222++++++++2222++++++++++++++++����!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3��������$�B3##�##����$�B3##�##����$�B3##�##����$�B3##�##���� ������������2222�������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-�������������������������$�555�������������������$�555�������������������$�555�������������������$�55522225555555522225555555555555555����!��������$�B7/###�##4������������!��������$�B7/###�##4������������!��������$�B7/###�##4������������!��������$�B7/###�##4��������������$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##��������$�B7##�##����$�B7##�##����$�B7##�##����$�B7##�##

39

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������������������

������������

(5 of 5)

��������������������������������$��������������������������������$��������������������������������$��������������������������������$���� ��������������$�0�����������������������$�0�����������������������$�0�����������������������$�0��������������������������������$�"""�������������������$�"""�������������������$�"""�������������������$�"""2222""""""""2222""""""""""""""""����-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##���������BE##�##�����BE##�##�����BE##�##�����BE##�##���� ��������������$�L���������������������$�L���������������������$�L���������������������$�L������������������������������$�111�������������������$�111�������������������$�111�������������������$�11122221111111122221111111111111111����������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##������-!�$�B"3�H74������-�8��$�5#�##������������������������B3H#�##B3H#�##B3H#�##B3H#�##���� �������������������$�����0�����������������������$�����0�����������������������$�����0�����������������������$�����0���������������������������$�+++�������������������$�+++�������������������$�+++�������������������$�+++2222++++++++2222++++++++++++++++����!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3!��������$�B"#/###�##4��������������$�#�#3���������B3##�##�����B3##�##�����B3##�##�����B3##�##���� ������������2222�������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-���������������������������$�����6�-�������������������������$�555�������������������$�555�������������������$�555�������������������$�55522225555555522225555555555555555����!��������$�B7/###�##4������������!��������$�B7/###�##4������������!��������$�B7/###�##4������������!��������$�B7/###�##4��������������$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##��$�#�#54��������$�B+##�##������-���������-����"#9����������$�B++#�##��-���������-����"#9����������$�B++#�##��-���������-����"#9����������$�B++#�##��-���������-����"#9����������$�B++#�##���������B7+#�##������������������������������������B7+#�##������������������������������������B7+#�##������������������������������������B7+#�##����������������������������������� ���������#���������������������������#���������������������������#���������������������������#�������������������������������"����� ���������������������"����� ���������������������"����� ���������������������"����� �������������������������1��������������������������������1��������������������������������1��������������������������������1������������������������������������+�����������������������������+�����������������������������+�����������������������������+����������������������������������������������������������������

Same results as when the employees were processed individually

Base salary is increased by 10%

Each employee’s type is displayed

40

1992-2007 Pearson Education, Inc. All rights reserved.

10.5.6 Demonstrating Polymorphic Processing, Operator instanceof and Downcasting

• Dynamic binding– Also known as late binding– Calls to overridden methods are resolved at execution

time, based on the type of object referenced

•instanceof operator– Determines whether an object is an instance of a certain

type

41

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.3

Assigning a superclass variable to a subclass variable (without an explicit cast) is a compilation error.

42

1992-2007 Pearson Education, Inc. All rights reserved.

If at execution time the reference of a subclass object has been assigned to a variable of one of its direct or indirect superclasses, it is acceptable to cast the reference stored in that superclassvariable back to a reference of the subclass type. Before performing such a cast, use the ������������������������������������ operator to ensure that the object is indeed an object of an appropriate subclass type.

Software Engineering Observation 10.5

Page 8: One Ring to rule them all, One Ring to find them, One Ring

8

43

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.4

When downcasting an object, a ��������>���������������>���������������>���������������>������� 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 superclasses.

44

1992-2007 Pearson Education, Inc. All rights reserved.

10.5.6 Demonstrating Polymorphic Processing, Operator instanceof and Downcasting (Cont.)

• Downcasting– Convert a reference to a superclass to a reference to a

subclass– Allowed only if the object has an is-a relationship with the

subclass

•getClass method– Inherited from Object– Returns an object of type Class

•getName method of class Class– Returns the class’s name

45

1992-2007 Pearson Education, Inc. All rights reserved.

10.5.7 Summary of the Allowed Assignments Between Superclass and Subclass Variables

• Superclass and subclass assignment rules– Assigning a superclass reference to a superclass variable is

straightforward– Assigning a subclass reference to a subclass variable is

straightforward– Assigning a subclass reference to a superclass variable is

safe because of the is-a relationship• Referring to subclass-only members through superclass

variables is a compilation error– Assigning a superclass reference to a subclass variable is a

compilation error• Downcasting can get around this error

46

1992-2007 Pearson Education, Inc. All rights reserved.

10.6 final Methods and Classes

•final methods– Cannot be overridden in a subclass– private and static methods are implicitly final– final methods are resolved at compile time, this is known

as static binding• Compilers can optimize by inlining the code

•final classes– Cannot be extended by a subclass– All methods in a final class are implicitly final

47

1992-2007 Pearson Education, Inc. All rights reserved.

Performance Tip 10.1

The compiler can decide to inline a ���������������� method call and will do so for small, simple ����������������

methods. Inlining does not violate encapsulation or information hiding, but does improve performance because it eliminates the overhead of making a method call.

48

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.5

Attempting to declare a subclass of a ���������������� class is a compilation error.

Page 9: One Ring to rule them all, One Ring to find them, One Ring

9

49

1992-2007 Pearson Education, Inc. All rights reserved.

In the Java API, the vast majority of classes are not declared ����������������. This enables inheritance and polymorphism—the fundamental capabilities of object-oriented programming. However, in some cases, it is important to declare classes ����������������—typically for security reasons.

Software Engineering Observation 10.6

50

1992-2007 Pearson Education, Inc. All rights reserved.

10.7 Case Study: Creating and Using Interfaces

• Interfaces– Keyword interface– Contains only constants and abstract methods

• All fields are implicitly public, static and final• All methods are implicitly public abstract methods

– Classes can implement interfaces• The class must declare each method in the interface using the

same signature or the class must be declared abstract

– Typically used when disparate classes need to share common methods and constants

– Normally declared in their own files with the same names as the interfaces and with the .java file-name extension

51

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 10.1

According to Chapter 9 of the Java Language Specification, it is proper style to declare an interface’s methods without keywords ������������������������ and �������������������� because they are redundant in interface method declarations. Similarly, constants should be declared without keywords ������������������������, ��������������������

and ���������������� because they, too, are redundant.

52

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 10.6

Failing to implement any method of an interface in a concrete class that ���������������������������������������� the interface results in a syntax error indicating that the class must be declared ��������������������.

53

1992-2007 Pearson Education, Inc. All rights reserved.

10.7.1 Developing a Payable Hierarchy

•Payable interface– Contains method getPaymentAmount– Is implemented by the Invoice and Employee classes

• UML representation of interfaces– Interfaces are distinguished from classes by placing the

word “interface” in guillemets (« and ») above the interface name

– The relationship between a class and an interface is known as realization

• A class “realizes” the methods of an interface

54

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 10.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.

Page 10: One Ring to rule them all, One Ring to find them, One Ring

10

55

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.10 | �������������������� interface hierarchy UML class diagram.

56

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������

1 ��� �!��"#�""$������������ �!��"#�""$������������ �!��"#�""$������������ �!��"#�""$���������

2 ��������������������������������������������������������������������������������������������������������

3

4 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

5 &&&&������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

6 �������������������������������������!��������%�����'*4��!��������%�����'*4��!��������%�����'*4��!��������%�����'*4������������������4����������������������������������4����������������������������������4����������������������������������4�����������������

7 <�<�<�<�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

Declare interface Payable

Declare getPaymentAmount method which is implicitly public and abstract

57

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������

(1 of 3)

1 ��� �!��"#�"1$�������������� �!��"#�"1$�������������� �!��"#�"1$�������������� �!��"#�"1$�����������

2 ������������������������������������������������������������������������������������������������������������������������������������

3 4 ����������������������������������������������������������������������������������������������������������������������������������������������������

5 &&&&����

6 �������������������������������������!���=����4������!���=����4������!���=����4������!���=����4�����

7 �������������������������������������!���N���������4�����!���N���������4�����!���N���������4�����!���N���������4����

8 �������������������������������������������������O������4�O������4�O������4�O������4����

9 �����������������������������������������������������������������������������������4�������4�������4�������4����

10 11 ������������������������������������2222!���������������!���������������!���������������!���������������

12 ��������������������������������������������'�����!���/�����!�����������/���������'�����!���/�����!�����������/���������'�����!���/�����!�����������/���������'�����!���/�����!�����������/�������������������/�������/�������/�������/�����

13 ������������������������������������������������������*������*������*������*����

14 ���&���&���&���&����

15 ��������=�����,���4��������=�����,���4��������=�����,���4��������=�����,���4����

16 ��������N����������,�����������4��������N����������,�����������4��������N����������,�����������4��������N����������,�����������4����

17 ���������P������'������������P������'������������P������'������������P������'�������*4�����*4�����*4�����*4�������������������O������������������������O������������������������O������������������������O������

18 �������������������'������*4��������������������'������*4��������������������'������*4��������������������'������*4�������������������������������������������������������������������������������������������������������������������������

19 ���<����<����<����<�����������������������������������������2222!�����������������������!�����������������������!�����������������������!�����������������������

20 21 ������������������������������������������������������������������������

22 ��������������������������������������������������������������=����'�����!����*������=����'�����!����*������=����'�����!����*������=����'�����!����*����

23 ���&���&���&���&����

24 ��������=�����,���4����������=�����,���4����������=�����,���4����������=�����,���4������

25 ���<����<����<����<��������������������=�����������������������=�����������������������=�����������������������=���� 26

Class Invoice implements interface Payable

58

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������

(2 of 3)

27 ���������������!��������������!��������������!��������������!�����������

28 �����������������������������������������!�!����=����'*�����!�!����=����'*�����!�!����=����'*�����!�!����=����'*����

29 ���&���&���&���&����

30 �������������������������������������������=����4���=����4���=����4���=����4����

31 ���<����<����<����<���������������!����=������������������!����=������������������!����=������������������!����=����

32 33 ��������������������������������������������������������������������������������

34 ��������������������������������������������������������������N���������'�����!������������*������N���������'�����!������������*������N���������'�����!������������*������N���������'�����!������������*����

35 ���&���&���&���&����

36 �����������������������N����������,�����������4���N����������,�����������4���N����������,�����������4���N����������,�����������4����

37 ���<����<����<����<��������������������N����������������������������N����������������������������N����������������������������N���������

38 39 ���������������!����������������!����������������!����������������!�������������

40 �����������������������������������������!�!����N���������'*�����!�!����N���������'*�����!�!����N���������'*�����!�!����N���������'*����

41 ���&���&���&���&����

42 �������������������������������������������N���������4���N���������4���N���������4���N���������4����

43 ���<����<����<����<���������������!����N�����������������������!����N�����������������������!����N�����������������������!����N���������

44 45 ����������������������������O����������O����������O����������O������

46 ������������������������������������������������������������P������'�����P������'�����P������'�����P������'��������������������*�������*�������*�������*����

47 ���&���&���&���&����

48 ������O�������,�'�������@�������O�������,�'�������@�������O�������,�'�������@�������O�������,�'�������@�####�*�A��*�A��*�A��*�A�####�$������4��$������4��$������4��$������4����O������������������!�������O������������������!�������O������������������!�������O������������������!����

49 ���<����<����<����<������������������P�����������������������P�����������������������P�����������������������P������

50 51 ���������������!���O���������!���O���������!���O���������!���O������

52 �����������������������������������������������������!��P������'*�!��P������'*�!��P������'*�!��P������'*����

53 ���&���&���&���&����

54 �����������������������������������������O������4�O������4�O������4�O������4����

55 ���<����<����<����<���������������!��P��������������������!��P��������������������!��P��������������������!��P������ 56

59

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������

(3 of 3)

57 ����������������������������������������������������������������������������������������

58 ����������������������������������������������������������������������'���������������'���������������'���������������'�������������������������������*������*������*������*����

59 ���&���&���&���&����

60 �����������������,�'������@������������������,�'������@������������������,�'������@������������������,�'������@�#�##�##�##�#�*�A��*�A��*�A��*�A�#�##�##�##�#�$�����4��$�����4��$�����4��$�����4���������������������������������������������������������

61 ���<����<����<����<�������������������������������������������������������������������������������������������������������������

62 63 ���������������!������������������!������������������!������������������!���������������

64 �����������������������������������������������������������������!������������'*�!������������'*�!������������'*�!������������'*����

65 ���&���&���&���&����

66 ���������������������������������������������������4�����������4�����������4�����������4����

67 ���<����<����<����<���������������!��������������������������!��������������������������!��������������������������!������������

68 69 ������������������������!������������������������������������������!������������������������������������������!������������������������������������������!������������������������������

70 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*����

71 ���&���&���&���&����

72 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.9�$�.9�$�.9�$�.9�$�::::�9�9�9�9�$�9��'9�*��$�9��'9�*��$�9��'9�*��$�9��'9�*�::::�9�$�9���9�$�9���9�$�9���9�$�9��::::�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�./�/�/�/�����

73 ������������������������������������.�������..�������..�������..�������.////�.��������.�.��������.�.��������.�.��������./�!����=����'*/�!����N���������'*/�/�!����=����'*/�!����N���������'*/�/�!����=����'*/�!����N���������'*/�/�!����=����'*/�!����N���������'*/�����

74 ������������������������������������.O������..O������..O������..O������./�!��P������'*/�/�!��P������'*/�/�!��P������'*/�/�!��P������'*/�.������������.������������.������������.������������./�!������������'*�*4./�!������������'*�*4./�!������������'*�*4./�!������������'*�*4����

75 ���<����<����<����<���������������������!��������������������!��������������������!��������������������!

76 77 �����������������������O�����������O�����������O�����������O����������������������-���������������������������������������-���������������������������������������-���������������������������������������-�������������������������������������

78 �����������������������������������������������������������������!��������%�����'*�!��������%�����'*�!��������%�����'*�!��������%�����'*����������������������������������������������������������������������������������������������������������������������������������������������������

79 ������������&&&&��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

80 �����������������������������������������!��P������'*�F�!����������!��P������'*�F�!����������!��P������'*�F�!����������!��P������'*�F�!������������'*4����'*4����'*4����'*4���������������������������������������������������������������������������������

81 ������������<�<�<�<���������������!��������%�������������������!��������%�������������������!��������%�������������������!��������%�����������������������������������������������������������������������������������������������������������������������������������������������������

82 <�<�<�<�����������������������������������������������������������������������������

Declare getPaymentAmount to fulfill contract with interface Payable

60

1992-2007 Pearson Education, Inc. All rights reserved.

10.7.3 Creating Class Invoice

• A class can implement as many interfaces as it needs

– Use a comma-separated list of interface names after keyword implements

• Example: public class ClassName extendsSuperclassName implements FirstInterface,SecondInterface, …

Page 11: One Ring to rule them all, One Ring to find them, One Ring

11

61

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(1 of 3)

1 ��� �!��"#�"+$��������������� �!��"#�"+$��������������� �!��"#�"+$��������������� �!��"#�"+$������������

2 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

3

4 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

5 &&&&����

6 �������������������������������������!�����=��4�����!�����=��4�����!�����=��4�����!�����=��4����

7 �������������������������������������!����=��4�����!����=��4�����!����=��4�����!����=��4����

8 �������������������������������������!�������������=������!�������������=������!�������������=������!�������������=����4���4���4���4����

9

10 ����������������������������������������2222!���������������!���������������!���������������!���������������

11 ���������������������������������������������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*���������'�����!�����/�����!����/�����!�����*����

12 ���&���&���&���&����

13 ����������=���,�����4����������=���,�����4����������=���,�����4����������=���,�����4����

14 ���������=���,����4���������=���,����4���������=���,����4���������=���,����4����

15 ������������������=�����,����4������������������=�����,����4������������������=�����,����4������������������=�����,����4����

16 ���<����<����<����<���������������������������������������������2222!���������!���������!���������!���������������������������������������������������������������������

17

Class Employee implements interface Payable

62

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(2 of 3)

18 ������������������������������������������������������������������������

19 ������������������������������������������������������������ ���=��'�����!������*���� ���=��'�����!������*���� ���=��'�����!������*���� ���=��'�����!������*����

20 ���&���&���&���&����

21 ����������=���,�����4����������=���,�����4����������=���,�����4����������=���,�����4����

22 ���<����<����<����<������������������ ���=������������������� ���=������������������� ���=������������������� ���=��

23 24 ����������������������������������������������������������������������������

25 �����������������������������������������!�!�� ���=��'*�����!�!�� ���=��'*�����!�!�� ���=��'*�����!�!�� ���=��'*����

26 ���&���&���&���&����

27 ������������������������������������������������������������=��4=��4=��4=��4����

28 ���<����<����<����<���������������!�� ���=����������������!�� ���=����������������!�� ���=����������������!�� ���=��

29 30 ��������������������������������������������������������������������

31 ������������������������������������������������������������6��=��'�����!�����*����6��=��'�����!�����*����6��=��'�����!�����*����6��=��'�����!�����*����

32 ���&���&���&���&����

33 ���������=���,����4���������=���,����4���������=���,����4���������=���,����4����

34 ���<����<����<����<������������������6��=�������������������6��=�������������������6��=�������������������6��=��

35 36 ������������������������������������������������������������������������

37 �����������������������������������������!�!��6�����!�!��6�����!�!��6�����!�!��6��=��'*��=��'*��=��'*��=��'*����

38 ���&���&���&���&����

39 ��������������������������������������������=��4����=��4����=��4����=��4����

40 ���<����<����<����<���������������!��6��=����������������!��6��=����������������!��6��=����������������!��6��=�� 41

63

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(3 of 3)

42 ��������������������������������������������������������������������������������������������������������������������

43 ������������������������������������������������������������������������=����'�����!�����*����������������=����'�����!�����*����������������=����'�����!�����*����������������=����'�����!�����*����

44 ���&���&���&���&����

45 ������������������=�����,����4�������������������=�����,����4�������������������=�����,����4�������������������=�����,����4�����������������������������������������������������������������

46 ���<����<����<����<������������������������������=���������������������������������=���������������������������������=���������������������������������=����

47 48 ������������������������������������������������������������������������������������������������������������������������

49 �����������������������������������������!�!��������������=����'*�����!�!��������������=����'*�����!�!��������������=����'*�����!�!��������������=����'*����

50 ���&���&���&���&����

51 �����������������������������������������������������=����4�������������=����4�������������=����4�������������=����4����

52 ���<����<����<����<���������������!��������������=������������������!��������������=������������������!��������������=������������������!��������������=����

53 54 ������������������������!�������������������������������������������!�������������������������������������������!�������������������������������������������!�������������������������������

55 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*����

56 ���&���&���&���&����

57 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.9��9�.9��9�.9��9�.9��9�::::��������������������$�9�.��������������������$�9�.��������������������$�9�.��������������������$�9�./�/�/�/�����

58 ���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4���������!�� ���=��'*/�!��6��=��'*/�!��������������=����'*�*4����

59 ���<����<����<����<���������������������!��������������������!��������������������!��������������������!

60 61 ���������������=���$�?��������������������������������!��������%�����������=���$�?��������������������������������!��������%�����������=���$�?��������������������������������!��������%�����������=���$�?��������������������������������!��������%����������������������������

62 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

63 <�<�<�<���������������������������������������������������������������������������������������������������������

getPaymentAmount method is

not implemented here

64

1992-2007 Pearson Education, Inc. All rights reserved.

10.7.5 Modifying Class SalariedEmployee for Use in the Payable Hierarchy

• Objects of any subclasses of the class that implements the interface can also be thought of as objects of the interface

– A reference to a subclass object can be assigned to an interface variable if the superclass implements that interface

65

1992-2007 Pearson Education, Inc. All rights reserved.

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 subclasses of a class that implements an interface also can be thought of as an object of the interface type.

Software Engineering Observation 10.7

66

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������

������������

(1 of 2)

1 ��� �!��"#�"5$�������������������� �!��"#�"5$�������������������� �!��"#�"5$�������������������� �!��"#�"5$�����������������

2 �����������������������>��������������/�-���������������������������������������������>��������������/�-���������������������������������������������>��������������/�-���������������������������������������������>��������������/�-����������������������

3 4 ���������������������������������������������������������������������������������������������������������>������>������>������>�������������������������������������������������

5 &&&&����

6 �������������������������������������������������������������-��8�����4�-��8�����4�-��8�����4�-��8�����4����

7 8 ������������������������������������2222!���������������!���������������!���������������!���������������

9 ��������������������������������������������������'�����!�����/�����!����/�����!����/���������������'�����!�����/�����!����/�����!����/���������������'�����!�����/�����!����/�����!����/���������������'�����!�����/�����!����/�����!����/�����

10 �����������������������������������������������������*�����*�����*�����*����

11 ���&���&���&���&����

12 ����������������������������������������'�����/����/�����*4�'�����/����/�����*4�'�����/����/�����*4�'�����/����/�����*4�����������������������������������������������������������������������������������������������������������������

13 ���������?��8�����'�����*4����������?��8�����'�����*4����������?��8�����'�����*4����������?��8�����'�����*4�������������������������������������������������������������������������������������

14 ���<����<����<����<�����������������������������������������2222!�����������������������������!�����������������������������!�����������������������������!�����������������������������

15 16 ����������������������������������������������������

17 ������������������������������������������������������������?��8�����'�����?��8�����'�����?��8�����'�����?��8�����'������������������������������*�����*�����*�����*����

18 ���&���&���&���&����

19 ������-��8������,�����@�������-��8������,�����@�������-��8������,�����@�������-��8������,�����@�#�##�##�##�#�A��A��A��A�#�##�##�##�#�$����4�$����4�$����4�$����4����

20 ���<����<����<����<������������������?��8����������������������?��8����������������������?��8����������������������?��8����� 21

Class SalariedEmployee extends class Employee(which implements interface Payable)

Page 12: One Ring to rule them all, One Ring to find them, One Ring

12

67

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

����������������������������������������������������

������������

(2 of 2)

22 ��������������������������������������������������������

23 �����������������������������������������������������������������!��?��8�����'*�!��?��8�����'*�!��?��8�����'*�!��?��8�����'*����

24 ���&���&���&���&����

25 �����������������������������������������-��8�����4�-��8�����4�-��8�����4�-��8�����4����

26 ���<����<����<����<���������������!��?��8�������������������!��?��8�������������������!��?��8�������������������!��?��8�����

27 28 ���������������������������!�4������������������������������������-����������������!�4������������������������������������-����������������!�4������������������������������������-����������������!�4������������������������������������-�

29 ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

30 �����������������������������������������������������������������!��������%�����'*�!��������%�����'*�!��������%�����'*�!��������%�����'*��������������������������������������������������������������������������������������������������������������������������������������������

31 ������������&&&&������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

32 �����������������������������������������!��?��8�����'*4�!��?��8�����'*4�!��?��8�����'*4�!��?��8�����'*4������������������������������������������������������������������������������������������������������������������������������������������������������������

33 ������������<�<�<�<���������������!��������%�������������������!��������%�������������������!��������%�������������������!��������%���������������������������������������������������������������������������������������������������������������������������������������������

34 35 ������������������������!������������������������������������������������!������������������������������������������������!������������������������������������������������!������������������������������������

36 �����������������������������������������!�������!'*�����!�������!'*�����!�������!'*�����!�������!'*����

37 ���&���&���&���&����

38 ���������������������������������������������!�����'������!�����'������!�����'������!�����'�.�������������.�������������.�������������.��������������$�9��$�9��$�9��$�9�::::�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�.�9�$�B9/�1�./�/�/�/�����

39 �����������������������������������������������������������!'*/��������!'*/��������!'*/��������!'*/�.-��8������..-��8������..-��8������..-��8������./�!��?��8�����'*�*4/�!��?��8�����'*�*4/�!��?��8�����'*�*4/�!��?��8�����'*�*4����

40 ���<����<����<����<���������������������!��������������������!��������������������!��������������������!

41 <�<�<�<�����������������������������������������������������������������������������������������������������

Declare getPaymentAmount method instead of earnings method

68

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.8

The “is-a” relationship that exists between superclasses and subclasses, 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 superclass or interface type, the method processes the object received as an argument polymorphically.

69

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.9

Using a superclass reference, we can polymorphically invoke any method specified in the superclass declaration (and in class Q�����Q�����Q�����Q�����). Using an interface reference, we can polymorphically invoke any method specified in the interface declaration (and in class Q�����Q�����Q�����Q�����).

70

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������

����������������������������

(1 of 2)

1 ��� �!��"#�"7$����������������������� �!��"#�"7$����������������������� �!��"#�"7$����������������������� �!��"#�"7$��������������������

2 ��������������������������������������������������������������������������������������������

3 4 ������������������������������������������������������������������������������������������������������������������������

5 &&&&����

6 ������������������������������������������������������������������������������������'�����!�!�()�*����'�����!�!�()�*����'�����!�!�()�*����'�����!�!�()�*����

7 ���&���&���&���&����

8 ��������������������������������������������������������������������2222������������������������������������������������������������

9 �����������������Q�����������������Q�����������������Q�����������������Q������()�,�������()�,�������()�,�������()�,���-��-��-��-������(�������(�������(�������(�5555�)4�)4�)4�)4����

10 11 �������������������������������������-��������������������������������������������-��������������������������������������������-��������������������������������������������-�������������������������������

12 �����������Q������(������������Q������(������������Q������(������������Q������(�####�)�,��)�,��)�,��)�,���-��-��-��-��������'���������'���������'���������'�.#"1+5..#"1+5..#"1+5..#"1+5./�/�/�/�.���..���..���..���./�/�/�/�1111/�/�/�/�+H7�##+H7�##+H7�##+H7�##�*4�*4�*4�*4����

13 �����������Q������(������������Q������(������������Q������(������������Q������(�""""�)�,��)�,��)�,��)�,���-��-��-��-��������'���������'���������'���������'�.73HEK..73HEK..73HEK..73HEK./�/�/�/�.���..���..���..���./�/�/�/�5555/�/�/�/�HK�K7HK�K7HK�K7HK�K7�*4�*4�*4�*4����

14 �����������Q������(������������Q������(������������Q������(������������Q������(�1111�)�,���)�,���)�,���)�,������

15 ��������������������������������������-��-��-��-��������������'���������������'���������������'���������������'�.0���..0���..0���..0���./�/�/�/�.�����..�����..�����..�����./�/�/�/�."""."""."""."""2222""""""""2222"""".""""."""".""""./�/�/�/�E##�##E##�##E##�##E##�##�*4�*4�*4�*4����

16 �����������Q������(������������Q������(������������Q������(������������Q������(�++++�)�,��)�,��)�,��)�,�����

17 ��������������������������������������-��-��-��-��������������'���������������'���������������'���������������'�.6��..6��..6��..6��./�/�/�/�.����..����..����..����./�/�/�/�.EEE.EEE.EEE.EEE2222EEEEEEEE2222EEEE.EEEE.EEEE.EEEE./�/�/�/�"1##�##"1##�##"1##�##"1##�##�*4�*4�*4�*4����

18 19 ��������������������������������������������������������������������������'�������'�������'�������'�����

20 ������������������������������������.��������������������������������������������$.��������������������������������������������$.��������������������������������������������$.��������������������������������������������$::::�.�.�.�.�*4��*4��*4��*4�����

21

Declare array of Payable variables

Assigning references to Invoice objects to Payable variables

Assigning references to SalariedEmployeeobjects to Payable variables

71

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

������������������������������������������������

����������������������������

(2 of 2)

22 ���������������������������!��������������������������������������Q���������!��������������������������������������Q���������!��������������������������������������Q���������!��������������������������������������Q������

23 ���������������������������������'������������������$������Q�������*�'������������������$������Q�������*�'������������������$������Q�������*�'������������������$������Q�������*����

24 ������&������&������&������&����

25 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

26 �������������������������'��������������������������'��������������������������'��������������������������'�.9��.9��.9��.9��::::�9�9�9�9�$�B9/�1��$�B9/�1��$�B9/�1��$�B9/�1�::::����::::�.�.�.�./�/�/�/�����

27 �����������������������������!'*/�����������������������������!'*/�����������������������������!'*/�����������������������������!'*/����

28 ������������������������������������������������.����������..����������..����������..����������./������������!��������%�����'*�*4�/������������!��������%�����'*�*4�/������������!��������%�����'*�*4�/������������!��������%�����'*�*4�����

29 ������<�������<�������<�������<�������������������������������������

30 ���<����<����<����<�����������������������������������������

31 <�<�<�<�����������������������������������������������������������������������������������������������������������������

��������������������������������������������������������������������������������������������������������������������������������������������������$����������$����������$����������$���� �������$�������$�������$�������$����

��������$�#"1+5�'���*��������$�#"1+5�'���*��������$�#"1+5�'���*��������$�#"1+5�'���*����O������$�1O������$�1O������$�1O������$�1����������������$�B+H7�##������������$�B+H7�##������������$�B+H7�##������������$�B+H7�##��������������$�BH7#�##����������$�BH7#�##����������$�BH7#�##����������$�BH7#�##����

�������$�������$�������$�������$����

��������$�73HEK�'���*��������$�73HEK�'���*��������$�73HEK�'���*��������$�73HEK�'���*����O������$�5O������$�5O������$�5O������$�5����������������$�BHK�K7������������$�BHK�K7������������$�BHK�K7������������$�BHK�K7��������������$�B+"K�E#����������$�B+"K�E#����������$�B+"K�E#����������$�B+"K�E#����

��������������$�0�����������������������$�0�����������������������$�0�����������������������$�0��������������������������������$�""�������������������$�""�������������������$�""�������������������$�""""""2222""""""""2222""""""""""""""""����-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##-��8������$�BE##�##��������������$�BE##�##����������$�BE##�##����������$�BE##�##����������$�BE##�##����

��������������$�6���������������������$�6���������������������$�6���������������������$�6������������������������������$�EEE�������������������$�EEE�������������������$�EEE�������������������$�EEE2222EEEEEEEE2222EEEEEEEEEEEEEEEE����-��8������$�B"/1##�##-��8������$�B"/1##�##-��8������$�B"/1##�##-��8������$�B"/1##�##��������������$�B"/1##�##����������$�B"/1##�##����������$�B"/1##�##����������$�B"/1##�##����

Call toString and getPaymentAmountmethods polymorphically

72

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.10

All methods of class Q�����Q�����Q�����Q����� can be called by using a reference of an interface type. A reference refers to an object, and all objects inherit the methods of class Q�����Q�����Q�����Q�����.

Page 13: One Ring to rule them all, One Ring to find them, One Ring

13

73

1992-2007 Pearson Education, Inc. All rights reserved.

10.7.7 Declaring Constants with Interfaces

• Interfaces can be used to declare constants used in many class declarations

– These constants are implicitly public, static and final

– Using a static import declaration allows clients to use these constants with just their names

74

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.11

It is considered a better programming practice to create sets of constants as enumerations with keyword ����������������. See Section 6.10 for an introduction to ���������������� and Section 8.9 for additional ���������������� details.

75

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.16 | Common interfaces of the Java API.'���"����1*'���"����1*'���"����1*'���"����1*

Interface Description

�������� As you learned in Chapter 2, Java contains several comparison operators (e.g., @, @,, C, C,, ,,, R,) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface ������� is used to allow objects of a class that implements the interface to be compared to one another. The interface contains one method, �������, that compares the object that calls the method to the object passed as an argument to the method. Classes must implement ������� such that it returns a value indicating whether the object on which it is invoked is less than (negative integer return value), equal to (# return value) or greater than (positive integer return value) the object passed as an argument, using any criteria specified by the programmer. For example, if class �������� implements �������, its ������� method could compare �������� objects by their earnings amounts. Interface ������� is commonly used for ordering objects in a collection such as an array. We use ������� in Chapter 18, Generics, and Chapter 19, Collections.

�����M����A tagging interface used only to identify classes whose objects can be written to (i.e., serialized) or read from (i.e., deserialized) some type of storage (e.g., file on disk, database field) or transmitted across a network. We use �����M��� in Chapter 14, Files and Streams, and Chapter 24, Networking.

76

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.16 | Common interfaces of the Java API.'���1����1*'���1����1*'���1����1*'���1����1*

Interface Description

I������� Implemented by any class for which objects of that class should be able to execute in parallel using a technique called multithreading (discussed in Chapter 23, Multithreading). The interface contains one method, ��, which describes the behavior of an object when executed.

GUI event-listener interfaces

You work with Graphical User Interfaces (GUIs) every day. For example, in your Web browser, you might type in a text field the address of a Web site to visit, or you might click a button to return to the previous site you visited. When you type a Web site address or click a button in the Web browser, the browser must respond to your interaction and perform the desired task for you. Your interaction is known as an event, and the code that the browser uses to respond to an event is known as an event handler. In Chapter 11, GUI Components: Part 1, and Chapter 22, GUI Components: Part 2, you will learn how to build Java GUIs and how to build event handlers to respond to user interactions. The event handlers are declared in classes that implement an appropriate event-listener interface. Each event listener interface specifies one or more methods that must be implemented to respond to user interactions.

�-��!���������Contains a set of constants used in GUI programming to position GUI elements on the screen. We explore GUI programming in Chapters 11 and 22.

77

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.17 | S�����S�����S�����S����� hierarchy.

78

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.18 | S�����S�����S�����S����� hierarchy with S������������S������������S������������S������������.

Page 14: One Ring to rule them all, One Ring to find them, One Ring

14

79

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.19 | Attributes and operations of classes �������O����������O����������O����������O���, ?����-�?����-�?����-�?����-� and N������N������N������N������.

80

1992-2007 Pearson Education, Inc. All rights reserved.

10.9 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System

• UML model for inheritance– The generalization relationship

• The superclass is a generalization of the subclasses• The subclasses are specializations of the superclass

•Transaction superclass– Contains the methods and fields BalanceInquiry, Withdrawal and Deposit have in common• execute method• accountNumber field

81

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10. 20 | Class diagram modeling generalization of superclass�������������������������������� and subclasses �������O����������O����������O����������O���, ?����-�?����-�?����-�?����-� andN������N������N������N������. Note that abstract class names (e.g., ��������������������������������) and

method names (e.g., �>������>������>������>����� in class ��������������������������������) appear in italics.

82

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.21 | Class diagram of the ATM system (incorporating inheritance). Note that abstract class names (e.g., ��������������������������������)

appear in italics.

83

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.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, methods and associations is substantial (as in Fig. 10.21 and Fig. 10.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 methods.

84

1992-2007 Pearson Education, Inc. All rights reserved.

10.9 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System (Cont.)

• Incorporating inheritance into the ATM system design

– If class A is a generalization of class B, then class B extends class A

– If class A is an abstract class and class B is a subclass of class A, then class B must implement the abstract methods of class A if class B is to be a concrete class

Page 15: One Ring to rule them all, One Ring to find them, One Ring

15

85

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 10.22 | Class diagram with attributes and operations (incorporating inheritance). Note that abstract class names (e.g., ��������������������������������) and method

names (e.g., �>������>������>������>����� in class ��������������������������������) appear in italic

86

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

?����-����?����-����?����-����?����-����

1 ��������?����-�������������%�S�-����-�������������������?����-�������������%�S�-����-�������������������?����-�������������%�S�-����-�������������������?����-�������������%�S�-����-�����������

2 ���������������������������������������������?����-���?����-���?����-���?����-���>������>������>������>���������������������������������������������

3 &&&&����

4 <<<<�������������?����-��������������?����-��������������?����-��������������?����-�

Subclass Withdrawal extends

superclass Transaction

87

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

?����-����?����-����?����-����?����-����

1 ���?����-�������?����-�������?����-�������?����-����

2 ���J�����������!������������!������ �!��"#�1"���� �!��"#�11���J�����������!������������!������ �!��"#�1"���� �!��"#�11���J�����������!������������!������ �!��"#�1"���� �!��"#�11���J�����������!������������!������ �!��"#�1"���� �!��"#�11

3 ���������������������������������������������?����-���?����-���?����-���?����-���>������>������>������>���������������������������������������������

4 &&&&����

5 ��������������������������������������������������������

6 ������������������������������������������������������������������4�������4�������4�������4�������������-����-������������-����-������������-����-������������-����-

7 ������������������������������������L�����8����4�L�����8����4�L�����8����4�L�����8����4������������������������������8�������������8�������������8�������������8����

8 ������������������������������������N�����������N�������4�����N�����������N�������4�����N�����������N�������4�����N�����������N�������4���������������������������������������������������������������������������������������������������������

9

10 ��������������������������������2222!���������������!���������������!���������������!���������������

11 �������������������������������������?����-�'*�?����-�'*�?����-�'*�?����-�'*����

12 ���&���&���&���&����

13 ���<����<����<����<�������������������������������������2222!������?����-�����������!������?����-�����������!������?����-�����������!������?����-�����������

14

15 ��������������������������!������������������!������������������!������������������!��>�������>�������>�������>�����

16 ����������������������������������������������������������>�����'*��>�����'*��>�����'*��>�����'*����

17 ���&���&���&���&����

18 ���<����<����<����<����������������>��������������������>��������������������>��������������������>�����

19 <�<�<�<�������������?����-�������������?����-�������������?����-�������������?����-�

Subclass Withdrawal extends superclass Transaction

88

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 10.13

Several UML modeling tools convert UML-based designs into Java code and can speed the implementation process considerably. For more information on these tools, refer to the Internet and Web Resources listed at the end of Section 2.9.

89

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(1 of 2)

1 ���%�������������������������������%�S������������%�������������������������������%�S������������%�������������������������������%�S������������%�������������������������������%�S���������

2 ������������������������������������������������������������������������������������������������������������

3 &&&&����

4 ��������������������������������������������������������

5 �������������������������������������������������������=����4��������=����4��������=����4��������=����4�������������������������������������������������������������������������������������������������������������

6 �����������������������������������������������4������������4������������4������������4����%�ST����������%�ST����������%�ST����������%�ST�������

7 ��������������������������������������������8N����8N����8N����8N����������8N����4���8N����4���8N����4���8N����4���������������������������������������������������������������������������������

8 9 ��������������������������������2222!��������������������8��������������������!�����'*!��������������������8��������������������!�����'*!��������������������8��������������������!�����'*!��������������������8��������������������!�����'*

10 ���������������������������������������������'*���������'*���������'*���������'*����

11 ���&���&���&���&����

12 ���<����<����<����<�������������������������������������2222!������������������������!������������������������!������������������������!������������������������

13 14 ��������������������������������������������������������������������������������������������

15 ��������������������������������������������������������!��%������=����'*!��%������=����'*!��%������=����'*!��%������=����'*����

16 ���&���&���&���&����

17 ���<����<����<����<���������������!��%������=������������������!��%������=������������������!��%������=������������������!��%������=���� 18

Declare abstract superclass Transaction

90

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������

(2 of 2)

19 ������������������������������������������������������������������������������������������������������������

20 ����������������������������������������������!�������'*������!�������'*������!�������'*������!�������'*����

21 ���&���&���&���&����

22 ���<����<����<����<���������������!���������������������!���������������������!���������������������!�������

23

24 ������������������������8������������������������������8������������������������������8������������������������������8������

25 ������������������������������������������8N�����!����8N����'*��8N�����!����8N����'*��8N�����!����8N����'*��8N�����!����8N����'*����

26 ���&���&���&���&����

27 ���<����<����<����<���������������!����8N���������������!����8N���������������!����8N���������������!����8N�������������

28

29 ����������������������������������������������������������������������������������������������������������������������������������������������������������������

30 ����������������������������������������������������������������������������������>�����'*4��>�����'*4��>�����'*4��>�����'*4����

31 <�<�<�<���������������������������������������������������������������������������������

Declare abstract method execute