object-oriented programming: inheritance save base ...kraemere/cse491/notes/javahtp7e_09_6pp.pdf ·...

15
1 1 1992-2007 Pearson Education, Inc. All rights reserved. 9 9 Object-Oriented Programming: Inheritance 2 1992-2007 Pearson Education Inc. All rights reserved. Say not you know another entirely, till you have divided an inheritance with him. Johann Kasper Lavater This method is to define as the number of a class the class of all classes similar to the given class. Bertrand Russell Good as it is to inherit a library, it is better to collect one. Augustine Birrell Save base authority from others' books. William Shakespeare 3 1992-2007 Pearson Education, Inc. All rights reserved. OBJECTIVES In this chapter you will learn: How inheritance promotes software reusability. The notions of superclasses and subclasses. To use keyword to create a class that inherits attributes and behaviors from another class. To use access modifier to give subclass methods access to superclass members. To access superclass members with . How constructors are used in inheritance hierarchies. The methods of class , the direct or indirect superclass of all classes in Java. 4 1992-2007 Pearson Education, Inc. All rights reserved. 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 Members 9.4 Relationship between Superclasses and Subclasses 9.4.1 Creating and Using a Class 9.4.2 Creating a Class without Using Inheritance 9.4.3 Creating a Inheritance Hierarchy 9.4.4 Inheritance Hierarchy Using Instance Variables 9.4.5 B Inheritance Hierarchy Using Instance Variables 5 1992-2007 Pearson Education, Inc. All rights reserved. 9.5 Constructors in Subclasses 9.6 Software Engineering with Inheritance 9.7 Class 9.8 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels 9.9 Wrap-Up 6 1992-2007 Pearson Education, Inc. All rights reserved. 9.1 Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Subclass extends superclass Subclass More specialized group of objects Behaviors inherited from superclass Can customize Additional behaviors

Upload: others

Post on 30-Apr-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

1

1

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

99Object-Oriented

Programming: Inheritance

2

1992-2007 Pearson Education Inc. All rights reserved.

Say not you know another entirely, till you have divided an inheritance with him.

— Johann Kasper Lavater

This method is to define as the number of a class the class of all classes similar to the given class.

— Bertrand Russell

Good as it is to inherit a library, it is better to collect one.— Augustine Birrell

Save base authority from others' books.— William Shakespeare

3

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

OBJECTIVESIn this chapter you will learn: � How inheritance promotes software reusability.� The notions of superclasses and subclasses.� To use keyword ������� to create a class that

inherits attributes and behaviors from another class.� To use access modifier ������� to give subclass

methods access to superclass members.� To access superclass members with �����.� How constructors are used in inheritance

hierarchies.� The methods of class � ���, the direct or indirect

superclass of all classes in Java.

4

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

9.1 Introduction

9.2 Superclasses and Subclasses 9.3 ���������������������������� Members

9.4 Relationship between Superclasses and Subclasses 9.4.1 Creating and Using a ������������������������������������������������������������ Class 9.4.2 Creating a �������������������������������������������������������������������������������������������� Class

without Using Inheritance 9.4.3 Creating a ������������������������������������������������������������–

�������������������������������������������������������������������������������������������� Inheritance Hierarchy 9.4.4 ������������������������������������������������������������–

�������������������������������������������������������������������������������������������� Inheritance Hierarchy Using ���������������������������� Instance Variables

9.4.5 ������������������������������������������������������������–B���������������������������������������������������������������������������������������� Inheritance Hierarchy Using ���������������������������� Instance Variables

5

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

9.5 Constructors in Subclasses

9.6 Software Engineering with Inheritance 9.7 � ���� ���� ���� ��� Class 9.8 (Optional) GUI and Graphics Case Study: Displaying Text

and Images Using Labels9.9 Wrap-Up

6

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

9.1 Introduction

• Inheritance– Software reusability– Create new class from existing class

• Absorb existing class’s data and behaviors• Enhance with new capabilities

– Subclass extends superclass• Subclass

– More specialized group of objects– Behaviors inherited from superclass

• Can customize– Additional behaviors

Page 2: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

2

7

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

9.1 Introduction (Cont.)

• Class hierarchy– Direct superclass

• Inherited explicitly (one level up hierarchy)– Indirect superclass

• Inherited two or more levels up hierarchy– Single inheritance

• Inherits from one superclass– Multiple inheritance

• Inherits from multiple superclasses– Java does not support multiple inheritance

8

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

9.2 Superclasses and subclasses

• Superclasses and subclasses– Object of one class “is an” object of another class

• Example: Rectangle is quadrilateral.– Class �������������������������������� inherits from class ����������������������������������������������������

– ����������������������������������������������������: superclass– ��������������������������������: subclass

– Superclass typically represents larger set of objects than subclasses

• Example: – superclass: ������������������������

• Cars, trucks, boats, bicycles, …– subclass: ������������

• Smaller, more-specific subset of vehicles

9

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

Fig. 9.1 | Inheritance examples.

Superclass Subclasses ���������������������������� ������������������������������������������������������������, ������������������� ������������������� ������������������� ������������������� �������������������� ��������������������, !�������!�������!�������!�������, ��������������������������������

"��"��"��"�� ���"�����"�����"�����"��, #��$���������"��#��$���������"��#��$���������"��#��$���������"��, %������"��%������"��%������"��%������"��

���������������������������� &�����&�����&�����&�����, ���''���''���''���''

���()������()������()������()��� ���(���)������(���)������(���)������(���)���, �������)����������)����������)����������)���

10

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

9.2 Superclasses and subclasses (Cont.)

• Inheritance hierarchy– Inheritance relationships: tree-like hierarchy structure– Each class becomes

• superclass– Supply members to other classes

OR• subclass

– Inherit members from other classes

11

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

Fig. 9.2 | Inheritance hierarchy for university ��������%�� ����������%�� ����������%�� ����������%�� ������

12

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

Fig. 9.3 | Inheritance hierarchy for ������������������������.

Page 3: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

3

13

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

9.3 ���������������������������� Members

*���������������������������� access– Intermediate level of protection between �� ���� ���� ���� �� and ����������������������������

– ���������������������������� members accessible by• superclass members• subclass members• Class members in the same package

– Subclass access to superclass member• Keyword �������������������� and a dot (.)

14

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

Software Engineering Observation 9.1

Methods of a subclass cannot directly access ���������������������������� members of their superclass. A subclass can change the state of ���������������������������� superclassinstance variables only through non-����������������������������

methods provided in the superclass and inherited by the subclass.

15

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

Software Engineering Observation 9.2

Declaring ���������������������������� instance variables helps programmers test, debug and correctly modify systems. If a subclass could access its superclass’s���������������������������� instance variables, classes that inherit from that subclass could access the instance variables as well. This would propagate access to what should be ���������������������������� instance variables, and the benefits of information hiding would be lost.

16

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

9.4 Relationship between Superclassesand Subclasses

• Superclass and subclass relationship– Example: ���������������+��������������������������������������+��������������������������������������+��������������������������������������+�����������������������

inheritance hierarchy* ������������������������������������������������������������

– First name, last name, SSN, commission rate, gross sale amount

* ��������������������������������������������������������������������������������������������

– First name, last name, SSN, commission rate, gross sale amount

– Base salary

17

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

9.4.1 Creating and Using a ������������������������������������������������������������ Class

• Class ������������������������������������������������������������

– Extends class � ���� ���� ���� ���

• Keyword ����������������������������

• Every class in Java extends an existing class– Except � ���� ���� ���� ���

• Every class inherits � ���� ���� ���� ���’s methods• New class implicitly extends Object

– If it does not extend another class

18

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

Software Engineering Observation 9.3

The Java compiler sets the superclass of a class to � ���� ���� ���� ��� when the class declaration does not explicitly extend a superclass.

Page 4: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

4

19

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

Outline

������������������������������������������������������������,����,����,����,����

(1 of 4)

Line 4

Lines 6-10

Line 16

Lines 17-21

Lines 20-21

----....---- ++-&��,-/,01-���������������,����++-&��,-/,01-���������������,����++-&��,-/,01-���������������,����++-&��,-/,01-���������������,����----

----2222---- ++-���������������-����-����������-�-�������-�������,++-���������������-����-����������-�-�������-�������,++-���������������-����-����������-�-�������-�������,++-���������������-����-����������-�-�������-�������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-���������������--���������������--���������������--���������������-����������������������������-� ���-� ���-� ���-� ���----

----4444---- 5555----

----6666---- ------------����������������������������-������-'����7���8-������-'����7���8-������-'����7���8-������-'����7���8----------------------------------------------------------------------------------------------------------------------------

----9999---- ------------����������������������������-������-���-������-���-������-���-������-����7���8�7���8�7���8�7���8--------------------------------------------------------------------------------------------------------------------------------

----::::---- ------------����������������������������-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8--------------------------------------------------------------------------------

----////---- ------------����������������������������----�� ���� ���� ���� ��-���������8--���������8--���������8--���������8-++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����--------------------------------

.<.<.<.<----------------����������������������������----�� ���� ���� ���� ��-�����������8--�����������8--�����������8--�����������8-++-�������-���������++-�������-���������++-�������-���������++-�������-���������----

........--------

.2.2.2.2----------------++-'���++-'���++-'���++-'���====��������������������������-���������-���������-���������-�������----------------------------------------------------------------------------------------------------------------------------------------------------------------

.3.3.3.3----------------�� ���� ���� ���� ��-���������������>-������-'����?-������-����?-������-���?-���������������>-������-'����?-������-����?-������-���?-���������������>-������-'����?-������-����?-������-���?-���������������>-������-'����?-������-����?-������-���?------------

.0.0.0.0----------------------------�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@--------------------------------------------------------------------------------------------------------------------------------------------------------

.4.4.4.4----------------5555----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

.6.6.6.6----------------------------++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����------------------------------------------------------------

.9.9.9.9----------------------------'����7���-A-'����8'����7���-A-'����8'����7���-A-'����8'����7���-A-'����8--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

.:.:.:.:----------------------------����7���-A-����8����7���-A-����8����7���-A-����8����7���-A-����8----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

././././----------------------------�����������7�� ��-A-���8�����������7�� ��-A-���8�����������7�� ��-A-���8�����������7�� ��-A-���8--------------------------------------------------------------------------------------------------------------------------------------------------------

2<2<2<2<----------------------------������������>-�����-@8-������������>-�����-@8-������������>-�����-@8-������������>-�����-@8-++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����--------------------------------

2.2.2.2.----------------------------���������������>-����-@8-���������������>-����-@8-���������������>-����-@8-���������������>-����-@8-++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����----

22222222----------------B-B-B-B-++-���-'���++-���-'���++-���-'���++-���-'���====��������������������������������-���������������-�������-���������������-�������-���������������-�������-���������������-�������------------------------------------------------------------

23232323--------

20202020----------------++-���-'����-����++-���-'����-����++-���-'����-����++-���-'����-����----

24242424----------------�� ���� ���� ���� ��----������������-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@----

26262626-------5---5---5---5----

29292929----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

2:2:2:2:-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

2/2/2/2/--------

Class ���������������������� class � ���

Implicit call to � ��� constructor

Initialize instance variables

Declare �������instance variables

Invoke methods ������������ and ��������������� to validate data

20

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

Outline

������������������������������������������������������������,����,����,����,����

(2 of 4)

3<3<3<3<----------------++-������-'����-����++-������-'����-����++-������-'����-����++-������-'����-����----

3.3.3.3.----------------�� ���� ���� ���� ��-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@----

32323232-------5---5---5---5----

33333333----------------------------������������������������-'����7���8-'����7���8-'����7���8-'����7���8----

30303030-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

34343434--------

36363636----------------++-���-����-����++-���-����-����++-���-����-����++-���-����-����----

39393939----------------�� ���� ���� ���� ��----������������-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@----

3:3:3:3:-------5---5---5---5----

3/3/3/3/----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

0<0<0<0<-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

0.0.0.0.--------

02020202----------------++-������-����-����++-������-����-����++-������-����-����++-������-����-����----

03030303----------------�� ���� ���� ���� ��-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@----

00000000-------5---5---5---5----

04040404----------------------------������������������������-����7���8-����7���8-����7���8-����7���8----

06060606-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

09090909--------

0:0:0:0:----------------++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��----

0/0/0/0/----------------�� ���� ���� ���� ��----������������-���������-���������-���������-��������������7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@----

4<4<4<4<-------5---5---5---5----

4.4.4.4.----------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-++-�����-��������++-�����-��������++-�����-��������++-�����-��������----

42424242-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

43434343--------

40404040----------------++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��----

44444444----------------�� ���� ���� ���� ��-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@----

46464646-------5---5---5---5----

49494949----------------------------������������������������-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8----

4:4:4:4:-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

4/4/4/4/--------

21

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

Outline

������������������������������������������������������������,����,����,����,����

(3 of 4)

Lines 85-88

6<6<6<6<----------------++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����----

6.6.6.6.----------------�� ���� ���� ���� ��----������������-������������>--������������>--������������>--������������>-�� ���� ���� ���� ��-�����-@-�����-@-�����-@-�����-@----

62626262-------5---5---5---5----

63636363----------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-�����8-1-�����8-1-�����8-1-�����8----

60606060-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

64646464--------

66666666----------------++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����----

69696969----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�����-�����-�����-������������>@�������>@�������>@�������>@----

6:6:6:6:-------5---5---5---5----

6/6/6/6/----------------------------������������������������-���������8-���������8-���������8-���������8----

9<9<9<9<-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

9.9.9.9.--------

92929292----------------++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

93939393----------------�� ���� ���� ���� ��----������������-���������������>--���������������>--���������������>--���������������>-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

90909090-------5---5---5---5----

94949494----------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-<,<<,<<,<<,<-FF-����-C--FF-����-C--FF-����-C--FF-����-C-.,<.,<.,<.,<-@-D-����-1--@-D-����-1--@-D-����-1--@-D-����-1-<<<<,<,<,<,<8888----

96969696-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

99999999--------

9:9:9:9:----------------++-������-�������-����++-������-�������-����++-������-�������-����++-������-�������-����----

9/9/9/9/----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-���������������>@-���������������>@-���������������>@-���������������>@----

:<:<:<:<-------5---5---5---5----

:.:.:.:.----------------------------������������������������-�����������8-�����������8-�����������8-�����������8----

:2:2:2:2-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:3:3:3:3--------

:0:0:0:0----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������------------------------------------------------------------------------

:4:4:4:4----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@------------------------------------------------------------

:6:6:6:6----------------5555--------------------------------------------------------------------------------------------------------------------------------------------------------

:9:9:9:9----------------------------������������������������-�����������-G-���������8-�����������-G-���������8-�����������-G-���������8-�����������-G-���������8----

::::::::----------------B-B-B-B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������------------------------------------------------------------

:/:/:/:/--------

Calculate earnings

22

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

Outline

������������������������������������������������������������,����,����,����,����

(4 of 4)

Lines 91-98

/</</</<----------------++-������-������-�������������-'-���������������- ���++-������-������-�������������-'-���������������- ���++-������-������-�������������-'-���������������- ���++-������-������-�������������-'-���������������- ���------------------------

/./././.----------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/2/2/2/2----------------5555--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/3/3/3/3----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HIHIHIHI�1-I�-I��1-I�-I��1-I�-I��1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H????----

/0/0/0/0----------------------------------------H�������-�������HH�������-�������HH�������-�������HH�������-�������H?-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���?--------------------------------------------------------------------

/4/4/4/4----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-�����������7�� ��??-�����������7�� ��??-�����������7�� ��??-�����������7�� ��?----------------------------------------------------

/6/6/6/6----------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-���������??-���������??-���������??-���������?----------------------------------------------------------------------------------------------------------------------------------------

/9/9/9/9----------------------------------------H�������-����HH�������-����HH�������-����HH�������-����H?-�����������-@8?-�����������-@8?-�����������-@8?-�����������-@8------------------------------------------------------------------------------------------------

/:/:/:/:----------------B-B-B-B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������------------------------------------------------------------------------------------------------------------------------------------------------------------------------

////////----B-B-B-B-++-���-����-���������������++-���-����-���������������++-���-����-���������������++-���-����-���������������----

Override method �������of class � ���

23

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

Common Programming Error 9.1

It is a syntax error to override a method with a more restricted access modifier—a �� ��-�� ��-�� ��-�� ��-method of the superclass cannot become a���������������������������� or ���������������������������� method in the subclass; a ���������������������������� method of the superclass cannot become a ���������������������������� method in the subclass. Doing so would break the “is-a” relationship in which it is required that all subclass objects be able to respond to method calls that are made to �� ���� ���� ���� ��methods declared in the superclass.(cont…)

24

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

Common Programming Error 9.1

If a �� ���� ���� ���� �� method could be overridden as a���������������������������� or ���������������������������� method, the subclass objects would not be able to respond to the same method calls as superclass objects. Once a method is declared �� ���� ���� ���� �� in a superclass, the method remains �� ���� ���� ���� �� for all that class’s direct and indirect subclasses.

Page 5: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

5

25

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

Outline

������������������������������������������������������������!���,����!���,����!���,����!���,����

(1 of 2)

Lines 9-10

Lines 15-25

Line 26-27

----....---- ++-&��,-/,41-���������������!���,����++-&��,-/,41-���������������!���,����++-&��,-/,41-���������������!���,����++-&��,-/,41-���������������!���,����----

----2222---- ++-!������-����-���������������,++-!������-����-���������������,++-!������-����-���������������,++-!������-����-���������������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-���������������!���--���������������!���--���������������!���--���������������!���-----

----4444---- 5555----

----6666---- ------------�� ���� ���� ���� ��----��������������������----������������-����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@-----

----9999---- ---5---5---5---5----

----::::---- ------------------------++-�����������-���������������- ���++-�����������-���������������- ���++-�����������-���������������- ���++-�����������-���������������- ���----

----////---- ------------------------���������������-�������-A-���������������-�������-A-���������������-�������-A-���������������-�������-A-��;��;��;��;-���������������>-���������������>-���������������>-���������������>----

.<.<.<.<----------------------------------------H���HH���HH���HH���H?-?-?-?-HM���HHM���HHM���HHM���H?-?-?-?-H222H222H222H222====22222222====2222H2222H2222H2222H?-?-?-?-.<<<<.<<<<.<<<<.<<<<?-?-?-?-,<6,<6,<6,<6-@8-@8-@8-@8----------------------------

........--------------------------------

.2.2.2.2----------------------------++-���-�������-�������-����++-���-�������-�������-����++-���-�������-�������-����++-���-�������-�������-����----

.3.3.3.3----------������,��,�������>------������,��,�������>------������,��,�������>------������,��,�������>----

.0.0.0.0----------------------------------------H�������-��'������- ������- �-���H�������-��'������- ������- �-���H�������-��'������- ������- �-���H�������-��'������- ������- �-���-������1--������1--������1--������1-JJJJ�H�H�H�H-@8-@8-@8-@8----

.4.4.4.4----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H&����-����-��HH&����-����-��HH&����-����-��HH&����-����-��H????----

.6.6.6.6----------------------------------------�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@-@8-@8-@8-@8----

.9.9.9.9----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H"���-����-��HH"���-����-��HH"���-����-��HH"���-����-��H?-?-?-?-----

.:.:.:.:----------------------------------------�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@-@8-@8-@8-@8----

././././----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-HHHH����-�������-��� ��-��H����-�������-��� ��-��H����-�������-��� ��-��H����-�������-��� ��-��H?-?-?-?-----

2<2<2<2<----------------------------------------�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@-@8-@8-@8-@8----

2.2.2.2.----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-�����-��HH����-�����-��HH����-�����-��HH����-�����-��H?-?-?-?-----

22222222----------------------------------------�������,������������>@�������,������������>@�������,������������>@�������,������������>@-@8-@8-@8-@8----

23232323----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H��������-����-��HH��������-����-��HH��������-����-��HH��������-����-��H????----

20202020----------------------------------------�������,���������������>@�������,���������������>@�������,���������������>@�������,���������������>@-@8-@8-@8-@8----

24242424--------

26262626----------------------------�������,������������>-�������,������������>-�������,������������>-�������,������������>-4<<4<<4<<4<<-@8--@8--@8--@8-++-���-����-�����++-���-����-�����++-���-����-�����++-���-����-�����--------------------------------

29292929----------------------------�������,���������������>-�������,���������������>-�������,���������������>-�������,���������������>-,.,.,.,.-@8--@8--@8--@8-++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

2:2:2:2:--------------------------------

Instantiate ��������������� object

Use ���������������’s get methods to retrieve the object’s instance variable values

Use ���������������’s set methods to change the object’s instance variable values

26

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

Outline

������������������������������������������������������������!���,����!���,����!���,����!���,����

(2 of 2)

Line 30

Program output

2/2/2/2/----------------------------������,��,�����'>-������,��,�����'>-������,��,�����'>-������,��,�����'>-HHHHJJJJ�I�1�I�1�I�1�I�1JJJJ����JJJJ�I��I��I��I�JJJJ�H�H�H�H????------------------------------------------------------------------------------------------------------------------------------------

3<3<3<3<----------------------------------------H ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������H?-�������-@8?-�������-@8?-�������-@8?-�������-@8----

3.3.3.3.-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

32323232----B-B-B-B-++-���-����-���������������!���++-���-����-���������������!���++-���-����-���������������!���++-���-����-���������������!���----

----�������-��'������- ������- ��������-��'������- ������- ��������-��'������- ������- ��������-��'������- ������- �-���-������1--���-������1--���-������1--���-������1---------&����-����-��-���&����-����-��-���&����-����-��-���&����-����-��-���----"���-����-��-M���"���-����-��-M���"���-����-��-M���"���-����-��-M���----����-�������-��� ��-��-222����-�������-��� ��-��-222����-�������-��� ��-��-222����-�������-��� ��-��-222====22222222====2222222222222222----����-�����-��-.<<<<,<<����-�����-��-.<<<<,<<����-�����-��-.<<<<,<<����-�����-��-.<<<<,<<----��������-����-��-<,<6��������-����-��-<,<6��������-����-��-<,<6��������-����-��-<,<6-------- ������-�������-��'������- ������- �-�������1 ������-�������-��'������- ������- �-�������1 ������-�������-��'������- ������- �-�������1 ������-�������-��'������- ������- �-�������1--------�������-�������1-���-M����������-�������1-���-M����������-�������1-���-M����������-�������1-���-M���----����-�������-��� ��1-222����-�������-��� ��1-222����-�������-��� ��1-222����-�������-��� ��1-222====22222222====2222222222222222----����-�����1-4<<,<<����-�����1-4<<,<<����-�����1-4<<,<<����-�����1-4<<,<<----�������-����1-<,.<�������-����1-<,.<�������-����1-<,.<�������-����1-<,.<--------

Implicitly call object’s ������� method

27

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

9.4.2 Creating a ��������������������������������������������������������������������������������������������Class without Using Inheritance

• Class ��������������������������������������������������������������������������������������������

– Implicitly extends � ���� ���� ���� ���

– Much of the code is similar to ������������������������������������������������������������

* ���������������������������� instance variables* �� ���� ���� ���� �� methods• constructor

– Additions* ���������������������������� instance variable ��������� ��������� ��������� ���������

• Methods ���������������������������������������������������� and ����������������������������������������������������

28

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

Outline

�����������������������������������������������������������������������,�����������,�����������,�����������,����

(1 of 4)

Line 12

Line 24

----....---- ++-&��,-/,61-�����������������������,����++-&��,-/,61-�����������������������,����++-&��,-/,61-�����������������������,����++-&��,-/,61-�����������������������,����----

----2222---- ++-�����������������������-����-����������-��-�������-����-�������++-�����������������������-����-����������-��-�������-����-�������++-�����������������������-����-����������-��-�������-����-�������++-�����������������������-����-����������-��-�������-����-�������----

----3333---- ++-�- ���-������-��-�������-�-�������,++-�- ���-������-��-�������-�-�������,++-�- ���-������-��-�������-�-�������,++-�- ���-������-��-�������-�-�������,----

----0000---- ----

----4444---- �� ���� ���� ���� ��----����������������-�����������������������-�����������������������-�����������������������-�����������������������----

----6666---- 5555----

----9999---- ------------����������������������������-������-'����7-������-'����7-������-'����7-������-'����7���8���8���8���8----

----::::---- ------------����������������������������-������-����7���8-������-����7���8-������-����7���8-������-����7���8----

----////---- ------------����������������������������-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8----

.<.<.<.<----------------����������������������������----�� ���� ���� ���� ��-���������8--���������8--���������8--���������8-++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����----

........----------------����������������������������----�� ���� ���� ���� ��-�����������8--�����������8--�����������8--�����������8-++-�������-���������++-�������-���������++-�������-���������++-�������-���������----

.2.2.2.2----------------����������������������������----�� ���� ���� ���� ��- ���������8-- ���������8-- ���������8-- ���������8-++- ���-������-���-++- ���-������-���-++- ���-������-���-++- ���-������-���-;��(;��(;��(;��(----

.3.3.3.3--------

.0.0.0.0----------------++-���++-���++-���++-���====��������-���������������-���������������-���������������-�������----

.4.4.4.4----------------�� ���� ���� ���� ��-�����������������������>-������-'����?-������-����?--�����������������������>-������-'����?-������-����?--�����������������������>-������-'����?-������-����?--�����������������������>-������-'����?-������-����?-----

.6.6.6.6----------������-���?-------������-���?-------������-���?-------������-���?-�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����?--����?--����?--����?-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

.9.9.9.9-------5---5---5---5----

.:.:.:.:----------------------------++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����----

././././----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

2<2<2<2<----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

2.2.2.2.----------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8----

22222222----------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����----

23232323----------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����----

20202020----------------------------����������������������>-������-@8-����������>-������-@8-����������>-������-@8-����������>-������-@8-++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������----

24242424-------B----B----B----B-++-���-���++-���-���++-���-���++-���-���====��������-�����������������������-���������������-�����������������������-���������������-�����������������������-���������������-�����������������������-�������----

26262626--------

Add instance variable ���������

Use method �������������to validate data

29

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

Outline

�����������������������������������������������������������������������,�����������,�����������,�����������,����

(2 of 4)

29292929----------------++-���-'����-����++-���-'����-����++-���-'����-����++-���-'����-����----

2:2:2:2:----------------�� ���� ���� ���� ��----������������-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@----

2/2/2/2/-------5---5---5---5----

3<3<3<3<----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

3.3.3.3.-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

32323232--------

33333333----------------++-������-'����-����++-������-'����-����++-������-'����-����++-������-'����-����----

30303030----------------�� ���� ���� ���� ��-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@----

34343434-------5---5---5---5----

36363636----------------------------������������������������-'����-'����-'����-'����7���87���87���87���8----

39393939-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

3:3:3:3:--------

3/3/3/3/----------------++-���-����-����++-���-����-����++-���-����-����++-���-����-����----

0<0<0<0<----------------�� ���� ���� ���� ��----������������-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@----

0.0.0.0.-------5---5---5---5----

02020202----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

03030303-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

00000000--------

04040404----------------++-������-����-����++-������-����-����++-������-����-����++-������-����-����----

06060606----------------�� ���� ���� ���� ��-������-���"�-������-���"�-������-���"�-������-���"���7���>@��7���>@��7���>@��7���>@----

09090909-------5---5---5---5----

0:0:0:0:----------------------------������������������������-����7���8-����7���8-����7���8-����7���8----

0/0/0/0/-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

4<4<4<4<--------

4.4.4.4.----------------++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��----

42424242----------------�� ���� ���� ���� ��----������������-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@----

43434343-------5---5---5---5----

40404040----------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-++-�����-��������++-�����-��������++-�����-��������++-�����-��������----

44444444-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

46464646--------

30

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

Outline

�����������������������������������������������������������������������,�����������,�����������,�����������,����

(3 of 4)

49494949----------------++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��----

4:4:4:4:----------------�� ���� ���� ���� ��-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@----

4/4/4/4/-------5---5---5---5----

6<6<6<6<----------------------------������������������������-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8----

6.6.6.6.-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

62626262--------

63636363----------------++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����----

60606060----------------�� ���� ���� ���� ��----������������-���������-���������-���������-������������>-���>-���>-���>-�� ���� ���� ���� ��-�����-@-�����-@-�����-@-�����-@----

64646464-------5---5---5---5----

66666666----------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-�����8-1-�����8-1-�����8-1-�����8----

69696969-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

6:6:6:6:--------

6/6/6/6/----------------++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����----

9<9<9<9<----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-������������>@-������������>@-������������>@-������������>@----

9.9.9.9.-------5---5---5---5----

92929292----------------------------������������������������-���������8-���������8-���������8-���������8----

93939393-------B----B----B----B-++++++++-���-�����-������������-���-�����-������������-���-�����-������������-���-�����-������������----

90909090--------

94949494----------------++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

96969696----------------�� ���� ���� ���� ��----������������-���������������>--���������������>--���������������>--���������������>-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

99999999-------5---5---5---5----

9:9:9:9:----------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-<,<<,<<,<<,<-FF-����-C--FF-����-C--FF-����-C--FF-����-C-.,<.,<.,<.,<-@-D-����-1--@-D-����-1--@-D-����-1--@-D-����-1-<,<<,<<,<<,<8888----

9/9/9/9/-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:<:<:<:<--------

:.:.:.:.----------------++-++-++-++-������-�������-����������-�������-����������-�������-����������-�������-����----

:2:2:2:2----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-���������������>@-���������������>@-���������������>@-���������������>@----

:3:3:3:3-------5---5---5---5----

:0:0:0:0----------------------------������������������������-�����������8-�����������8-�����������8-�����������8----

:4:4:4:4-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:6:6:6:6--------

Page 6: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

6

31

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

Outline

�����������������������������������������������������������������������,�����������,�����������,�����������,����

(4 of 4)

Lines 88-91

Lines 94-97

Line 102

Lines 108-113

:9:9:9:9----------------++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----------------------------------------------------------------------------------------------------------------------------------------------------

::::::::----------------�� ���� ���� ���� ��----������������-�������������>--�������������>--�������������>--�������������>-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----------------------------------------------------

:/:/:/:/----------------5555------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/</</</<---------------------------- ���������-A->-������-C- ���������-A->-������-C- ���������-A->-������-C- ���������-A->-������-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-������8-1-������8-1-������8-1-������8----------------------------

/./././.----------------B-B-B-B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������--------------------------------------------------------------------------------------------------------

/2/2/2/2--------

/3/3/3/3----------------++-������- ���-������++-������- ���-������++-������- ���-������++-������- ���-������----------------------------------------------------------------------------------------------------------------------------------------------------

/0/0/0/0----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�������������>@-�������������>@-�������������>@-�������������>@--------------------------------------------------------------------------------------------------------------------

/4/4/4/4----------------5555------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/6/6/6/6----------------------------������������������������- ���������8- ���������8- ���������8- ���������8----------------------------------------------------------------------------------------------------------------------------------------------------

/9/9/9/9----------------B-B-B-B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������--------------------------------------------------------------------------------------------------------------------

/:/:/:/:--------

////////----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

.<<.<<.<<.<<---- ------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

.<..<..<..<.---- ---5---5---5---5----

.<2.<2.<2.<2---- ------------------------������������������������---- ���������-N->-� ���������-N->-� ���������-N->-� ���������-N->-�����������-G-���������-@8����������-G-���������-@8����������-G-���������-@8����������-G-���������-@8----

.<3.<3.<3.<3---- ---B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

.<0.<0.<0.<0---- ----

.<4.<4.<4.<4---- ------------++-������-������-�������������-'-�����������������������++-������-������-�������������-'-�����������������������++-������-������-�������������-'-�����������������������++-������-������-�������������-'-�����������������������----

.<6.<6.<6.<6---- ------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

.<9.<9.<9.<9---- ---5---5---5---5----

.<:.<:.<:.<:---- ------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-----

.</.</.</.</---- ------------------------------------HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�I�I�I�1-I,2'�1-I,2'�1-I,2'�1-I,2'JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ����I�1-I,2'I�1-I,2'I�1-I,2'I�1-I,2'HHHH?-?-?-?-----

..<..<..<..<---- ------------------------------------H ���H ���H ���H ���====��������-�������-�������H��������-�������-�������H��������-�������-�������H��������-�������-�������H?-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���?----

............---- ------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-�����������7�� ��??-�����������7�� ��??-�����������7�� ��??-�����������7�� ��?----

..2..2..2..2---- ------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-���������?-?-���������?-?-���������?-?-���������?-H�������-����HH�������-����HH�������-����HH�������-����H?-�����������?-?-�����������?-?-�����������?-?-�����������?-----

..3..3..3..3---- ------------------------------------H ���-������HH ���-������HH ���-������HH ���-������H?- ���������?- ���������?- ���������?- ���������-@8-@8-@8-@8----

..0..0..0..0---- ---B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

..4..4..4..4---- B-B-B-B-++-���-����-�����������������������++-���-����-�����������������������++-���-����-�����������������������++-���-����-�����������������������----

Method ������������� validates data and sets instance variable ���������

Method ������������� returns the value of instance variable ���������

Update method �������� to calculate the earnings of a base-salaried commission employee

Update method �������to display base salary

32

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

Outline

�����������������������������������������������������������������������!���,�����������!���,�����������!���,�����������!���,����

(1 of 2)

Line 9-11

Lines 16-27

----....---- ++-&��,-/,91-�����������������������!���,����++-&��,-/,91-�����������������������!���,����++-&��,-/,91-�����������������������!���,����++-&��,-/,91-�����������������������!���,����----

----2222---- ++-!������-����-�����������������������,++-!������-����-�����������������������,++-!������-����-�����������������������,++-!������-����-�����������������������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-�����������������������!���--�����������������������!���--�����������������������!���--�����������������������!���-----

----4444---- 5555----

----6666---- ------------�� ���� ���� ���� ��----��������������������----������������-����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@-----

----9999---- ---5---5---5---5----

----::::---- ------------------------++-�����������-�����������++-�����������-�����������++-�����������-�����������++-�����������-�����������������������- ���������������- ���������������- ���������������- ���----

----////---- ------------------------�����������������������-�������-A�����������������������-�������-A�����������������������-�������-A�����������������������-�������-A------------------------------------------------------------

.<.<.<.<----------------------------------------��;��;��;��;-�����������������������>-�����������������������>-�����������������������>-�����������������������>------------------------------------------------------------------------

........----------------------------------------H� HH� HH� HH� H?-?-?-?-H"�;��HH"�;��HH"�;��HH"�;��H?-?-?-?-H333H333H333H333====33333333====3333H3333H3333H3333H?-?-?-?-4<<<4<<<4<<<4<<<?-?-?-?-,<0,<0,<0,<0?-?-?-?-3<<3<<3<<3<<-@8-@8-@8-@8----

.2.2.2.2--------------------------------

.3.3.3.3----------------------------++-���- ���++-���- ���++-���- ���++-���- ���====��������-��������-��������-��������-�������-�������-�����������-�������-�����������-�������-�����������-�������-����----

.0.0.0.0----------������,��,�������>------������,��,�������>------������,��,�������>------������,��,�������>----

.4.4.4.4----------------------------------------H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-JJJJ�H�H�H�H-@8-@8-@8-@8----

.6.6.6.6----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H&����-����-��HH&����-����-��HH&����-����-��HH&����-����-��H????----

.9.9.9.9----------------------------------------�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@-@8-@8-@8-@8----

.:.:.:.:----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-IHI�-IHI�-IHI�-I����JJJJ�H�H�H�H?-?-?-?-H"���-����-��HH"���-����-��HH"���-����-��HH"���-����-��H?-?-?-?-----

././././----------------------------------------�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@-@8-@8-@8-@8----

2<2<2<2<----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��H?-?-?-?-----

2.2.2.2.----------------------------------------�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@-@8-@8-@8-@8----

22222222----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-�����-��HH����-�����-��HH����-�����-��HH����-�����-��H?-?-?-?-----

23232323----------------------------------------�������,������������>@�������,������������>@�������,������������>@�������,������������>@-@8-@8-@8-@8----

20202020----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H��������-����-��HH��������-����-��HH��������-����-��HH��������-����-��H????----

24242424----------------------------------------�������,���������������>@�������,���������������>@�������,���������������>@�������,���������������>@-@8-@8-@8-@8----

26262626----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-������-��HH����-������-��HH����-������-��HH����-������-��H????----

29292929----------------------------------------�������,�������������>@�������,�������������>@�������,�������������>@�������,�������������>@-@8-@8-@8-@8----

2:2:2:2:--------

Instantiate ����������������������� object

Use ����������������������’s getmethods to retrieve the object’s instance variable values

33

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

Outline

�����������������������������������������������������������������������!���,�����������!���,�����������!���,�����������!���,����

(2 of 2)

Line 29

Line 33

Program output

2/2/2/2/----------------------------�������,�������������>-�������,�������������>-�������,�������������>-�������,�������������>-.<<<.<<<.<<<.<<<-@8--@8--@8--@8-++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

3<3<3<3<--------------------------------

3.3.3.3.----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HHHHJJJJ�I�1�I�1�I�1�I�1JJJJ����JJJJ�I��I��I��I�JJJJ�H�H�H�H?-?-?-?-----

32323232-------------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H?-?-?-?-----

33333333--------------------------------------------�������,�������>@-@8�������,�������>@-@8�������,�������>@-@8�������,�������>@-@8----

30303030-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

34343434----B-B-B-B-++-���-����-�����������������������!���++-���-����-�����������������������!���++-���-����-�����������������������!���++-���-����-�����������������������!���----

----�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1---------&����-����-��-� &����-����-��-� &����-����-��-� &����-����-��-� ----"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��----����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333====33333333====3333333333333333----����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<----��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0----����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<-------- ������-�������- ������-�������- ������-�������- ������-�������-��'������- ������- �-�������1��'������- ������- �-�������1��'������- ������- �-�������1��'������- ������- �-�������1-------- ��� ��� ��� ���====��������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;��----����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333====33333333====3333333333333333----����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<----�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0---- ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<<--------

Use �����������������������’s������������� methods to set base salary

Explicitly call object’s �������method

34

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

Software Engineering Observation 9.4

Copying and pasting code from one class to another can spread errors across multiple source code files. To avoid duplicating code (and possibly errors), use inheritance, rather than the “copy-and-paste” approach, in situations where you want one class to “absorb” the instance variables and methods of another class.

35

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

Software Engineering Observation 9.5

With inheritance, the common instance variables and methods of all the classes in the hierarchy are declared in a superclass. When changes are required for these common features, software developers need only to make the changes in the superclass—subclasses then inherit the changes. Without inheritance, changes would need to be made to all the source code files that contain a copy of the code in question.

36

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

9.4.3 Creating a ������������������������������������������������������������====������������������������������������������������������������������������������������ Inheritance Hierarchy

• Class �����������������������2�����������������������2�����������������������2�����������������������2

– Extends class ������������������������������������������������������������

– Is a ������������������������������������������������������������

– Has instance variable ��������� ��������� ��������� ���������

– Inherits �� ���� ���� ���� �� and ���������������������������� members– Constructor not inherited

Page 7: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

7

37

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

Outline

�����������������������������������������������������������������������2,�����������2,�����������2,�����������2,����

(1 of 3)

Line 4

Line 13

----....---- ++-&��,-/,:1-�����������������������2,����++-&��,-/,:1-�����������������������2,����++-&��,-/,:1-�����������������������2,����++-&��,-/,:1-�����������������������2,����----

----2222---- ++-�����������������������2-��������-'��-����-���������������,++-�����������������������2-��������-'��-����-���������������,++-�����������������������2-��������-'��-����-���������������,++-�����������������������2-��������-'��-����-���������������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-�����������������������2--�����������������������2--�����������������������2--�����������������������2-����������������������������-���������������-���������������-���������������-���������������----

----4444---- 5555----

----6666---- ------------����������������������������----�� ���� ���� ���� ��- ���������8-- ���������8-- ���������8-- ���������8-++- ���-������-++- ���-������-++- ���-������-++- ���-������-���-;��(���-;��(���-;��(���-;��(----

----9999---- ----

----::::---- ------------++-���++-���++-���++-���====��������-���������������-���������������-���������������-�������----

----////---- ------------�� ���� ���� ���� ��-�����������������������2>-������-'����?-������-����?--�����������������������2>-������-'����?-������-����?--�����������������������2>-������-'����?-������-����?--�����������������������2>-������-'����?-������-����?-----

.<.<.<.<----------������-���?-------������-���?-------������-���?-------������-���?-�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����?--����?--����?--����?-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

........-------5---5---5---5----

.2.2.2.2----------------------------++-�������-���-�-���������-���������������++-�������-���-�-���������-���������������++-�������-���-�-���������-���������������++-�������-���-�-���������-���������������-�������-�������-�������-�������----

.3.3.3.3----------------------------��������������������>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8--------------------------------------------------------------------------------------------

.0.0.0.0--------

.4.4.4.4----------�������������>-�����-@8-------�������������>-�����-@8-------�������������>-�����-@8-------�������������>-�����-@8-++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������----

.6.6.6.6-------B----B----B----B-++-���-���++-���-���++-���-���++-���-���====��������-�����������������������2-���������������-�����������������������2-���������������-�����������������������2-���������������-�����������������������2-�������----

.9.9.9.9--------------------

.:.:.:.:----------------++-���-++-���-++-���-++-���- ���-������ ���-������ ���-������ ���-������----

././././----------------�� ���� ���� ���� ��----������������-�������������>--�������������>--�������������>--�������������>-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

2<2<2<2<-------5---5---5---5----

2.2.2.2.---------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-������8-1-������8-1-������8-1-������8----

22222222-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

23232323--------

Class ����������������������2is a subclass of ���������������

Invoke the superclass constructor using the superclass constructor call syntax

38

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

Outline

�����������������������������������������������������������������������2,�����������2,�����������2,�����������2,����

(2 of 3)

Line 34

Lines 41-46

20202020----------------++-������- ���-������++-������- ���-������++-������- ���-������++-������- ���-������----

24242424----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�������������>@-�������������>@-�������������>@-�������������>@----

26262626-------5---5---5---5----

29292929----------------------------������������������������- ���������8- ���������8- ���������8- ���������8----

2:2:2:2:-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

2/2/2/2/--------

3<3<3<3<----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

3.3.3.3.----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

32323232-------5---5---5---5----

33333333----------------------------++-��-���;��1-���++-��-���;��1-���++-��-���;��1-���++-��-���;��1-�����������-���-���������-�������-��-�����������������-���-���������-�������-��-�����������������-���-���������-�������-��-�����������������-���-���������-�������-��-���������----

30303030----------------------------������������������������- ���������-N->-- ���������-N->-- ���������-N->-- ���������-N->-��������������������������������������������-G--G--G--G-������������������������������������-@8-@8-@8-@8----------------------------------------------------------------

34343434-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

36363636--------

39393939----------------++-������-������-�������������-'-�����������������������2++-������-������-�������������-'-�����������������������2++-������-������-�������������-'-�����������������������2++-������-������-�������������-'-�����������������������2----

3:3:3:3:----------------�� ���� ���� ���� ��-�����-�����-�����-������-�������>@�-�������>@�-�������>@�-�������>@----

3/3/3/3/-------5---5---5---5----

0<0<0<0<----------------------------++-��-���;��1-��������-�-����-�������-���������-��� ���++-��-���;��1-��������-�-����-�������-���������-��� ���++-��-���;��1-��������-�-����-�������-���������-��� ���++-��-���;��1-��������-�-����-�������-���������-��� ���----------------

0.0.0.0.----------------------------������������������������-������,'����>-������,'����>-������,'����>-������,'����>--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

02020202----------------------------------------HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H????------------------------------------------------

03030303----------------------------------------H ���H ���H ���H ���====��������-�������-�������H��������-�������-�������H��������-�������-�������H��������-�������-�������H?-?-?-?-'����7���'����7���'����7���'����7���?-?-?-?-����7�������7�������7�������7���????--------------------

00000000----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-?-?-?-�����������7�� �������������7�� �������������7�� �������������7�� ��????------------------------------------------------------------

04040404----------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-?-?-?-������������������������������������?-?-?-?-H�������-����HH�������-����HH�������-����HH�������-����H?-?-?-?-��������������������������������������������????----

06060606----------------------------------------H ���-������HH ���-������HH ���-������HH ���-������H?- ���?- ���?- ���?- ���������-@8������-@8������-@8������-@8----------------------------------------------------------------------------------------------------------------------------------------

09090909-------B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

0:0:0:0:----B-B-B-B-++-���-����-�����������������������2++-���-����-�����������������������2++-���-����-�����������������������2++-���-����-�����������������������2----

Compiler generates errors because superclass’s instance variable ����������� and ��������� are �������

Compiler generates errors because superclass’s instance variable '����7���, ����7���, �����������7�� ��, ��������� and ����������� are �������

39

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

Outline

�����������������������������������������������������������������������2,�����������2,�����������2,�����������2,����

(3 of 3)

Compiler generated errorss

----�����������������������2,����1301-�����������-���-�������-����-��-�����������������������2,����1301-�����������-���-�������-����-��-�����������������������2,����1301-�����������-���-�������-����-��-�����������������������2,����1301-�����������-���-�������-����-��-������������������������������������������������������������----------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8--------------------------------O----------------------------O----------------------------O----------------------------O----�����������������������2,����1301-���������-���-�������-����-��-�����������������������2,����1301-���������-���-�������-����-��-�����������������������2,����1301-���������-���-�������-����-��-�����������������������2,����1301-���������-���-�������-����-��-������������������������������������������������������������----------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8------������- ���������-N->-�����������-G-���������-@8-------------------------------------------------O---------------------------------------------O---------------------------------------------O---------------------------------------------O----�����������������������2,����1031-'����7���-���-�������-����-��-�����������������������2,����1031-'����7���-���-�������-����-��-�����������������������2,����1031-'����7���-���-�������-����-��-�����������������������2,����1031-'����7���-���-�������-����-��-������������������������������������������������������������-------------H ���---------H ���---------H ���---------H ���====��������-�������-�������H��������-�������-�������H��������-�������-�������H��������-�������-�������H?-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���?--------------------------------------------------O----------------------------------------------O----------------------------------------------O----------------------------------------------O----�����������������������2,����1031-����7���-���-�������-����-��-�����������������������2,����1031-����7���-���-�������-����-��-�����������������������2,����1031-����7���-���-�������-����-��-�����������������������2,����1031-����7���-���-�������-����-��-������������������������������������������������������������-------------H ���---------H ���---------H ���---------H ���====��������-�������-�������H?-'����7���?-����7���?��������-�������-�������H?-'����7���?-����7���?��������-�������-�������H?-'����7���?-����7���?��������-�������-�������H?-'����7���?-����7���?-------------------------------------------------------------------------------------------------------------------------------------------------------O---------------------------O---------------------------O---------------------------O----�����������������������2,����1001-�����������7�� ��-���-�������-����-��-�����������������������2,����1001-�����������7�� ��-���-�������-����-��-�����������������������2,����1001-�����������7�� ��-���-�������-����-��-�����������������������2,����1001-�����������7�� ��-���-�������-����-��-������������������������������������������������������������-------------H����-�������-��� ��H?-�����������7�� ��?---------H����-�������-��� ��H?-�����������7�� ��?---------H����-�������-��� ��H?-�����������7�� ��?---------H����-�������-��� ��H?-�����������7�� ��?---------------------------------------O-----------------------------------O-----------------------------------O-----------------------------------O----�����������������������2,����10�����������������������2,����10�����������������������2,����10�����������������������2,����1041-���������-���-�������-����-��-41-���������-���-�������-����-��-41-���������-���-�������-����-��-41-���������-���-�������-����-��-������������������������������������������������������������-------------H����-�����H?-���������?-H�������-����H?-�����������?---------H����-�����H?-���������?-H�������-����H?-�����������?---------H����-�����H?-���������?-H�������-����H?-�����������?---------H����-�����H?-���������?-H�������-����H?-�����������?----------------------------O------------------------O------------------------O------------------------O----�����������������������2,����1041-�����������-���-�������-����-��-�����������������������2,����1041-�����������-���-�������-����-��-�����������������������2,����1041-�����������-���-�������-����-��-�����������������������2,����1041-�����������-���-�������-����-��-������������������������������������������������������������-------------H---------H---------H---------H����-�����H?-���������?-H�������-����H?-�����������?����-�����H?-���������?-H�������-����H?-�����������?����-�����H?-���������?-H�������-����H?-�����������?����-�����H?-���������?-H�������-����H?-�����������?-----------------------------------------------------------O-------------------------------------------------------O-------------------------------------------------------O-------------------------------------------------------O----9-�����9-�����9-�����9-�����--------

40

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

Common Programming Error 9.2

A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not match exactly the number and types of parameters specified in one of the superclass constructor declarations.

41

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

9.4.4 ������������������������������������������������������������====�������������������������������������������������������������������������������������������� Inheritance Hierarchy Using protected Instance Variables

• Use ���������������������������� instance variables– Enable class �������������������������������������������������������������������������������������������� to

directly access superclass instance variables– Superclass’s ���������������������������� members are inherited by all

subclases of that superclass

42

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

Outline

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

�������2,�����������2,�����������2,�����������2,����

(1 of 4)

Line 6-10

----....---- ++-&��,-/,/1-���������������2,����++-&��,-/,/1-���������������2,����++-&��,-/,/1-���������������2,����++-&��,-/,/1-���������������2,����----

----2222---- ++-���������������2-����-����������-�-�������-�������,++-���������������2-����-����������-�-�������-�������,++-���������������2-����-����������-�-�������-�������,++-���������������2-����-����������-�-�������-�������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������----���������������2���������������2���������������2���������������2----

----4444---- 5555----

----6666---- ------------����������������������������-������-'����7���8-������-'����7���8-������-'����7���8-������-'����7���8----------------------------------------------------------------------------------------------------------------------------

----9999---- ------------����������������������������-������-����7���8-������-����7���8-������-����7���8-������-����7���8--------------------------------------------------------------------------------------------------------------------------------

----::::---- ------------����������������������������-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8--------------------------------------------------------------------------------

----////---- ------------����������������������������----�� ���� ���� ���� ��-���������8--���������8--���������8--���������8-++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����--------------------------------

.<.<.<.<----------------����������������������������----�� ���� ���� ���� ��-�����������8--�����������8--�����������8--�����������8-++-�������-���������++-�������-���������++-�������-���������++-�������-���������----

........--------

.2.2.2.2----------------++-'���++-'���++-'���++-'���====��������������������������������-�������-�������-�������-�������----

.3.3.3.3----------------�� ���� ���� ���� ��----���������������2���������������2���������������2���������������2>-������-'����?-������-����?-������-���?->-������-'����?-������-����?-������-���?->-������-'����?-������-����?-������-���?->-������-'����?-������-����?-������-���?-----

.0.0.0.0----------------------------�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

.4.4.4.4-------5---5---5---5----

.6.6.6.6----------------------------++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����----

.9.9.9.9----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

.:.:.:.:----------����7���-A-����------����7���-A-����------����7���-A-����------����7���-A-����8888----

././././----------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8----

2<2<2<2<----------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����----

2.2.2.2.----------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����----

22222222-------B----B----B----B-++-���-'���++-���-'���++-���-'���++-���-'���====��������-���������������2-���������������-���������������2-���������������-���������������2-���������������-���������������2-�������----

23232323--------

20202020----------------++-���-'����-����++-���-'����-����++-���-'����-����++-���-'����-����----

24242424----------------�� ���� ���� ���� ��----������������-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@----

26262626-------5---5---5---5----

29292929----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

2:2:2:2:-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

2/2/2/2/--------

Declare �������instance variables

Page 8: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

8

43

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

Outline

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

�������2,�����������2,�����������2,�����������2,����

(2 of 4)

3<3<3<3<----------------++-������-'����-����++-������-'����-����++-������-'����-����++-������-'����-����----

3.3.3.3.----------------�� ���� ���� ���� ��-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@----

32323232-------5---5---5---5----

33333333----------------------------������������������������-'����7���8-'����7���8-'����7���8-'����7���8----

30303030-------B---B---B---B-++-���-�����-���&����7���-++-���-�����-���&����7���-++-���-�����-���&����7���-++-���-�����-���&����7���----

34343434--------

36363636----------------++-���-����-����++-���-����-����++-���-����-����++-���-����-����----

39393939----------------�� ���� ���� ���� ��----������������-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@----

3:3:3:3:-------5---5---5---5----

3/3/3/3/----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

0<0<0<0<-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

0.0.0.0.--------

02020202----------------++-������-����-����++-������-����-����++-������-����-����++-������-����-����----

03030303----------------�� ���� ���� ���� ��-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@----

00000000-------5---5---5---5----

04040404----------------------------������������������������-����7���8-����7���8-����7���8-����7���8----

06060606-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

09090909--------

0:0:0:0:----------------++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��----

0/0/0/0/----------------�� ���� ���� ���� ��----������������-���������-���������-���������-��������������7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@----

4<4<4<4<-------5---5---5---5----

4.4.4.4.----------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-++-�����-��������++-�����-��������++-�����-��������++-�����-��������----

42424242-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

43434343--------

40404040----------------++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��----

44444444----------------�� ���� ���� ���� ��-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@----

46464646-------5---5---5---5----

49494949----------------------------������������������������-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8----

4:4:4:4:-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

4/4/4/4/--------

44

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

Outline

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

�������2,�����������2,�����������2,�����������2,����

(3 of 4)

6<6<6<6<----------------++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����----

6.6.6.6.----------------�� ���� ���� ���� ��----������������-������������>--������������>--������������>--������������>-�� ���� ���� ���� ��-�����-@-�����-@-�����-@-�����-@----

62626262-------5---5---5---5----

63636363----------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-�����8-1-�����8-1-�����8-1-�����8----

60606060-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

64646464--------

66666666----------------++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����----

69696969----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�����-�����-�����-������������>@�������>@�������>@�������>@----

6:6:6:6:-------5---5---5---5----

6/6/6/6/----------------------------������������������������-���������8-���������8-���������8-���������8----

9<9<9<9<-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

9.9.9.9.--------

92929292----------------++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

93939393----------------�� ���� ���� ���� ��----������������-���������������>--���������������>--���������������>--���������������>-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

90909090-------5---5---5---5----

94949494----------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-<,<<,<<,<<,<-FF-����-C--FF-����-C--FF-����-C--FF-����-C-.,<.,<.,<.,<-@-D-����-1--@-D-����-1--@-D-����-1--@-D-����-1-<<<<,<,<,<,<8888----

96969696-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

99999999--------

9:9:9:9:----------------++-������-�������-����++-������-�������-����++-������-�������-����++-������-�������-����----

9/9/9/9/----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-���������������>@-���������������>@-���������������>@-���������������>@----

:<:<:<:<-------5---5---5---5----

:.:.:.:.----------------------------������������������������-�����������8-�����������8-�����������8-�����������8----

:2:2:2:2-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:3:3:3:3--------

:0:0:0:0----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

:4:4:4:4----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

:6:6:6:6-------5---5---5---5----

:9:9:9:9----------------------------������������������������-�����������-G-���������8-�����������-G-���������8-�����������-G-���������8-�����������-G-���������8----

::::::::-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

:/:/:/:/--------

45

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

Outline

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

�������2,�����������2,�����������2,�����������2,����

(4 of 4)

/</</</<----------------++-������-������-�������������-'-���������������2- ���++-������-������-�������������-'-���������������2- ���++-������-������-�������������-'-���������������2- ���++-������-������-�������������-'-���������������2- ���----

/./././.----------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

/2/2/2/2-------5---5---5---5----

/3/3/3/3----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H?-?-?-?-----

/0/0/0/0----------------------------------------H�������-�������HH�������-�������HH�������-�������HH�������-�������H?-'����7���?-����7���?-?-'����7���?-����7���?-?-'����7���?-����7���?-?-'����7���?-����7���?-----

/4/4/4/4----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-�����������7�� ��?-?-�����������7�� ��?-?-�����������7�� ��?-?-�����������7�� ��?-----

/6/6/6/6----------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-���������?-?-���������?-?-���������?-?-���������?-----

/9/9/9/9----------------------------------------H�������-����HH�������-����HH�������-����HH�������-����H?-�����������-@8?-�����������-@8?-�����������-@8?-�����������-@8----

/:/:/:/:-------B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

////////----B-B-B-B-++-���-����-���������������2++-���-����-���������������2++-���-����-���������������2++-���-����-���������������2----

46

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

Outline

�����������������������������������������������������������������������3,�����������3,�����������3,�����������3,����

(1 of 2)

Line 13

----....---- ++-&��,-/,.<1-�����������������������3,����++-&��,-/,.<1-�����������������������3,����++-&��,-/,.<1-�����������������������3,����++-&��,-/,.<1-�����������������������3,����----

----2222---- ++-�����������������������3-��������-'��-���������������2-���-���++-�����������������������3-��������-'��-���������������2-���-���++-�����������������������3-��������-'��-���������������2-���-���++-�����������������������3-��������-'��-���������������2-���-���--------

----3333---- ++-����-�-���������������2P�-�������-��� ���,++-����-�-���������������2P�-�������-��� ���,++-����-�-���������������2P�-�������-��� ���,++-����-�-���������������2P�-�������-��� ���,----

----0000---- ----

----4444---- �� ���� ���� ���� ��----����������������-�����������������������3--�����������������������3--�����������������������3--�����������������������3-����������������������������-����������-����������-����������-���������������2�����2�����2�����2----

----6666---- 5555----

----9999---- ------------����������������������������----�� ���� ���� ���� ��- ���������8-- ���������8-- ���������8-- ���������8-++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(----

----::::---- ----

----////---- ------------++-���++-���++-���++-���====��������-���������������-���������������-���������������-�������----

.<.<.<.<----------------�� ���� ���� ���� ��-�����������������������3>-������-'����?-������-����?--�����������������������3>-������-'����?-������-����?--�����������������������3>-������-'����?-������-����?--�����������������������3>-������-'����?-������-����?-----

........----------������-���?-------������-���?-------������-���?-------������-���?-�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����?--����?--����?--����?-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

.2.2.2.2----------5--5--5--5----

.3.3.3.3----------------------------��������������������>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8----

.0.0.0.0----------�������������>-������-@8-------�������������>-������-@8-------�������������>-������-@8-------�������������>-������-@8-++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������----

.4.4.4.4-------B----B----B----B-++-���-���++-���-���++-���-���++-���-���====��������-�����������������������3-���������������-�����������������������3-���������������-�����������������������3-���������������-�����������������������3-�������----

.6.6.6.6--------------------

.9.9.9.9----------------++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

.:.:.:.:----------------�� ���� ���� ���� ��----������������-�����-�����-�����-�������������>-��������>-��������>-��������>-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

././././-------5---5---5---5----

2<2<2<2<---------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-������8-1-������8-1-������8-1-������8----

2.2.2.2.-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

22222222--------

23232323----------------++-������- ���-������++-������- ���-������++-������- ���-������++-������- ���-������----

20202020----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�������������>@-�������������>@-�������������>@-�������������>@----

24242424-------5---5---5---5----

26262626----------------------------������������������������- ���������8- ���������8- ���������8- ���������8----

29292929-------B----B----B----B-+++++-���-�����-�������������+-���-�����-�������������+-���-�����-�������������+-���-�����-�������������----

2:2:2:2:--------

Must call superclass’sconstructor

47

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

Outline

�����������������������������������������������������������������������3,�����������3,�����������3,�����������3,����

(2 of 2)

Line 32

Lines 38-43

2/2/2/2/----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

3<3<3<3<----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

3.3.3.3.-------5---5---5---5----

32323232----------------------------������������������������- ���������-N->-�����������-G-���������-@8- ���������-N->-�����������-G-���������-@8- ���������-N->-�����������-G-���������-@8- ���������-N->-�����������-G-���������-@8----

33333333-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

30303030--------

34343434----------------++-������-������-�������������-'-�����������������������3++-������-������-�������������-'-�����������������������3++-������-������-�������������-'-�����������������������3++-������-������-�������������-'-�����������������������3----

36363636----------------�������� �� �� �� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

39393939-------5---5---5---5----

3:3:3:3:----------------------------������������������������-������,'����>-������,'����>-������,'����>-������,'����>--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3/3/3/3/----------------------------------------HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H????------------------------------------------------

0<0<0<0<----------------------------------------H ���H ���H ���H ���====��������-�������-�������H��������-�������-�������H��������-�������-�������H��������-�������-�������H?-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���??-'����7���?-����7���?--------------------

0.0.0.0.----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-�����������7�� ��??-�����������7�� ��??-�����������7�� ��??-�����������7�� ��?------------------------------------------------------------

02020202----------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-���������?-?-���������?-?-���������?-?-���������?-H�������-����HH�������-����HH�������-����HH�������-����H?-�����������??-�����������??-�����������??-�����������?----

03030303----------------------------------------H ���-������HH ���-������HH ���-������HH ���-������H?- ���������-@8?- ���������-@8?- ���������-@8?- ���������-@8----------------------------------------------------------------------------------------------------------------------------------------

00000000-------B----B----B----B-++-���-�����++-���-�����++-���-�����++-���-�����-�������-�������-�������-�������----

04040404----B-B-B-B-++-���-����-�����������������������3++-���-����-�����������������������3++-���-����-�����������������������3++-���-����-�����������������������3----

Directly access superclass’s �������instance variables

48

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

Outline

�����������������������������������������������������������������������!���3,�����������!���3,�����������!���3,�����������!���3,����

(1 of 2)

----....---- ++-&��,-/,..1-�����������������������!���3,����++-&��,-/,..1-�����������������������!���3,����++-&��,-/,..1-�����������������������!���3,����++-&��,-/,..1-�����������������������!���3,����----

----2222---- ++-!������-����-�����������������������3,++-!������-����-�����������������������3,++-!������-����-�����������������������3,++-!������-����-�����������������������3,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-�����������������������!���3--�����������������������!���3--�����������������������!���3--�����������������������!���3-----

----4444---- 5555----

----6666---- ------------�� ���� ���� ���� ��----��������������������----������������-����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@-----

----9999---- ---5---5---5---5----

----::::---- ------------------------++-�����������-��������++-�����������-��������++-�����������-��������++-�����������-�����������������������3- ������������������3- ������������������3- ������������������3- ���----

----////---- ------------------------�����������������������3-�������-A�����������������������3-�������-A�����������������������3-�������-A�����������������������3-�������-A--------------------------------------------------------

.<.<.<.<----------------------------------------��;��;��;��;-�����������������������3>-�����������������������3>-�����������������������3>-�����������������������3>--------------------------------------------------------------------

........----------------------------------------H� HH� HH� HH� H?-?-?-?-H"�;��HH"�;��HH"�;��HH"�;��H?-?-?-?-H333H333H333H333====33333333====3333H3333H3333H3333H?-?-?-?-4<<<4<<<4<<<4<<<?-?-?-?-,<0,<0,<0,<0?-?-?-?-3<<3<<3<<3<<-@8-@8-@8-@8----

.2.2.2.2--------------------------------

.3.3.3.3----------------------------++-���- ���++-���- ���++-���- ���++-���- ���====��������������������������-�������-�������-������-�������-�������-������-�������-�������-������-�������-�������-����----

.0.0.0.0----------������,��,�������>------������,��,�������>------������,��,�������>------������,��,�������>----

.4.4.4.4----------------------------------------H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-JJJJ�H�H�H�H-@8-@8-@8-@8----

.6.6.6.6----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H&����-����-��HH&����-����-��HH&����-����-��HH&����-����-��H????----

.9.9.9.9----------------------------------------�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@-@8-@8-@8-@8----

.:.:.:.:----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H"���-����-��HH"���-����-��HH"���-����-��HH"���-����-��H?-?-?-?-----

././././----------------------------------------�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@-@8-@8-@8-@8----

2<2<2<2<----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��H?-?-?-?-----

2.2.2.2.----------------------------------------�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@-@8-@8-@8-@8----

22222222----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-�����-��HH����-�����-��HH����-�����-��HH����-�����-��H?-?-?-?-----

22223333----------------------------------------�������,������������>@�������,������������>@�������,������������>@�������,������������>@-@8-@8-@8-@8----

20202020----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H��������-����-��HH��������-����-��HH��������-����-��HH��������-����-��H????----

24242424----------------------------------------�������,���������������>@�������,���������������>@�������,���������������>@�������,���������������>@-@8-@8-@8-@8----

26262626----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-������-��HH����-������-��HH����-������-��HH����-������-��H????----

29292929----------------------------------------�������,�������������>@-@8�������,�������������>@-@8�������,�������������>@-@8�������,�������������>@-@8----

2222::::--------

Page 9: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

9

49

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

Outline

�����������������������������������������������������������������������!���3,�����������!���3,�����������!���3,�����������!���3,����

(2 of 2)

Program output

2/2/2/2/----------------------------�������,�������������>-�������,�������������>-�������,�������������>-�������,�������������>-.<<<.<<<.<<<.<<<-@8--@8--@8--@8-++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

3<3<3<3<--------------------------------

3.3.3.3.----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HHHHJJJJ�I�1�I�1�I�1�I�1JJJJ����JJJJ�I��I��I��I�JJJJ�H�H�H�H?-?-?-?-----

32323232-------------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H---------H ������-�������-��'������- ������- �-�������H????----

33333333----------------------------------------�������,�������>@�������,�������>@�������,�������>@�������,�������>@-@8-@8-@8-@8----

30303030-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

34343434----B-B-B-B-++++++++-���-����-�����������������������!���3-���-����-�����������������������!���3-���-����-�����������������������!���3-���-����-�����������������������!���3----

----�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1---------&����-����-��-� &����-����-��-� &����-����-��-� &����-����-��-� ----"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��----����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333====33333333====3333333333333333----����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<----��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0----����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<-------- ������-�������-� ������-�������-� ������-�������-� ������-�������-��'������- ������- �-�������1�'������- ������- �-�������1�'������- ������- �-�������1�'������- ������- �-�������1-------- ��� ��� ��� ���====��������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;��----����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333====33333333====3333333333333333----����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<----�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0---- ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<<--------

50

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

9.4.4 ������������������������������������������������������������====�������������������������������������������������������������������������������������������� Inheritance Hierarchy Using protected Instance Variables (Cont.)

• Using ���������������������������� instance variables– Advantages

• subclasses can modify values directly• Slight increase in performance

– Avoid set/get method call overhead– Disadvantages

• No validity checking– subclass can assign illegal value

• Implementation dependent– subclass methods more likely dependent on superclass

implementation– superclass implementation changes may result in subclass

modifications• Fragile (brittle) software

51

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

Software Engineering Observation 9.6

Use the ���������������������������� access modifier when a superclass should provide a method only to its subclasses and other classes in the same package, but not to other clients.

52

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

Software Engineering Observation 9.7

Declaring superclass instance variables ����������������������������

(as opposed to ����������������������������) enables the superclassimplementation of these instance variables to change without affecting subclass implementations.

53

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

Error-Prevention Tip 9.1

When possible, do not include ���������������������������� instance variables in a superclass. Instead, include non-���������������������������� methods that access ���������������������������� instance variables. This will ensure that objects of the class maintain consistent states.

54

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

9.4.5 ������������������������������������������������������������====�������������������������������������������������������������������������������������������� Inheritance Hierarchy Uing private Instance Variables

• Reexamine hierarchy– Use the best software engineering practice

• Declare instance variables as ����������������������������

• Provide public get and set methods• Use get method to obtain values of instance variables

Page 10: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

10

55

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

Outline

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

�������3,�����������3,�����������3,�����������3,����

(1 of 4)

Lines 6-10

----....---- ++-&��,-/,.21-���������������3,����++-&��,-/,.21-���������������3,����++-&��,-/,.21-���������������3,����++-&��,-/,.21-���������������3,����----

----2222---- ++-���������������3-����-����������-�-�������-�������,++-���������������3-����-����������-�-�������-�������,++-���������������3-����-����������-�-�������-�������,++-���������������3-����-����������-�-�������-�������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-���������������3-���������������3-���������������3-���������������3----

----4444---- 5555----

----6666---- ------------����������������������������-������-'����7���8-������-'����7���8-������-'����7���8-������-'����7���8----------------------------------------------------------------------------------------------------------------------------

----9999---- ------------����������������������������-������-����7���8-������-����7���8-������-����7���8-������-����7���8--------------------------------------------------------------------------------------------------------------------------------

----::::---- ------------����������������������������-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8-������-�����������7�� ��8--------------------------------------------------------------------------------

----////---- ------------����������������������������----�� ���� ���� ���� ��-���������8--���������8--���������8--���������8-++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����--------------------------------

.<.<.<.<----------------����������������������������----�� ���� ���� ���� ��-�����������8--�����������8--�����������8--�����������8-++-�������-���������++-�������-���������++-�������-���������++-�������-���������----

........--------

.2.2.2.2----------------++-'���++-'���++-'���++-'���====��������-�������������-�������������-�������������-�������������----

.3.3.3.3----------------�� ���� ���� ���� ��-���������������3>-������-'����?-������-����?-������-���?--���������������3>-������-'����?-������-����?-������-���?--���������������3>-������-'����?-������-����?-������-���?--���������������3>-������-'����?-������-����?-������-���?-----

.0.0.0.0----------------------------�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

.4.4.4.4-------5---5---5---5----

.6.6.6.6----------------------------++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����----

.9.9.9.9----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

.:.:.:.:----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

././././----------------�����������7�� ��-A-���8----�����������7�� ��-A-���8----�����������7�� ��-A-���8----�����������7�� ��-A-���8----

2<2<2<2<----------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����++-��������-���-����-����-�����----

2.2.2.2.----------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����----

22222222-------B----B----B----B-++-���-'���++-���-'���++-���-'���++-���-'���====��������-���������������3-���������������-���������������3-���������������-���������������3-���������������-���������������3-�������----

23232323--------

20202020----------------++++++++-���-'����-����-���-'����-����-���-'����-����-���-'����-����----

24242424----------------�� ���� ���� ���� ��----������������-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@----

26262626-------5---5---5---5----

29292929----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

2:2:2:2:-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

2/2/2/2/--------

Declare �������instance variables

56

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

Outline

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

�������3,�����������3,�����������3,�����������3,����

(2 of 4)

3<3<3<3<----------------++-������-'����-����++-������-'����-����++-������-'����-����++-������-'����-����----

3.3.3.3.----------------�� ���� ���� ���� ��-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@----

32323232-------5---5---5---5----

33333333----------------------------������������������������-'����7���8-'����7���8-'����7���8-'����7���8----

30303030-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

34343434--------

36363636----------------++-���-����-����++-���-����-����++-���-����-����++-���-����-����----

39393939----------------�� ���� ���� ���� ��----������������-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@----

3:3:3:3:-------5---5---5---5----

3/3/3/3/----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

0<0<0<0<-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

0.0.0.0.--------

02020202----------------++-������-����-����++-������-����-����++-������-����-����++-������-����-����----

03030303----------------�� ���� ���� ���� ��-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@-������-���"���7���>@----

00000000-------5---5---5---5----

04040404----------------------------������������������������-����7���8-����7���8-����7���8-����7���8----

06060606-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

09090909--------

0:0:0:0:----------------++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��----

0/0/0/0/----------------�� ���� ���� ���� ��----������������-���������-���������-���������-��������������7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@�����7�� ��>-������-���-@----

4<4<4<4<-------5---5---5---5----

4.4.4.4.----------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-++-�����-��������++-�����-��������++-�����-��������++-�����-��������----

42424242-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

43434343--------

40404040----------------++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��----

44444444----------------�� ���� ���� ���� ��-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@----

46464646-------5---5---5---5----

49494949----------------------------������������������������-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8----

4:4:4:4:-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

4/4/4/4/--------

57

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

Outline

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

�������3,�����������3,�����������3,�����������3,����

(3 of 4)

6<6<6<6<----------------++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����----

6.6.6.6.----------------�� ���� ���� ���� ��----������������-������������>--������������>--������������>--������������>-�� ���� ���� ���� ��-�����-@-�����-@-�����-@-�����-@----

62626262-------5---5---5---5----

63636363----------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-�����8-1-�����8-1-�����8-1-�����8----

60606060-------B----B----B----B-++-���-�����-�����������++-���-�����-�����������++-���-�����-�����������++-���-�����-���������������----

64646464--------

66666666----------------++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����----

69696969----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�����-�����-�����-������������>@�������>@�������>@�������>@----

6:6:6:6:-------5---5---5---5----

6/6/6/6/----------------------------������������������������-���������8-���������8-���������8-���������8----

9<9<9<9<-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

9.9.9.9.--------

92929292----------------++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

93939393----------------�� ���� ���� ���� ��----������������-���������������>--���������������>--���������������>--���������������>-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

90909090-------5---5---5---5----

94949494----------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-<,<<,<<,<<,<-FF-����-C--FF-����-C--FF-����-C--FF-����-C-.,<.,<.,<.,<-@-D-����-1--@-D-����-1--@-D-����-1--@-D-����-1-<<<<,<,<,<,<8888----

96969696-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

99999999--------

9:9:9:9:----------------++-������-�������-����++-������-�������-����++-������-�������-����++-������-�������-����----

9/9/9/9/----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-���������������>@-���������������>@-���������������>@-���������������>@----

:<:<:<:<-------5---5---5---5----

:.:.:.:.----------------------------������������������������-�����������8-�����������8-�����������8-�����������8----

:2:2:2:2-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:3:3:3:3--------

58

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

Outline

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

�������3,�����������3,�����������3,�����������3,����

(4 of 4)

Line 87

Lines 94-97

:0:0:0:0----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

:4:4:4:4----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

:6:6:6:6-------5---5---5---5----

:9:9:9:9----------------------------������������������������----���������������>@���������������>@���������������>@���������������>@-G--G--G--G-������������>@������������>@������������>@������������>@8888----

::::::::-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

:/:/:/:/--------

/</</</<----------------++-������-������-�������������-'-���������������3- ���++-������-������-�������������-'-���������������3- ���++-������-������-�������������-'-���������������3- ���++-������-������-�������������-'-���������������3- ���----

/./././.----------------�� ���� ���� ���� ��-���-���-���-������-�������>@���-�������>@���-�������>@���-�������>@----

/2/2/2/2-------5---5---5---5----

/3/3/3/3----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H?-?-?-?-----

/0/0/0/0----------------------------------------H�������-�������HH�������-�������HH�������-�������HH�������-�������H?-?-?-?-���&����7���>@���&����7���>@���&����7���>@���&����7���>@?-?-?-?-���"���7���>@���"���7���>@���"���7���>@���"���7���>@????--------

/4/4/4/4----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-?-?-?-��������������7�� ��>@��������������7�� ��>@��������������7�� ��>@��������������7�� ��>@????--------

/6/6/6/6----------------------------------------H���H���H���H����-�����H�-�����H�-�����H�-�����H?-?-?-?-������������>@������������>@������������>@������������>@????--------

/9/9/9/9----------------------------------------H�������-����HH�������-����HH�������-����HH�������-����H?-?-?-?-���������������>@���������������>@���������������>@���������������>@-@8-@8-@8-@8----

/:/:/:/:-------B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

////////----B-B-B-B-++-���-����-���������������3++-���-����-���������������3++-���-����-���������������3++-���-����-���������������3----

Use get methods to obtain the values of instance variables

59

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

Outline

�����������������������������������������������������������������������0,�����������0,�����������0,�����������0,����

(1 of 2)

----....---- ++-&��,-/,.31-�����������������������0,����++-&��,-/,.31-�����������������������0,����++-&��,-/,.31-�����������������������0,����++-&��,-/,.31-�����������������������0,����----

----2222---- ++-�����������������������0-����-��������-'��-���������������3-���++-�����������������������0-����-��������-'��-���������������3-���++-�����������������������0-����-��������-'��-���������������3-���++-�����������������������0-����-��������-'��-���������������3-���----

----3333---- ++-������-���������������3P�-�������-����-���-���������������3P�-++-������-���������������3P�-�������-����-���-���������������3P�-++-������-���������������3P�-�������-����-���-���������������3P�-++-������-���������������3P�-�������-����-���-���������������3P�-----

----0000---- ++-�� ��-������,++-�� ��-������,++-�� ��-������,++-�� ��-������,----

----4444---- ----

----6666---- �� ���� ���� ���� ��----����������������-����-����-����-�����������������������0-�������������������0-�������������������0-�������������������0-����������������������������-���������������3-���������������3-���������������3-���������������3----

----9999---- 5555----

----::::---- ------------����������������������������----�� ���� ���� ���� ��- ���������8-- ���������8-- ���������8-- ���������8-++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(----

----////---- ----

.<.<.<.<----------------++-���++-���++-���++-���====��������-���������������-���������������-���������������-�������----

........----------------�� ���� ���� ���� ��-�����������������������0>-������-'����?-������-����?--�����������������������0>-������-'����?-������-����?--�����������������������0>-������-'����?-������-����?--�����������������������0>-������-'����?-������-����?-----

.2.2.2.2----------������-���?-------������-���?-------������-���?-------������-���?-�� �� �� �� ��������-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����?--����?--����?--����?-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

.3.3.3.3-------5---5---5---5----

.0.0.0.0----------------------------��������������������>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8----

.4.4.4.4----------�������������>-������-@8-------�������������>-������-@8-------�������������>-������-@8-------�������������>-������-@8-++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������----

.6.6.6.6-------B----B----B----B-++-���-���++-���-���++-���-���++-���-���====��������-�����������������������0-���������������-�����������������������0-���������������-�����������������������0-���������������-�����������������������0-�������----

.9.9.9.9--------------------

.:.:.:.:----------------++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

././././----------------�� ���� ���� ���� ��----������������-�������������>--�������������>--�������������>--�������������>-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

2<2<2<2<-------5---5---5---5----

2.2.2.2.---------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-������8-1-������8-1-������8-1-������8----

22222222-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

23232323--------

Inherits from ���������������3

60

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

Outline

�����������������������������������������������������������������������0,�����������0,�����������0,�����������0,����

(2 of 2)

Line 33 & 40

Line 33

Lines 40

20202020----------------++-������- ���-������++-������- ���-������++-������- ���-������++-������- ���-������----

24242424----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-�������������>@-�������������>@-�������������>@-�������������>@----

26262626-------5---5---5---5----

29292929----------------------------������������������������- ���������8- ���������8- ���������8- ���������8----

2:2:2:2:-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

2/2/2/2/--------

3<3<3<3<----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

3.3.3.3.----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-��������>@-��������>@-��������>@-��������>@----

32323232-------5---5---5---5----

33333333----------------------------������������������������-�������������>-�������������>-�������������>-�������������>@-N-@-N-@-N-@-N-��������������������,��������>@8,��������>@8,��������>@8,��������>@8----

30303030-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

34343434--------

36363636----------------++-������-������-�������������-'-�����������������������0++-������-������-�������������-'-�����������������������0++-������-������-�������������-'-�����������������������0++-������-������-�������������-'-�����������������������0----

39393939----------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

3:3:3:3:-------5---5---5---5----

3/3/3/3/----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H?-?-?-?-H ���H ���H ���H ���====��������H��������H��������H��������H????----

0<0<0<0<----------------------------------------��������������������,�������>@?-,�������>@?-,�������>@?-,�������>@?-H ���-������HH ���-������HH ���-������HH ���-������H?-�������������>@-@8?-�������������>@-@8?-�������������>@-@8?-�������������>@-@8----------------

0.0.0.0.-------B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

02020202----B-B-B-B-++-���-����-�����������������������0++-���-����-�����������������������0++-���-����-�����������������������0++-���-����-�����������������������0----

Use get methods to obtain the values of instance variables

Invoke an overridden superclassmethod from a subclass

Invoke an overridden superclassmethod from a subclass

Page 11: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

11

61

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

Common Programming Error 9.3

When a superclass method is overridden in a subclass, the subclass version often calls the superclass version to do a portion of the work. Failure to prefix the superclass method name with the keyword �������������������� and a dot (.) separator when referencing the superclass’s method causes the subclass method to call itself, creating an error called infinite recursion. Recursion, used correctly, is a powerful capability discussed in Chapter 15, Recursion.

62

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

Outline

�����������������������������������������������������������������������!���0,�����������!���0,�����������!���0,�����������!���0,����

(1 of 2)

Lines 9-11

Lines 16-25

----....---- ++-&��,-/,.01-�����������������������!���0,����++-&��,-/,.01-�����������������������!���0,����++-&��,-/,.01-�����������������������!���0,����++-&��,-/,.01-�����������������������!���0,����----

----2222---- ++-!������-����-�����������������������0,++-!������-����-�����������������������0,++-!������-����-�����������������������0,++-!������-����-�����������������������0,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-�����������������������!���0-�����������������������!���0-�����������������������!���0-�����������������������!���0----

----4444---- 5555----

----6666---- ------------�� ���� ���� ���� ��----��������������������----������������-����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@--����>-������-����KL-@-----

----9999---- ---5---5---5---5----

----::::---- ------------------------++-�����������-���������++-�����������-���������++-�����������-���������++-�����������-�����������������������0- �����������������0- �����������������0- �����������������0- ���----

----////---- ------------------------�����������������������0-�������-A�����������������������0-�������-A�����������������������0-�������-A�����������������������0-�������-A--------------------------------------------------------

.<.<.<.<----------------------------------------��;��;��;��;-�����������������������0>-�����������������������0>-�����������������������0>-�����������������������0>--------------------------------------------------------------------

........----------------------------------------H� HH� HH� HH� H?-?-?-?-H"�;��HH"�;��HH"�;��HH"�;��H?-?-?-?-H333H333H333H333====33333333====3333H3333H3333H3333H?-?-?-?-4<<<4<<<4<<<4<<<?-?-?-?-,<0,<0,<0,<0?-?-?-?-3<<3<<3<<3<<-@8-@8-@8-@8----

.2.2.2.2--------------------------------

.3.3.3.3----------------------------++-���- ���++-���- ���++-���- ���++-���- ���====�����������������������������-�������-�������-�����-�������-�������-�����-�������-�������-�����-�������-�������-����----

.0.0.0.0----------������,��,�������>------������,��,�������>------������,��,�������>------������,��,�������>----

.4.4.4.4----------------------------------------H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-H�������-��'������- ������- �-���-������1-JJJJ�H�H�H�H-@8-@8-@8-@8----

.6.6.6.6----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H&����-����-��HH&����-����-��HH&����-����-��HH&����-����-��H????----

.9.9.9.9----------------------------------------�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@�������,���&����7���>@-@8-@8-@8-@8----

.:.:.:.:----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HHHHI�-I�I�-I�I�-I�I�-I�JJJJ�H�H�H�H?-?-?-?-H"���-����-��HH"���-����-��HH"���-����-��HH"���-����-��H?-?-?-?-----

././././----------------------------------------�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@�������,���"���7���>@-@8-@8-@8-@8----

2<2<2<2<----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�H�H�H�H?-?-?-?-H����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��HH����-�������-��� ��-��H?-?-?-?-----

2.2.2.2.----------------------------------------�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@�������,��������������7�� ��>@-@8-@8-@8-@8----

22222222----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-�����-��HH����-�����-��HH����-�����-��HH����-�����-��H?-?-?-?-----

23232323----------------------------------------�������,������������>@�������,������������>@�������,������������>@�������,������������>@-@8-@8-@8-@8----

20202020----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H��������-����-��HH��������-����-��HH��������-����-��HH��������-����-��H????----

24242424----------------------------------------�������,���������������>@�������,���������������>@�������,���������������>@�������,���������������>@-@8-@8-@8-@8----

26262626----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HI�-I,2'HI�-I,2'HI�-I,2'HI�-I,2'JJJJ�H�H�H�H?-?-?-?-H����-������-��HH����-������-��HH����-������-��HH����-������-��H????----

29292929----------------------------------------�������,�������������>@�������,�������������>@�������,�������������>@�������,�������������>@-@8-@8-@8-@8----

2:2:2:2:--------

Create �����������������������0

object.

Use inherited get methods to access inherited �������instance variables

Use �����������������������0 getmethod to access ������� instance variable.

63

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

Outline

�����������������������������������������������������������������������!���0,�����������!���0,�����������!���0,�����������!���0,����

(2 of 2)

2/2/2/2/----------------------------�������,�������������>-�������,�������������>-�������,�������������>-�������,�������������>-.<<<.<<<.<<<.<<<-@8--@8--@8--@8-++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

3<3<3<3<--------------------------------

3.3.3.3.----------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-------������,��,�����'>-HHHHJJJJ�I�1�I�1�I�1�I�1JJJJ����JJJJ�I��I��I��I�JJJJ�H�H�H�H?-?-?-?-----

32323232----------------------------------------H ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������HH ������-�������-��'������- ������- �-�������H????----

33333333----------------------------------------�������,�������>@�������,�������>@�������,�������>@�������,�������>@-@8-@8-@8-@8----

30303030-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

34343434----B-B-B-B-++++++++-���-����-�����������������������!���0-���-����-�����������������������!���0-���-����-�����������������������!���0-���-����-�����������������������!���0----

----�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1-�������-��'������- ������- �-���-������1---------&����-����-��-� &����-����-��-� &����-����-��-� &����-����-��-� ----"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��"���-����-��-"�;��----����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333����-�������-��� ��-��-333====33333333====3333333333333333----����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<����-�����-��-4<<<,<<----��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0��������-����-��-<,<0----����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<����-������-��-3<<,<<-------- ������-�������-� ������-�������-� ������-�������-� ������-�������-��'������- ������- �-�������1�'������- ������- �-�������1�'������- ������- �-�������1�'������- ������- �-�������1-------- ��� ��� ��� ���====��������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;����������-�������-�������1-� -"�;��----����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333====33333333====3333333333333333----����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<----�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0---- ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<< ���-������1-.<<<,<<--------

Use �����������������������0 setmethod to modify ������� instance variable ���������.

64

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

9.5 Constructors in Subclasses

• Instantiating subclass object– Chain of constructor calls

• subclass constructor invokes superclass constructor– Implicitly or explicitly

• Base of inheritance hierarchy– Last constructor called in chain is � ���� ���� ���� ���’s constructor– Original subclass constructor’s body finishes executing last– Example: ���������������3���������������3���������������3���������������3====

�����������������������0�����������������������0�����������������������0�����������������������0 hierarchy* ���������������3���������������3���������������3���������������3 constructor called second last

(last is � ���-� ���-� ���-� ���-constructor)* ���������������3���������������3���������������3���������������3 constructor’s body finishes

execution second (first is � ���� ���� ���� ��� constructor’s body)

65

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

Software Engineering Observation 9.8

When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor (explicitly, via ��������������������, or implicitly). The superclass constructor’s body executes to initialize the superclass’s instance variables that are part of the subclass object, then the subclass constructor’s body executes to initialize the subclass-only instance variables.(cont…)

66

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

Software Engineering Observation 9.8

Java ensures that even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value (e.g., <<<< for primitive numeric types, '����'����'����'���� for ���� ���� ���� ����s, ���������������� for references).

Page 12: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

12

67

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

Outline

������������������������������������������������������������0,����0,����0,����0,����

(1 of 4)

Lines 23-24

----....---- ++-&��,-/,.41-���������������0,����++-&��,-/,.41-���������������0,����++-&��,-/,.41-���������������0,����++-&��,-/,.41-���������������0,����----

----2222---- ++-���������������0-����-����������-�-�������-�������,++-���������������0-����-����������-�-�������-�������,++-���������������0-����-����������-�-�������-�������,++-���������������0-����-����������-�-�������-�������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-���������������0-���������������0-���������������0-���������������0----

----4444---- 5555----

----6666---- ------------����������������������������-������-'����7���8-������-'����7���8-������-'����7���8-������-'����7���8----

----9999---- ------------����������������������������-������-����7���8-������-����7���8-������-����7���8-������-����7���8----

----::::---- ------------����������������������������-������-���������-������-���������-������-���������-������-�����������7�� ��8��7�� ��8��7�� ��8��7�� ��8----

----////---- ------------����������������������������----�� ���� ���� ���� ��-���������8--���������8--���������8--���������8-++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����++-����-;��(��-�����----

.<.<.<.<----------------����������������������������----�� ���� ���� ���� ��-�����������8--�����������8--�����������8--�����������8-++-�������-���������++-�������-���������++-�������-���������++-�������-���������----

........--------

.2.2.2.2----------------++-'���++-'���++-'���++-'���====��������-���������������-���������������-���������������-�������----

.3.3.3.3----------------�� ���� ���� ���� ��-���������������0>-������-'����?-������-����?-������-���?--���������������0>-������-'����?-������-����?-������-���?--���������������0>-������-'����?-������-����?-������-���?--���������������0>-������-'����?-������-����?-������-���?-----

.0.0.0.0----------------------------�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

.4.4.4.4-------5---5---5---5----

.6.6.6.6----------------------------++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����++-�������-���-�-� ���-�������-���-����----

.9.9.9.9----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

.:.:.:.:----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

././././----------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8------�����������7�� ��-A-���8----

2<2<2<2<----------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-------������������>-�����-@8-++-��������-���-���++-��������-���-���++-��������-���-���++-��������-���-����-����-������-����-������-����-������-����-�����----

2.2.2.2.----------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-------���������������>-����-@8-++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����++-��������-���-����-�������-����----

22222222--------

23232323----------------------------������,��,�����'>������,��,�����'>������,��,�����'>������,��,�����'>----------------------------------------------------------------------------------------------------------------------------------------------------

20202020----------------------------------------HHHHJJJJ����������������0-�������1����������������0-�������1����������������0-�������1����������������0-�������1JJJJ�I��I��I��I�JJJJ�H�H�H�H?-?-?-?-����������������-@8-@8-@8-@8----

24242424-------B----B----B----B-++-���-'���++-���-'���++-���-'���++-���-'���====��������������������������-���������������0-���������-���������������0-���������-���������������0-���������-���������������0-�������----

26262626--------

Constructor outputs message to demonstrate method call order.

68

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

Outline

������������������������������������������������������������0,����0,����0,����0,����

(2 of 4)

29292929----------------++-���-'����-����++-���-'����-����++-���-'����-����++-���-'����-����----

2:2:2:2:----------------�� ���� ���� ���� ��----������������-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@-���&����7���>-������-'����-@----

2/2/2/2/-------5---5---5---5----

3<3<3<3<----------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8------'����7���-A-'����8----

3.3.3.3.-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

32323232--------

33333333----------------++-������-'����-����++-������-'����-����++-������-'����-����++-������-'����-����----

30303030----------------�� ���� ���� ���� ��-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@-������-���&����7���>@----

34343434-------5---5---5---5----

36363636----------------------------������������������������-'����-'����-'����-'����7���87���87���87���8----

39393939-------B----B----B----B-++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���++-���-�����-���&����7���----

3:3:3:3:--------

3/3/3/3/----------------++-���-����-����++-���-����-����++-���-����-����++-���-����-����----

0<0<0<0<----------------�� ���� ���� ���� ��----������������-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@-���"���7���>-������-����-@----

0.0.0.0.-------5---5---5---5----

02020202----------����7���-A-����8------����7���-A-����8------����7���-A-����8------����7���-A-����8----

03030303-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

00000000--------

04040404----------------++-������-����-����++-������-����-����++-������-����-����++-������-����-����----

06060606----------------�� ���� ���� ���� ��-������-���"�-������-���"�-������-���"�-������-���"���7���>@��7���>@��7���>@��7���>@----

09090909-------5---5---5---5----

0:0:0:0:----------------------------������������������������-����7���8-����7���8-����7���8-����7���8----

0/0/0/0/-------B----B----B----B-++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���++-���-�����-���"���7���----

4<4<4<4<--------

4.4.4.4.----------------++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��++-���-����-�������-��� ��----

42424242----------------�� ���� ���� ���� ��----������������-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@-��������������7�� ��>-������-���-@----

43434343-------5---5---5---5----

40404040----------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-------�����������7�� ��-A-���8-++-�����-��������++-�����-��������++-�����-��������++-�����-��������----

44444444-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

46464646--------

69

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

Outline

������������������������������������������������������������0,����0,����0,����0,����

(3 of 4)

49494949----------------++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��++-������-����-�������-��� ��----

4:4:4:4:----------------�� ���� ���� ���� ��-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@-������-��������������7�� ��>@----

4/4/4/4/-------5---5---5---5----

6<6<6<6<----------------------------������������������������-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8-�����������7�� ��8----

6.6.6.6.-------B----B----B----B-++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��++-���-�����-��������������7�� ��----

62626262--------

63636363----------------++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����++-���-����-�����-�����----

60606060----------------�� ���� ���� ���� ��----������������-���������-���������-���������-������������>-���>-���>-���>-�� ���� ���� ���� ��-�����-@-�����-@-�����-@-�����-@----

64646464-------5---5---5---5----

66666666----------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-------���������-A->-�����-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-�����8-1-�����8-1-�����8-1-�����8----

69696969-------B----B----B----B-++-���-�����-������������++-���-�����-������������++-���-�����-������������++-���-�����-������������----

6:6:6:6:--------

6/6/6/6/----------------++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����++-������-����-�����-�����----

9<9<9<9<----------------�� ���� ���� ���� ��----�� ���� ���� ���� ��-������������>@-������������>@-������������>@-������������>@----

9.9.9.9.-------5---5---5---5----

92929292----------------------------������������������������-���������8-���������8-���������8-���������8----

93939393-------B----B----B----B-++++++++-���-�����-������������-���-�����-������������-���-�����-������������-���-�����-������������----

90909090--------------------

94949494----------------++-���-�������-����++-���-�������-����++-���-�������-����++-���-�������-����----

96969696----------------�� ���� ���� ���� ��----������������-���������������>--���������������>--���������������>--���������������>-�� ���� ���� ���� ��-����-@-����-@-����-@-����-@----

99999999-------5---5---5---5----

9:9:9:9:----------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-------�����������-A->-����-E-<,<<,<<,<<,<-FF-����-C--FF-����-C--FF-����-C--FF-����-C-.,<.,<.,<.,<-@-D-����-1--@-D-����-1--@-D-����-1--@-D-����-1-<,<<,<<,<<,<8888----

9/9/9/9/-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:<:<:<:<--------

70

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

Outline

������������������������������������������������������������0,����0,����0,����0,����

(4 of 4)

:.:.:.:.----------------++-������-�������-����++-������-�������-����++-������-�������-����++-������-�������-����----

:2:2:2:2----------------�� ��-�� ���� ��-�� ���� ��-�� ���� ��-�� ��-���������������>@-���������������>@-���������������>@-���������������>@----

:3:3:3:3-------5---5---5---5----

:0:0:0:0----------------------------������������������������-�����������8-�����������8-�����������8-�����������8----

:4:4:4:4-------B----B----B----B-++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������++-���-�����-���������������----

:6:6:6:6--------

:9:9:9:9----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

::::::::----------------�� ��-�� ���� ��-�� ���� ��-�� ���� ��-�� ��-��������>@-��������>@-��������>@-��������>@----

:/:/:/:/-------5---5---5---5----

/</</</<----------------------------������������������������-���������������>@-G-������������>@8-���������������>@-G-������������>@8-���������������>@-G-������������>@8-���������������>@-G-������������>@8----

/./././.-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

/2/2/2/2--------

/3/3/3/3----------------++-������-������-�������������-'-���������������0- ���++-������-������-�������������-'-���������������0- ���++-������-������-�������������-'-���������������0- ���++-������-������-�������������-'-���������������0- ���----

/0/0/0/0----------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

/4/4/4/4-------5---5---5---5----

/6/6/6/6----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�HI�1-I�-I�JJJJ�I�1-I��I�1-I��I�1-I��I�1-I�JJJJ�I�1-I,2'�I�1-I,2'�I�1-I,2'�I�1-I,2'JJJJ����I�1-I,2'HI�1-I,2'HI�1-I,2'HI�1-I,2'H?-?-?-?-----

/9/9/9/9----------------------------------------H�������-�������HH�������-�������HH�������-�������HH�������-�������H?-���&����7���>@?-���"���7���>@?-?-���&����7���>@?-���"���7���>@?-?-���&����7���>@?-���"���7���>@?-?-���&����7���>@?-���"���7���>@?-----

/:/:/:/:----------------------------------------H����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��HH����-�������-��� ��H?-��������������7�� ��>@?-?-��������������7�� ��>@?-?-��������������7�� ��>@?-?-��������������7�� ��>@?-----

////////----------------------------------------H����-�����HH����-�����HH����-�����HH����-�����H?-������������>@?-?-������������>@?-?-������������>@?-?-������������>@?-----

.<<.<<.<<.<<---- ----------------------------H�������-����HH�������-����HH�������-����HH�������-����H?-���������������>@-@8?-���������������>@-@8?-���������������>@-@8?-���������������>@-@8----

.<..<..<..<.---- ---B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

.<2.<2.<2.<2---- B-B-B-B-++-���-����-���������������0++-���-����-���������������0++-���-����-���������������0++-���-����-���������������0----

71

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

Outline

�����������������������������������������������������������������������4,�����������4,�����������4,�����������4,����

(1 of 2)

Lines 15-16

----....---- ++-&��,-/,.61-�����������������������4,����++-&��,-/,.61-�����������������������4,����++-&��,-/,.61-�����������������������4,����++-&��,-/,.61-�����������������������4,����----

----2222---- ++-�����������������������4-����-���������,++-�����������������������4-����-���������,++-�����������������������4-����-���������,++-�����������������������4-����-���������,----

----3333---- ----

----0000---- �� ���� ���� ���� ��----����������������-�����������������������4--�����������������������4--�����������������������4--�����������������������4-����������������������������-���������������0-���������������0-���������������0-���������������0----

----4444---- 5555----

----6666---- ------------�������-�� ���������-�� ���������-�� ���������-�� ��- ���������8-- ���������8-- ���������8-- ���������8-++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(++- ���-������-���-;��(----

----9999---- ----

----::::---- ------------++-���++-���++-���++-���====��������-���������������-���������������-���������������-�������----

----////---- ------------�� ���� ���� ���� ��-�����������������������4>-������-'����?-������-����?--�����������������������4>-������-'����?-������-����?--�����������������������4>-������-'����?-������-����?--�����������������������4>-������-'����?-������-����?-----

.<.<.<.<----------������-���?-------������-���?-------������-���?-------������-���?-�� ���� ���� ���� ��-�����?--�����?--�����?--�����?-�� ���� ���� ���� ��-����?--����?--����?--����?-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

........-------5---5---5---5----

.2.2.2.2----------------------------��������������������>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8>-'����?-����?-���?-�����?-����-@8----

.3.3.3.3----------�������������>-��------�������������>-��------�������������>-��------�������������>-������-@8-����-@8-����-@8-����-@8-++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������++-��������-���-����- ���-������----

.0.0.0.0--------------------

.4.4.4.4----------------------------������,��,�����'>������,��,�����'>������,��,�����'>������,��,�����'>------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

.6.6.6.6----------------------------------------HHHHJJJJ������������������������4-�������1������������������������4-�������1������������������������4-�������1������������������������4-�������1JJJJ�I��I��I��I�JJJJ�H�H�H�H?-?-?-?-����������������-@8-@8-@8-@8----

.9.9.9.9-------B----B----B----B-++-���-���++-���-���++-���-���++-���-���====��������-�����������������������4��������-�����������������������4��������-�����������������������4��������-�����������������������4-�������-�������-�������-�������----

.:.:.:.:--------------------

././././----------------++-���- ���-������++-���- ���-������++-���- ���-������++-���- ���-������----

2<2<2<2<----------------�� ��-����� ��-����� ��-����� ��-���-�������������>--�������������>--�������������>--�������������>-�� ���� ���� ���� ��-������-@-������-@-������-@-������-@----

2.2.2.2.-------5---5---5---5----

22222222---------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C------- ���������-A->-������-C-<,<<,<<,<<,<-@-D--@-D--@-D--@-D-<,<<,<<,<<,<-1-������8-1-������8-1-������8-1-������8----

23232323-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

20202020--------

Constructor outputs message to demonstrate method call order.

72

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

Outline

�����������������������������������������������������������������������4,�����������4,�����������4,�����������4,����

(2 of 2)

24242424----------------++-������- ���-������++-������- ���-������++-������- ���-������++-������- ���-������----

26262626----------------�� ��-�� ���� ��-�� ���� ��-�� ���� ��-�� ��-�������������>@-�������������>@-�������������>@-�������������>@----

29292929-------5---5---5---5----

2:2:2:2:----------------------------������������������������- ���������8- ���������8- ���������8- ���������8----

2/2/2/2/-------B----B----B----B-++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������++-���-�����-�������������----

3<3<3<3<--------

3.3.3.3.----------------++-�������-��������++-�������-��������++-�������-��������++-�������-��������----

32323232----------------�� ��-�� ���� ��-�� ���� ��-�� ���� ��-�� ��-��������>@-��������>@-��������>@-��������>@----

33333333-------5---5---5---5----

30303030----------------------------������������������������-�������������>-�������������>-�������������>-�������������>@-N-@-N-@-N-@-N-��������������������,��������>@8,��������>@8,��������>@8,��������>@8----

34343434-------B----B----B----B-++-���-�����-��������++-���-�����-��������++-���-�����-��������++-���-�����-��������----

36363636--------

39393939----------------++-������-������-�������������-'-�����������������������4++-������-������-�������������-'-�����������������������4++-������-������-�������������-'-�����������������������4++-������-������-�������������-'-�����������������������4----

3:3:3:3:----------------�� ���� ���� ���� ��-������-�������>@-������-�������>@-������-�������>@-������-�������>@----

3/3/3/3/-------5---5---5---5----

0<0<0<0<----------------------------������������������������-������,'����>--������,'����>--������,'����>--������,'����>-HI�-I�HI�-I�HI�-I�HI�-I�JJJJ�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H�I�1-I,2'H?-?-?-?-H ���H ���H ���H ���====��������H��������H��������H��������H?-?-?-?-----

0.0.0.0.----------------------------------------��������������������,�������>@?-,�������>@?-,�������>@?-,�������>@?-H ���-������HH ���-������HH ���-������HH ���-������H?-�������������>@-@8?-�������������>@-@8?-�������������>@-@8?-�������������>@-@8----

02020202-------B----B----B----B-++-���-�����-�������++-���-�����-�������++-���-�����-�������++-���-�����-�������----

03030303----B-B-B-B-++-���-����-�����������������������4++-���-����-�����������������������4++-���-����-�����������������������4++-���-����-�����������������������4----

Page 13: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

13

73

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

Outline

��������!�����������!�����������!�����������!���

,����,����,����,����

(1 of 2)

Lines 8-9

Lines 12-19

----....---- ++-&��,-/,.91-��������!���,����++-&��,-/,.91-��������!���,����++-&��,-/,.91-��������!���,����++-&��,-/,.91-��������!���,����----

----2222---- ++-Q������-����-��-;���-���������-���-�� ����-��������-���-�����,++-Q������-����-��-;���-���������-���-�� ����-��������-���-�����,++-Q������-����-��-;���-���������-���-�� ����-��������-���-�����,++-Q������-����-��-;���-���������-���-�� ����-��������-���-�����,----

----3333---- ----

----0000---- �� ��-������ ��-������ ��-������ ��-����-��������!���--��������!���--��������!���--��������!���-----

----4444---- 5555----

----6666---- ------------�� ���� ���� ���� ��----�����-��������-��������-��������-���-����>-������-����KL-@-����>-������-����KL-@-����>-������-����KL-@-����>-������-����KL-@----

----9999---- ---5---5---5---5----

----::::---- ------------------------���������������0-���������������������0-���������������������0-���������������������0-�������.-A-�.-A-�.-A-�.-A-��;��;��;��;-���������������0>-���������������0>-���������������0>-���������������0>----------------------------------------

----////---- ------------------------------------H� HH� HH� HH� H?-?-?-?-H"�;��HH"�;��HH"�;��HH"�;��H?-?-?-?-H333H333H333H333====33333333====3333H3333H3333H3333H?-?-?-?-4<<<4<<<4<<<4<<<?-?-?-?-,<0,<0,<0,<0-@8-@8-@8-@8--------------------------------------------------------------------------------

.<.<.<.<--------------------------------

........----------������,��,�������>@8------������,��,�������>@8------������,��,�������>@8------������,��,�������>@8----

.2.2.2.2----------------------------�����������������������4-�������2-A�����������������������4-�������2-A�����������������������4-�������2-A�����������������������4-�������2-A----------------------------------------------------------------------------

.3.3.3.3----------------------------------------��;��;��;��;-���������-���������-���������-�����������������������4>��������������4>��������������4>��������������4>--------------------------------------------------------------------------------------------

.0.0.0.0----------------------------------------H"���HH"���HH"���HH"���H?-?-?-?-HM���HHM���HHM���HHM���H?-?-?-?-H444H444H444H444====44444444====4444H4444H4444H4444H?-?-?-?-2<<<2<<<2<<<2<<<?-?-?-?-,<6,<6,<6,<6?-?-?-?-:<<:<<:<<:<<-@8-@8-@8-@8------------------------

.4.4.4.4--------------------------------

.6.6.6.6----------������,��,�������>@8------������,��,�������>@8------������,��,�������>@8------������,��,�������>@8----

.9.9.9.9----------------------------�����������������������4-�������3-A�����������������������4-�������3-A�����������������������4-�������3-A�����������������������4-�������3-A----------------------------------------------------------------------------

.:.:.:.:----------------------------------------��;��;��;��;-������������-������������-������������-�����������������������4>�����������4>�����������4>�����������4>--------------------------------------------------------------------------------------------

././././----------------------------------------H%��(HH%��(HH%��(HH%��(H?-?-?-?-H�����HH�����HH�����HH�����H?-?-?-?-H:::H:::H:::H:::====::::::::====::::H::::H::::H::::H?-?-?-?-:<<<:<<<:<<<:<<<?-?-?-?-,.4,.4,.4,.4?-?-?-?-2<<<2<<<2<<<2<<<-@8-@8-@8-@8--------------------

2<2<2<2<-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

2.2.2.2.----B-B-B-B-++-���-����-��������!���++-���-����-��������!���++-���-����-��������!���++-���-����-��������!���----

Instantiate two �����������������������4objects to demonstrate order of subclass and superclass constructor method calls.

Instantiate ���������������0 object

74

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

Outline

��������!�����������!�����������!�����������!���

,����,����,����,����

(2 of 2)

----���������������0-�������1���������������0-�������1���������������0-�������1���������������0-�������1----�������-�������1-� -"�;���������-�������1-� -"�;���������-�������1-� -"�;���������-�������1-� -"�;��----����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333����-�������-��� ��1-333====33333333====3333333333333333----����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<����-�����1-4<<<,<<----�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0�������-����1-<,<0--------���������������0-�������1���������������0-�������1���������������0-�������1���������������0-�������1---- ��� ��� ��� ���====��������-�������-�������1-"���-M�����������-�������-�������1-"���-M�����������-�������-�������1-"���-M�����������-�������-�������1-"���-M���----����-�������-��� ��1-444����-�������-��� ��1-444����-�������-��� ��1-444����-�������-��� ��1-444====44444444====4444444444444444----����-�����1-2<<<,<<����-�����1-2<<<,<<����-�����1-2<<<,<<����-�����1-2<<<,<<----�������-����1-<,<6�������-����1-<,<6�������-����1-<,<6�������-����1-<,<6---- ���-������1-<,<< ���-������1-<,<< ���-������1-<,<< ���-������1-<,<<--------�����������������������4-�������1�����������������������4-�������1�����������������������4-�������1�����������������������4-�������1---- ��� ��� ��� ���====��������-�������-�������1-"���-M�����������-�������-�������1-"���-M�����������-�������-�������1-"���-M�����������-�������-�������1-"���-M���----����-�������-��� ��1-444����-�������-��� ��1-444����-�������-��� ��1-444����-�������-��� ��1-444====44444444====4444444444444444----����-�����1-2<<<,<<����-�����1-2<<<,<<����-�����1-2<<<,<<����-�����1-2<<<,<<----�������-����1-<,<6�������-����1-<,<6�������-����1-<,<6�������-����1-<,<6---- ���-������1-:<<,<< ���-������1-:<<,<< ���-������1-:<<,<< ���-������1-:<<,<<------------������������������0-�������1��������������0-�������1��������������0-�������1��������������0-�������1---- ��� ��� ��� ���====��������-�������-�������1-%��(-�������������-�������-�������1-%��(-�������������-�������-�������1-%��(-�������������-�������-�������1-%��(-�����----����-�������-��� ��1-:::����-�������-��� ��1-:::����-�������-��� ��1-:::����-�������-��� ��1-:::====::::::::====::::::::::::::::----����-�����1-:<<<,<<����-�����1-:<<<,<<����-�����1-:<<<,<<����-�����1-:<<<,<<----�������-����1-<,.4�������-����1-<,.4�������-����1-<,.4�������-����1-<,.4---- ���-������1-<,<< ���-������1-<,<< ���-������1-<,<< ���-������1-<,<<--------�����������������������4-�������1�����������������������4-�������1�����������������������4-�������1�����������������������4-�������1---- ��� ��� ��� ���====��������-�������-�������1-%��(-��������-�������-�������1-%��(-��������-�������-�������1-%��(-��������-�������-�������1-%��(-��������������������----����-�������-��� ��1-:::����-�������-��� ��1-:::����-�������-��� ��1-:::����-�������-��� ��1-:::====::::::::====::::::::::::::::----����-�����1-:<<<,<<����-�����1-:<<<,<<����-�����1-:<<<,<<����-�����1-:<<<,<<----�������-����1-<,.4�������-����1-<,.4�������-����1-<,.4�������-����1-<,.4---- ���-������1-2<<<,<< ���-������1-2<<<,<< ���-������1-2<<<,<< ���-������1-2<<<,<<--------

Subclass �����������������������4constructor body executes after superclass���������������0’s constructor finishes execution.

75

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

9.6 Software Engineering with Inheritance

• Customizing existing software– Inherit from existing classes

• Include additional members• Redefine superclass members• No direct access to superclass’s source code

– Link to object code– Independent software vendors (ISVs)

• Develop proprietary code for sale/license– Available in object-code format

• Users derive new classes – Without accessing ISV proprietary source code

76

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

Software Engineering Observation 9.9

Despite the fact that inheriting from a class does not require access to the class’s source code, developers often insist on seeing the source code to understand how the class is implemented. Developers in industry want to ensure that they are extending a solid class—for example, a class that performs well and is implemented securely.

77

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

Software Engineering Observation 9.10

At the design stage in an object-oriented system, the designer often finds that certain classes are closely related. The designer should “factor out”common instance variables and methods and place them in a superclass. Then the designer should use inheritance to develop subclasses, specializing them with capabilities beyond those inherited from the superclass.

78

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

Software Engineering Observation 9.11

Declaring a subclass does not affect its superclass’s source code. Inheritance preserves the integrity of the superclass.

Page 14: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

14

79

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

Software Engineering Observation 9.12

Just as designers of non-object-oriented systems should avoid method proliferation, designers of object-oriented systems should avoid class proliferation. Such proliferation creates management problems and can hinder software reusability, because in a huge class library it becomes difficult for a client to locate the most appropriate classes. The alternative is to create fewer classes that provide more substantial functionality, but such classes might prove cumbersome.

80

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

Performance Tip 9.1

If subclasses are larger than they need to be (i.e., contain too much functionality), memory and processing resources might be wasted. Extend the superclass that contains the functionality that is closest to what is needed.

81

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

9.7 � ���� ���� ���� ��� Class

• Class � ���� ���� ���� ��� methods– ������������

– �R�����R�����R�����R����

– '�����S�'�����S�'�����S�'�����S�

– ��������������������������������

– ����������������������������

– ���'����'����'����'�, ���'�)�����'�)�����'�)�����'�)��, ;���;���;���;���

– ����������������������������

82

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

Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes.(Part 1 of 4)

Method Description

����������������---- This ���������������������������� method, which takes no arguments and returns an � ���� ���� ���� ��� reference, makes a copy of the object on which it is called. When cloning is required for objects of a class, the class should override method ������������ as a �� ���� ���� ���� �� method and should implement interface ����� ������� ������� ������� �� (package ����,��������,��������,��������,����). The default implementation of this method performs a so-called shallow copy—instance variable values in one object are copied into another object of the same type. For reference types, only the references are copied. A typical overridden ������������ method’s implementation would perform a deep copy that creates a new object for each reference type instance variable. There are many subtleties to overriding method ������������. You can learn more about cloning in the following article:

����,���,�+��������+MQ�!��!���+2<<.+��<3<6,��������,���,�+��������+MQ�!��!���+2<<.+��<3<6,��������,���,�+��������+MQ�!��!���+2<<.+��<3<6,��������,���,�+��������+MQ�!��!���+2<<.+��<3<6,����----

83

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

Fig. 9.18 | � ���� ���� ���� ��� methods that are inherited directly or indirectly by all classes.(Part 2 of 4)

Method Description

�R�����R�����R�����R����---- This method compares two objects for equality and returns ���������������� if they are equal and '����'����'����'���� otherwise. The method takes any � ���� ���� ���� ��� as an argument. When objects of a particular class must be compared for equality, the class should override method �R�����R�����R�����R���� to compare the contents of the two objects. The method’s implementation should meet the following requirements:

• It should return '����'����'����'���� if the argument is ����������������.

• It should return ���������������� if an object is compared to itself, as in � � � ���.,�R����>��.,�R����>��.,�R����>��.,�R����>---- ���. ���. ���. ���.----@@@@.

• It should return ���������������� only if both ���.,�R����> ���.,�R����> ���.,�R����> ���.,�R����>---- ���2 ���2 ���2 ���2----@@@@ and ���2,�R����> ���2,�R����> ���2,�R����> ���2,�R����>---- ���. ���. ���. ���.----@@@@ would return ����������������.

• For three objects, if ���.,�R����> ���.,�R����> ���.,�R����> ���.,�R����>---- ���2 ���2 ���2 ���2----@@@@ returns ���������������� and ���2,�R����> ���2,�R����> ���2,�R����> ���2,�R����>---- ���3 ���3 ���3 ���3----@@@@ returns ����������������, then ���., ���., ���., ���.,�R����>�R����>�R����>�R����>---- ���3 ���3 ���3 ���3----@@@@ should also return ����������������.

• If �R�����R�����R�����R���� is called multiple times with the two objects and the objects do not change, the method should consistently return ���������������� if the objects are equal and '����'����'����'���� otherwise.

A class that overrides �R�����R�����R�����R���� should also override ���������������������������� to ensure that equal objects have identical hashcodes. The default �R�����R�����R�����R���� implementation uses operator AAAAAAAA to determine whether two references refer to the same object in memory. Section 29.3.3 demonstrates class ������������������������’s �R�����R�����R�����R���� method and differentiates between comparing ������������������������ objects with AAAAAAAA and with �R�����R�����R�����R����.

84

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

Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes.(Part 3 of 4)

Method Description

'�����S�'�����S�'�����S�'�����S�---- This ���������������������������� method (introduced in Section 8.10 and Section 8.11) is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object’s memory. It is not guaranteed that the garbage collector will reclaim an object, so it cannot be guaranteed that the object’s '�����S�'�����S�'�����S�'�����S� method will execute. The method must specify an empty parameter list and must return ������������. The default implementation of this method serves as a placeholder that does nothing.

��������������������������������---- Every object in Java knows its own type at execution time. Method �������������������������������� (used in Section 10.5 and Section 21.3) returns an object of class �������������������� (package ����,��������,��������,��������,����) that contains information about the object’s type, such as its class name (returned by �������������������� method ���7������7������7������7���). You can learn more about class �������������������� in the online API documentation at ����,���,�+�2��+4,<+��+���+����+����+���������,���,�+�2��+4,<+��+���+����+����+���������,���,�+�2��+4,<+��+���+����+����+���������,���,�+�2��+4,<+��+���+����+����+�����,����,����,����,����.

Page 15: Object-Oriented Programming: Inheritance Save base ...kraemere/cse491/Notes/JavaHTP7e_09_6pp.pdf · Object-Oriented Programming: Inheritance 2 Ó 1992-2007 Pearson Education Inc

15

85

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

Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes.(Part 4 of 4)

Method Description

����������������������������---- A hashtable is a data structure (discussed in Section 19.10) that relates one object, called the key, to another object, called the value. When initially inserting a value into a hashtable, the key’s ���������������������������� method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key’s hashcode is also used by the hashtable to locate the key’s corresponding value.

���'�?-���'�?-���'�?-���'�?-----���'�)��?-���'�)��?-���'�)��?-���'�)��?-;���;���;���;���----

Methods ���'����'����'����'�, ������'�)����'�)����'�)����'�)�� and the three overloaded versions of ;���;���;���;��� are related to multithreading, which is discussed in Chapter 23. In J2SE 5.0, the multithreading model has changed substantially, but these features continue to be supported.

����������������������������---- This method (introduced in Section 9.4.1) returns a ������������������������ representation of an object. The default implementation of this method returns the package name and class name of the object’s class followed by a hexadecimal representation of the value returned by the object’s ���������������������������� method.

86

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

9.8 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels

• Labels– Display information and instructions– M"� ��M"� ��M"� ��M"� ��

• Display a single line of text• Display an image• Display both text and image

87

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

Outline

"� ��Q��,����"� ��Q��,����"� ��Q��,����"� ��Q��,����

(1 of 2)

Line 13

Line 16

Line 19

Line 25

----....---- ++-&��-/,./1-"� ��Q��,����++-&��-/,./1-"� ��Q��,����++-&��-/,./1-"� ��Q��,����++-&��-/,./1-"� ��Q��,����----

----2222---- ++-Q����������-���-���-'-�� ���,++-Q����������-���-���-'-�� ���,++-Q����������-���-���-'-�� ���,++-Q����������-���-���-'-�� ���,----

----3333---- ��������������������-����,�;�,�����"����8-����,�;�,�����"����8-����,�;�,�����"����8-����,�;�,�����"����8----

----0000---- ��������������������-�����,�;���,$����$�8-�����,�;���,$����$�8-�����,�;���,$����$�8-�����,�;���,$����$�8----

----4444---- ��������������������-�����,�;���,M"� ��8-�����,�;���,M"� ��8-�����,�;���,M"� ��8-�����,�;���,M"� ��8----

----6666---- ��������������������-�����,�;���,M&����8-�����,�;���,M&����8-�����,�;���,M&����8-�����,�;���,M&����8----

----9999---- ----

----::::---- �� ���� ���� ���� ��----����������������-"� ��Q��-"� ��Q��-"� ��Q��-"� ��Q��----

----////---- 5555----

.<.<.<.<----------------�� ���� ���� ���� ��----��������������������----������������-����>-������-����KL-@-����>-������-����KL-@-����>-������-����KL-@-����>-������-����KL-@----

........-------5---5---5---5----

.2.2.2.2----------------------------++-������-�-�� ��-;���-�����-����++-������-�-�� ��-;���-�����-����++-������-�-�� ��-;���-�����-����++-������-�-�� ��-;���-�����-����----

.3.3.3.3----------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-��;��;��;��;-M"� ��>--M"� ��>--M"� ��>--M"� ��>-H7���HH7���HH7���HH7���H-@8-@8-@8-@8----

.0.0.0.0--------------------------------

.4.4.4.4----------------------------++-�����-��-��-'��-��-�����-�-;�-��-���-��-�-�-M"� ��++-�����-��-��-'��-��-�����-�-;�-��-���-��-�-�-M"� ��++-�����-��-��-'��-��-�����-�-;�-��-���-��-�-�-M"� ��++-�����-��-��-'��-��-�����-�-;�-��-���-��-�-�-M"� ��----

.6.6.6.6----------$����$�-�� ��$�-A------$����$�-�� ��$�-A------$����$�-�� ��$�-A------$����$�-�� ��$�-A----��;��;��;��;-$����$�>--$����$�>--$����$�>--$����$�>-H� $���,��'HH� $���,��'HH� $���,��'HH� $���,��'H-@8-@8-@8-@8----

.9.9.9.9--------------------------------

.:.:.:.:----------------------------++-�����-�-�� ��-;���-��-$�-�������-'-����++-�����-�-�� ��-;���-��-$�-�������-'-����++-�����-�-�� ��-;���-��-$�-�������-'-����++-�����-�-�� ��-;���-��-$�-�������-'-����----

././././----------M"� ��-�����"� ��-A-------M"� ��-�����"� ��-A-------M"� ��-�����"� ��-A-------M"� ��-�����"� ��-A-��;��;��;��;-M"� ��>-�� ��$�-@8-M"� ��>-�� ��$�-@8-M"� ��>-�� ��$�-@8-M"� ��>-�� ��$�-@8----

2<2<2<2<--------------------------------

2.2.2.2.----------------------------++-�����-������-�� ��-;���-��-$�++-�����-������-�� ��-;���-��-$�++-�����-������-�� ��-;���-��-$�++-�����-������-�� ��-;���-��-$�----

22222222----------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-------M"� ��-����"� ��-A-��;��;��;��;-M"-M"-M"-M"� ��>-�� ��$�-@8� ��>-�� ��$�-@8� ��>-�� ��$�-@8� ��>-�� ��$�-@8----

23232323--------------------------------

20202020----------------------------++-���-���-�� ��-�-�������-����->��-;���-��-��-��@++-���-���-�� ��-�-�������-����->��-;���-��-��-��@++-���-���-�� ��-�-�������-����->��-;���-��-��-��@++-���-���-�� ��-�-�������-����->��-;���-��-��-��@----

24242424----------����"� ��,���!���>-------����"� ��,���!���>-------����"� ��,���!���>-------����"� ��,���!���>-H����HH����HH����HH����H-@8-@8-@8-@8----

26262626--------------------------------

Change the text the ����"� �� displays

$����$� constructor argument specifies the path to the image

Declare and initialize �����"� �� with a M"� ��that displays the �� ��$�

Create a M"� �� that displays the string “North”

88

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

Outline

"� ��Q��,����"� ��Q��,����"� ��Q��,����"� ��Q��,����

(2 of 2)

Lines 34-36

29292929----------------------------++-�����-�-'����-�-���-���-�� ���++-�����-�-'����-�-���-���-�� ���++-�����-�-'����-�-���-���-�� ���++-�����-�-'����-�-���-���-�� ���----

2:2:2:2:----------M&����-���������-A-------M&����-���������-A-------M&����-���������-A-------M&����-���������-A-��;��;��;��;-M&����>@8-M&����>@8-M&����>@8-M&����>@8----

2/2/2/2/--------------------------------

3<3<3<3<----------���������,���Q�'����������������>-------���������,���Q�'����������������>-------���������,���Q�'����������������>-------���������,���Q�'����������������>-M&����,�T$!U�7U�"���M&����,�T$!U�7U�"���M&����,�T$!U�7U�"���M&����,�T$!U�7U�"���-@8-@8-@8-@8----

3.3.3.3.--------

32323232----------------------------++-���-���-�� ���-�-���-'����8-���-����-��������-����'���++-���-���-�� ���-�-���-'����8-���-����-��������-����'���++-���-���-�� ���-�-���-'����8-���-����-��������-����'���++-���-���-�� ���-�-���-'����8-���-����-��������-����'���----

33333333----------------------------++-;����-�-���-'����-�-���-���-�� ��-++-;����-�-���-'����-�-���-���-�� ��-++-;����-�-���-'����-�-���-���-�� ��-++-;����-�-���-'����-�-���-���-�� ��-----

30303030----------���������,���>-����"� ��?-------���������,���>-����"� ��?-------���������,���>-����"� ��?-------���������,���>-����"� ��?-�����"����,7��!#�����"����,7��!#�����"����,7��!#�����"����,7��!#-@8-@8-@8-@8----

34343434----------���������,���>-�����"� ��?-------���������,���>-�����"� ��?-------���������,���>-�����"� ��?-------���������,���>-�����"� ��?-�����"����,��7!�������"����,��7!�������"����,��7!�������"����,��7!��-@8-------@8-------@8-------@8----------

36363636----------���������,���>-����"� ��?-------���������,���>-����"� ��?-------���������,���>-����"� ��?-------���������,���>-����"� ��?-�����"����,�� !#�����"����,�� !#�����"����,�� !#�����"����,�� !#-@8-------@8-------@8-------@8----------

39393939--------

3:3:3:3:----------���������,�����S�>-------���������,�����S�>-------���������,�����S�>-------���������,�����S�>-3<<3<<3<<3<<?-?-?-?-3<<3<<3<<3<<-@8--@8--@8--@8-++-���-���-��S�-'-���-'����++-���-���-��S�-'-���-'����++-���-���-��S�-'-���-'����++-���-���-��S�-'-���-'����----

3/3/3/3/----------���������,������� ��>-------���������,������� ��>-------���������,������� ��>-------���������,������� ��>-����������������-@8--@8--@8--@8-++-��;-���-'����++-��;-���-'����++-��;-���-'����++-��;-���-'����----

0<0<0<0<-------B----B----B----B-++-���-����++-���-����++-���-����++-���-����----

0.0.0.0.----B-B-B-B-++-���-����-"� ��Q��++-���-����-"� ��Q��++-���-����-"� ��Q��++-���-����-"� ��Q��----

---- ----

Attach the labels to the M&����at north, center and south

89

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

Fig. 9.20 | M"� ��M"� ��M"� ��M"� �� displaying shape statistics.