chapter 9 inheritance and polymorphism highlight tip filechapter 9 inheritance and polymorphism /**...

26
Chapter 9 Inheritance and Polymorphism O bject-oriented programming is based on a paradigm in which objects are used to model a specification. Objects are created from classes, which provide encapsulation. Inheritance extends a class and provides a means of polymorphism. This chapter discusses inheritance and polymorphism. Abstract classes and interfaces are also discussed. Extending a Class Often times there is an existing class that provides a basis for an object that models a specification. However, the existing class may need addi- tional methods or different implementations of existing methods to more closely represent the object for the model. For example, consider a disk, which has circular shape. It is similar to a circle. However, a disk is three- dimensional and also has a thickness. Rather than create a whole new class to represent a disk, a class named Disk could extend the Circle class. Making one class an extension of another involves inheritance. Inheritance allows a class to define a specialized type of an already existing class. In this case, a disk is a solid circle with a thickness. Classes that are derived from existing classes demonstrate an is-a relationship. A class “is a” type of another class. In this case, a disk is a circle with a thickness. A class can have many levels of inheritance. For example, consider the following class hierarchy: The Puck class inherits the Disk class, which inherits the Circle class. The Circle class is the superclass of Disk. Disk is the subclass of Circle and the superclass of Puck. Puck is the subclass of Disk. Chapter 9 Inheritance and Polymorphism

Upload: others

Post on 06-Sep-2019

52 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects are created from classes, which provide encapsu lation. Inheritance extends a class and provides a means of polymorphism. This chapter d iscusses inheritance and polymorphism. Abstract classes and interfaces are also d iscussed.

Ex tending a Class Often times there is an existing class that provides a basis for an object that models a specification. However, the existing class may need add i-tional methods or d ifferent implementations of existing methods to more closely represent the object for the model. For example, consider a d isk, which has circu lar shape. It is sim ilar to a circle. However, a d isk is three-d imensional and also has a thickness. Rather than create a whole new class to represent a d isk, a class named Disk cou ld extend the Circle class.

Making one class an extension of another involves inheritance. Inheritance allows a class to define a specialized type of an already existing class. In th is case, a d isk is a solid circle w ith a th ickness. Classes that are derived from existing classes demonstrate an is-a relationship. A class “is a” type of another class. In th is case, a d isk is a circle w ith a th ickness.

A class can have many levels of inheritance. For example, consider the follow ing class h ierarchy:

����

������

����

The Puck class inherits the Disk class, which inherits the Circle class. The Circle class is the superclass of Disk. Disk is the subclass of Circle and the superclass of Puck. Puck is the subclass of Disk.

Chapter 9I nherit ance and Polymorphism

kimberlysmith
Highlight
kimberlysmith
Highlight
kimberlysmith
Highlight
kimberlysmith
Sticky Note
highlight tip
Page 2: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

I mplement ing a Subclass A class that inherits another class includes the keyword ext ends in the class declaration and takes the form:

publ i c cl as s <name> ext ends <cl as s _ name> { <cl as s def i ni t i on>}

Design ing a subclass requ ires selecting the superclass, or base class, and then defin ing any add itional variable and method members for the subclass. In many cases, existing methods in the base class w ill also be overridden by new definitions in the subclass, also called the derived class. For example, the Disk class design appears sim ilar to:

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

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

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

The Disk class implementation, based on the design above, is:

/ ** * Di s k cl as s . */publ i c cl as s Di s k ext ends Ci r cl e { pr i vat e doubl e t hi cknes s ; / ** * cons t r uct or * pr e: none * pos t : A Di s k obj ect has been cr eat ed wi t h r adi us r * and t hi ckness t . */ publ i c Di s k( doubl e r , doubl e t ) { s uper ( r ) ; t hi ckness = t ; }

/ ** * Changes t he t hi ckness of t he di s k. * pr e: none * pos t : Thi cknes s has been changed. */ publ i c voi d s et Thi ckness( doubl e newThi ckness ) { t hi ckness = newThi cknes s ; }

kimberlysmith
Highlight
Page 3: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Ret ur ns t he t hi cknes s of t he di s k. * pr e: none * pos t : The t hi ckness of t he di s k has been r et ur ned. */ publ i c doubl e get Thi cknes s( ) { r et ur n( t hi cknes s ) ; } / ** * Ret ur ns t he vol ume of t he di s k. * pr e: none * pos t : The vol ume of t he di s k has been r et ur ned. */ publ i c doubl e vol ume( ) { doubl e v; v = s uper . ar ea( ) * t hi cknes s ; r et ur n( v) ; }

/ ** * Det er mi nes i f t he obj ect i s equal t o anot her * Di s k obj ect . * pr e: d i s a Di s k obj ect . * pos t : t r ue has been r et ur ned i f obj ect s have t he s ame * r adi i and t hi ckness . f al s e has been r et ur ned ot her wi se. */ publ i c bool ean equal s( Obj ect d) { Di s k t es t Obj = ( Di s k) d; i f ( t es t Obj . get Radi us( ) == s uper . get Radi us( ) && t es t Obj . get Thi cknes s( ) == t hi cknes s ) { r et ur n( t r ue) ; } el s e { r et ur n( f al se) ; } }

/ ** * Ret ur ns a St r i ng t hat r epr esent s t he Di s k obj ect . * pr e: none * pos t : A s t r i ng r epr esent i ng t he Di s k obj ect has * been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { St r i ng di s kSt r i ng;

di s kSt r i ng = "The di s k has r adi us " + s uper . get Radi us( ) + " and t hi cknes s " + t hi cknes s + "."; r et ur n( di s kSt r i ng) ; }}

In a su bclass, the keyword s upe r is u sed to access m ethod s of the base class. For example, the statement s uper ( r ) calls the constructor of the superclass, Circle, and passes an argu ment for sett ing the rad iu s value. Members that are declared pr i vat e are not accessible to derived classes. Therefore, accessor method s are u sed to get inherited member variable values. For example, the equals() method in the Disk class calls getRad ius().

kimberlysmith
Highlight
kimberlysmith
Highlight
kimberlysmith
Sticky Note
highlight tip
Page 4: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Inherited methods are called d irectly from an object, just as any method of the class is called. Whether a method is original to the Disk class or inherited from the Circle class is transparent to client code, as demonstrated in the TestDisk application:

publ i c cl as s Tes t Di s k {

publ i c s t at i c voi d mai n( St r i ng[ ] ar gs ) { Di s k s aucer = new Di s k( 10, 0.02) ;

Sys t em. out . pr i nt l n( "Di s k r adi us : " + s aucer . get Radi us( )) ; Sys t em. out . pr i nt l n( "Di s k s ur f ace ar ea: " + s aucer . ar ea( )) ; Sys t em. out . pr i nt l n( "Di s k vol ume: " + s aucer .vol ume( )) ;

Di s k pl at e1 = new Di s k( 12, 0.05) ; Di s k pl at e2 = new Di s k( 12, 0.07) ; i f ( pl at e1. equal s( pl at e2)) { Sys t em. out . pr i nt l n( "Obj ect s ar e equal .") ; } el s e { Sys t em. out . pr i nt l n( "Obj ect s ar e not equal .") ; } Sys t em. out . pr i nt l n( pl at e1) ; Sys t em. out . pr i nt l n( pl at e2) ; }}

The TestDisk application d isplays the following output:

Review : Puck – par t 1 of 2Create a Puck class that inherits the Disk class. The Puck class shou ld include member variables wei ght , s t andar d, and yout h. The s t andar d and yout h variables shou ld be bool ean variables that are set to either t r ue or f al s e depend ing on the weight of the puck. A standard puck weighs between 5 and 5.5 ou nces. A youth puck weighs between 4 and 4.5 ounces. Official hockey pucks, regard less of weight, are one inch-thick with a three-inch d iameter. The Puck class shou ld also contain member methods getWeight(), getDivision(), which retu rns a string stating whether the puck is standard or youth, and equals() and toString(), which overrride the same methods in the Disk class. The Puck constructor shou ld requ ire an argument for weight. Be su re that the constructor in itializes other variables to appropriate values as necessary.

Create a Hockey application that tests the Puck class.

kimberlysmith
Highlight
Page 5: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Polymorphism Polymorphism is an OOP property in which objects have the ability to assume d ifferent types. In object-oriented program m ing, polymorphism is based on inheritance. Because a subclass is derived from a superclass, a superclass object can reference an object of the subclass. For example, the follow ing statements are valid because Disk inherits Circle:

Ci r cl e waf er ;Di s k cooki e = new Di s k( 2, 0. 5) ;waf er = cooki e; / / waf er now r ef er ences cooki e

The waf er object, declared a Circle, is polymorphic, as demonstrated in the statement waf er = cooki e where waf er assu mes the form of cooki e, a Disk object.

Polymorph ism is fu r ther demonstrated when the referenced object determ ines which method to execute. Th is is possible when a subclass overrides a superclass method. In th is case, the Disk class has overridden the equals() and toString() methods. Because of th is, the follow ing state-ment executes the Disk toString() method even though waf er was declared a Circle object:

/ * di s pl ays : The di s k has r adi us 2.0 and t hi cknes s 0. 5. */Sys t em. out . pr i nt l n( waf er ) ;

To fu r ther demonstrate p olymorph ism, the Mu sic applicat ion w ill be developed in th is section. The Mu sic application allows the u ser to assemble a small band. The user can assign a band member either vocals (voice) or a woodw ind instrument (piccolo or clarinet). The user can then select to hear either a solo, duet, or trio performance from th is band.

The Instru ment class, its subclasses, and the Perform ance class are used to model the objects for the Music application. The d iagram below illustrates the client code and classes for the Music application. Note the h ierarchy of the Instrument class and its subclasses:

����������

����������

�����������

�����

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

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

�����

�����������

kimberlysmith
Highlight
kimberlysmith
Highlight
Page 6: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The Music client code is show n below:

/ * * Mus i c.j ava */ i mpor t j ava. ut i l . Scanner ; publ i c cl as s Mus i c {

/ * Ret ur ns a s el ect ed i ns t r ument . * pr e: none * pos t : An i ns t r ument obj ect has been r et ur ned. */ publ i c s t at i c I ns t r ument as s i gnI ns t r ument ( ) { St r i ng i ns t r ument Choi ce; Scanner i nput = new Scanner ( Sys t em. i n) ; Sys t em. out . pr i nt l n( "Sel ect an i ns t r ument f or t he band member . ") ; Sys t em. out . pr i nt ( "Vocal s , Pi ccol o, or Cl ar i net : ") ; i ns t r ument Choi ce = i nput . next Li ne( ) ; Sys t em. out . pr i nt ( "Ent er t he band member' s name: ") ; name = i nput . next Li ne( ) ; i f ( i ns t r ument Choi ce. equal s I gnor eCase( "V")) { r et ur n( new Vocal ( name)) ; } el s e i f ( i ns t r ument Choi ce. equal s I gnor eCase( "P")) { r et ur n( new Pi ccol o( name)) ; } el s e { / / def aul t t o cl ar i net r et ur n( new Cl ar i net ( name)) ; } }

publ i c s t at i c voi d mai n( St r i ng[ ] ar gs ) { Per f or mance band; I ns t r ument bandMember 1, bandMember 2, bandMember 3; Scanner i nput = new Scanner ( Sys t em. i n) ; St r i ng per f or manceChoi ce; / * as s i gn i ns t r ument s */ bandMember 1 = as s i gnI ns t r ument ( ) ; bandMember 2 = as s i gnI ns t r ument ( ) ; bandMember 3 = as s i gnI ns t r ument ( ) ; Sys t em. out . pr i nt l n( bandMember 1 + " " + bandMember 2 + " " + bandMember 3 + " \ n") ; Sys t em. out . pr i nt ( "Woul d you l i ke t o hear a Sol o, a Duet , a Tr i o, or Leave? ") ; per f or manceChoi ce = i nput . next Li ne( ) ; whi l e ( ! per f or manceChoi ce. equal s I gnor eCase( "L")) { i f ( per f or manceChoi ce. equal s I gnor eCas e( "S")) { band = new Per f or mance( bandMember 1) ; } el s e i f ( per f or manceChoi ce. equal s I gnor eCas e( "D")) { band = new Per f or mance( bandMember 1, bandMember 2) ; } el s e { / / def aul t t o t r i o band = new Per f or mance( bandMember 1, bandMember 2, bandMember 3) ; } band. begi n( ) ; Sys t em. out . pr i nt ( " \ nWoul d you l i ke t o hear a Sol o, a Duet , a Tr i o, or Leave? ") ; ; per f or manceChoi ce = i nput . next Li ne( ) ; } }}

Page 7: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The assignInstrument() method declares an Instrument retu rn type, but the ind ividual r et ur n statements retu rn Vocal, Piccola, and Clarinet types. The Instru ment object retu rned by the method is polymorphic, changing to whichever subclass is actually retu rned.

The Music application produces output sim ilar to:

The Music application allows the user numerous combinations for select-ing a band and hearing performances. The code for such an application wou ld be more complicated and less flexible w ithout the object-oriented principles of inheritance and polymorphism. Music is versatile because it takes advantage of inheritance and polymorphism.

The docu mentat ion for the In stru ment, Vocal, Woodw ind, Piccolo, and Clarinet classes is below. Note that the makeSou nd () method in the Instrument class is a method that must be implemented (written) in a sub-class. This is d iscussed fu rther in the next section. The code for the classes is also show n in the next section where abstract classes are d iscussed.

Class InstrumentConstructor/MethodsI ns t r ument ( St r i ng name) creates an in st ru ment object w ith mu sician

name.get Mus i ci an( ) retu rns a string that is the musician’s name.makeSound( ) an abstract method that shou ld retu rn a String

representing the instru ment’s sou nd.

kimberlysmith
Highlight
Page 8: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Class Vocal (inherits Instrument)

Constructor/MethodsVocal ( St r i ng name) creates a singer object w ith singer name.makeSound( ) retu rns the String LaLaLa.t oSt r i ng( ) retu rns a String that represents the singer.

Class Woodwind (inherits Instrument)

Constructor/MethodWoodwi nd( St r i ng name) creates a woodw ind in stru ment object w ith

musician name.makeSound( ) retu rns the String t oot .

Class Piccolo (inherits Woodwind)

Constructor/MethodsPi ccol o( St r i ng name) creates a piccoloist object w ith musician name.makeSound( ) retu rns the String peep.t oSt r i ng( ) retu rns a String that represents the object.

Class Clarinet (inherits Woodwind)

Constructor/MethodsCl ar i net ( St r i ng name) creates a clarinetist object w ith musician name.makeSound( ) retu rns the String s quawk.t oSt r i ng( ) retu rns a String that represents the object.

The Performance class creates an arrangement of Instru ment objects. The con structors requ ire In stru ment argu m ents, but p olymorph ism enables objects of Instructor subclasses to be passed :

/ ** * Per f or mance cl as s . */publ i c cl as s Per f or mance { pr i vat e St r i ng ar r angement ; pr i vat e I ns t r ument s ol o; pr i vat e I ns t r ument duet _ 1, duet _ 2; pr i vat e I ns t r ument t r i o _ 1, t r i o _ 2, t r i o _ 3;

/ ** * cons t r uct or * pr e: none * pos t : A s ol oi s t has been s el ect ed. */ publ i c Per f or mance( I ns t r ument s ) { s ol o = s ; ar r angement = s ol o. makeSound( ) ; }

kimberlysmith
Highlight
Page 9: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * cons t r uct or * pr e: none * pos t : The member s of a duet have been s el ect ed. */ publ i c Per f or mance( I ns t r ument d1, I ns t r ument d2) { duet _ 1 = d1; duet _ 2 = d2; ar r angement = duet _ 1. makeSound( ) + duet _ 2. makeSound( ) ; }

/ ** * cons t r uct or * pr e: none * pos t : The member s of a t r i o have been sel ect ed. */ publ i c Per f or mance( I ns t r ument t 1, I ns t r ument t 2, I ns t r ument t 3) { t r i o _ 1 = t 1; t r i o _ 2 = t 2; t r i o _ 3 = t 3; ar r angement = t r i o _ 1. makeSound( ) + t r i o _ 2. makeSound( ) + t r i o _ 3. makeSound( ) ; }

/ ** * Begi ns t he per f or mance. * pr e: none * pos t : The per f or mance has been pl ayed. */ publ i c voi d begi n( ) { Sys t em. out . pr i nt l n( ar r angement ) ; }

/ ** * Ret ur ns a St r i ng t hat r epr esent s t he per f or mer s . * pr e: none * pos t : A s t r i ng r epr esent i ng t he per f or mer s has * been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { St r i ng pr ogr am = "The per f or mance i ncl udes "; pr ogr am += ar r angement ; r et ur n( pr ogr am); }}

Review : Music – par t 1 of 2Modify the Music application to allow the user to select a quartet (fou r band members) in add ition to the other performances. Changes to the Performance class w ill also be requ ired to provide the option of creat-ing a quartet.

Page 10: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Abst ract Classes An abstract class models an abstract concept. For example, a mu sical instrument is an abstract concept. An instrument is something that can be played, but there is no such th ing an “instru ment” instrument. There are however, flutes, piccolos, d rums, and cymbals.

Abstract classes cannot be instantiated because they shou ld not repre-sent objects. They instead describe the more general details and actions of a type of object. For example, the Instru ment class describes the very basics of an instrument—it can make a sound. The Woodwind class is also an abtract class because it describes a group of instru ments. It includes a general sou nd that woodwind instruments make.

Abstract classes are declared w ith the keyword abs t r act in the class declaraction. They are intended to be inherited. The public members of the abstract class are visible to derived objects. However, an abstract class can also contain an abstract method. An abstract method is declared w ith the keyword abs t r act and contains a method declaration, but no body. The abstract class must be implemented in its subclass.

The Instrument class is an abstract class w ith an abstract method. The makeSou nd () method must be implemented in an Instrument subclass:

/ ** * I ns t r ument cl as s . */abs t r act cl as s I ns t r ument { St r i ng mus i ci an;

/ ** * cons t r uct or * pr e: none * pos t : A mus i ci an has been as s i gned t o t he i ns t r ument . */ publ i c I ns t r ument ( St r i ng name) { mus i ci an = name; } / ** * Ret ur ns t he name of t he mus i ci an * pr e: none * pos t : The name of t he mus i ci an pl ayi ng t he i ns t r ument * has been r et ur ned. */ publ i c St r i ng get Mus i ci an( ) { r et ur n( mus i ci an) ; }

/ ** * Shoul d r et ur n t he s ound of t he i ns t r ument . * pr e: none * pos t : The s ound made by t he i ns t r ument i s r et ur ned. */ abs t r act St r i ng makeSound( ) ;}

The Vocal class is a subclass of Instrument. It provides the body for the makeSou nd () method :

kimberlysmith
Highlight
Page 11: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Vocal cl as s . */publ i c cl as s Vocal ext ends I ns t r ument {

/ ** * cons t r uct or * pr e: none * pos t : A s i nger has been cr eat ed. */ publ i c Vocal ( St r i ng s i nger Name) { s uper ( s i nger Name) ; }

/ ** * Ret ur ns t he s ound of t he i ns t r ument . * pr e: none * pos t : The s ound made by t he s i nger . */ publ i c St r i ng makeSound( ) { r et ur n( "LaLaLa") ; }

/ ** * Ret ur ns a St r i ng t hat r epr es ent s t he i ns t r ument . * pr e: none * pos t : A s t r i ng r epr esent i ng t he s i nger . */ publ i c St r i ng t oSt r i ng( ) { r et ur n( s uper . get Mus i ci an( ) + " s i ngs " + makeSound( ) + ".") ; }}

The Woodw ind class is also an Instrument subclass. It too implements the m akeSou nd () method . However, Woodw ind describes a group of instru ments so it has also been declared abstract:

/ ** * Woodwi nd cl as s . */abs t r act cl as s Woodwi nd ext ends I ns t r ument { / ** * cons t r uct or * pr e: none * pos t : A woodwi nd i ns t r ument has been cr eat ed. */ publ i c Woodwi nd( St r i ng pl ayer ) { s uper ( pl ayer ) ; }

/ ** * Ret ur ns t he s ound of t he i ns t r ument . * pr e: none * pos t : The s ound made by t he i ns t r ument i s r et ur ned. */ publ i c St r i ng makeSound( ) { r et ur n( "t oot ") ; }}

Page 12: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The Piccolo class is a subclass of Woodwind. It overrides the makeSound() method:

/ ** * Pi ccol o cl as s . */publ i c cl as s Pi ccol o ext ends Woodwi nd { / ** * cons t r uct or * pr e: none * pos t : A pi ccol o has been cr eat ed. */ publ i c Pi ccol o( St r i ng pi ccol oi s t ) { s uper ( pi ccol oi s t ) ; }

/ ** * Ret ur ns t he s ound of t he i ns t r ument . * pr e: none * pos t : The s ound made by t he i ns t r ument i s r et ur ned. */ publ i c St r i ng makeSound( ) { r et ur n( "peep") ; }

/ ** * Ret ur ns a St r i ng t hat r epr es ent s t he i ns t r ument . * pr e: none * pos t : A s t r i ng r epr es ent i ng t he i ns t r ument has * been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { r et ur n( s uper . get Mus i ci an( ) + " pl ays " + makeSound( ) + ".") ; }}

The Clarinet class is also a Woodw ind subclass. It too overrides the makeSou nd () method :

/ ** * Cl ar i net cl as s . */publ i c cl as s Cl ar i net ext ends Woodwi nd { / ** * cons t r uct or * pr e: none * pos t : A cl ar i net has been cr eat ed. */ publ i c Cl ar i net ( St r i ng cl ar i net i s t ) { s uper ( cl ar i net i s t ) ; }

/ ** * Ret ur ns t he s ound of t he i ns t r ument . * pr e: none * pos t : The s ound made by t he i ns t r ument i s r et ur ned. */ publ i c St r i ng makeSound( ) { r et ur n( "s quawk") ; }

Page 13: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Ret ur ns a St r i ng t hat r epr es ent s t he i ns t r ument . * pr e: none * pos t : A s t r i ng r epr esent i ng t he i ns t r ument has * been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { r et ur n( s uper . get Mus i ci an( ) + " pl ays " + makeSound( ) + ".") ; }}

Through inheritance and abstraction, a hierarchy of classes can be cre-ated that begin w ith a general abstraction and lead to a specific object.

Review : Music – par t 2 of 2Mod ify the Music application to allow the user to select a cymbal or d rum in add ition to the other instru-ments for the band members. The Music application changes will requ ire that Percussion, Cymbal, and Drum classes be created. The Percussion class shou ld be an abstract class that inherits the Instru ment class. The Cymbal and Dru m classes shou ld inherit the Percussion class.

I nt er faces An interface is a class w ith method declarations that have no implemen-tations. Although an interface may seem sim ilar to an abstract class, it is very d ifferent. An interface cannot be inherited . It may on ly be imple-mented in a class. An interface can add behavior to a class, but it does not provide a h ierarchy for the class.

An interface takes the form:

<access _ l evel > i nt er f ace <name> { <r et ur n _ t ype> <met hod _ name> ( <met hod _ par am>);

…additional methods}

The method s defined in an interface are by defau lt public and abstract. Therefore, the methods in an interface are only declarations followed by a sem icolon.

The Comparable interface is part of the java.lang package. It contains one method:

Interface Comparable ( java.lang.Comparable)

Methodcompar eTo( Obj ect obj ) retu rns 0 when obj is the same as the object,

a negat ive in teger is retu rned w hen obj is less than the object, and a positive integer is retu rned when obj is greater than the object.

When an interface is implemented in a class, the class must implement each method defined in the interface. In this case, the Comparable interface contains ju st one method. The Circle class show n on the next page has been mod ified to implement the Comparable interface.

kimberlysmith
Highlight
kimberlysmith
Highlight
kimberlysmith
Highlight
Page 14: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Ci r cl e cl as s . */publ i c cl as s Ci r cl e i mpl ement s Compar abl e { pr i vat e s t at i c f i nal doubl e PI = 3. 14; pr i vat e doubl e r adi us ; / ** * cons t r uct or * pr e: none * pos t : A Ci r cl e obj ect cr eat ed. Radi us i ni t i al i zed t o 1. */ publ i c Ci r cl e( ) { r adi us = 1; / / def aul t r adi us }

…getRadius(), setRadius(), and other Circle class methods

/ ** * Det er mi nes i f obj ect c i s s mal l er , t he s ame, * or l ar ger t han t hi s Ci r cl e obj ect . * pr e: c i s a Ci r cl e obj ect * pos t : -1 has been r et ur ned i f c i s l ar ger t han * t hi s Ci r cl e, 0 has been r et ur ned i f t hey ar e t he * s ame s i ze, and 1 has been r et ur ned i f c i s s mal l er * t hen t hi s Ci r cl e. */ publ i c i nt compar eTo( Obj ect c) { Ci r cl e t es t Ci r cl e = ( Ci r cl e) c; i f ( r adi us < t es t Ci r cl e. get Radi us( )) { r et ur n( -1) ; } el s e i f ( r adi us == t es t Ci r cl e. get Radi us( )) { r et ur n( 0) ; } el s e { r et ur n( 1) ; } }

The TestCircle client code tests the compareTo() method :

/ ** * The Ci r cl e cl as s i s t es t ed. */ publ i c cl as s Tes t Ci r cl e {

publ i c s t at i c voi d mai n( St r i ng[ ] ar gs ) { Ci r cl e s pot 1 = new Ci r cl e( 3) ; Ci r cl e s pot 2 = new Ci r cl e( 4) ; i f ( s pot 1. compar eTo( s pot 2) == 0) { Sys t em. out . pr i nt l n( "Obj ect s ar e equal .") ; } el s e i f ( s pot 1. compar eTo( s pot 2) < 0) { Sys t em. out . pr i nt l n( "s pot 1 i s s mal l er t han s pot 2.") ; } el s e { Sys t em. out . pr i nt l n( "s pot 1 i s l ar ger t han s pot 2.") ; } Sys t em. out . pr i nt l n( s pot 1) ; Sys t em. out . pr i nt l n( s pot 2) ; }}

Page 15: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The TestCircle application produces the output:

A class can implement mu ltiple interfaces. When more than one inter-face is implemented, the interface names are separated by com mas in the class declaration.

Review : DiskModify the Disk class to implement the Comparable interface. Two d isks are equal when they have the same thickness and same rad ius. Mod ify the existing client code to test the new method.

Review : Puck – par t 2 of 2Mod ify the Puck class to implement the Comparable interface. Two pucks are the equal when they have the same weight. Mod ify the existing client code to test the new method.

Review : Rect angle – par t 4 of 4Mod ify the Rectangle class to implement the Comparable interface. Two rectangles are the equal when they have the same w idth and height. Mod ify the existing client code to test the new method.

Review : Rect angle – par t 4 of 5Create an interface named ComparableArea that contains one method named compareToArea(). This method shou ld retu rn 0 when the object has the same area as another object, –1 shou ld be retu rned when the object has an area less than another object, and 1 retu rned otherwise.

Mod ify the Rectangle class to implement the ComparableArea interface as well as the Comparable interface implemented in the previous review. Mod ify the existing client code to test the new method.

Chapter 9 Case St udy

In th is case study, a sales center application w ill be created. The sales center has three employees, which include a manager and two associates. The manager earns a salary and the associates are paid by the hour. The owner of the sales center wants a computer application to d isplay employee information and calcu late payroll.

kimberlysmith
Highlight
Page 16: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

SalesCenter Specification

The SalesCenter application stores information about three employees. There is one manager (Diego Martin, salary $55,000), and two associates (Kylie Walter earn ing $18.50 per hou r and Michael Rose earn ing $16.75 per hou r). SalesCenter shou ld be able to d isplay the name and title for a specified employee. Add itionally, the SalesCenter application shou ld calcu late and d isplay the pay for a specified employee based on the pay argument entered by the user. The pay argument should correlate to hours worked if the pay for an associate is to be calcu lated. The pay argument for a manager shou ld correlate to the number of weeks the manager is to be paid for.

The SalesCenter interface should provide a menu of options. Depending on the option selected, add itional input may be needed. The SalesCenter output sketch:

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

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

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

The SalesCenter algorithm:

1. Display a menu of options.

2. Prompt the user for a menu choice.

3. If the u ser has not selected to qu it, p rompt the u ser to sp ecify employee 1, 2, or 3.

4. Perform the action requested by the user.

5. Repeat steps 1 through 4 u ntil the user has selected the option to qu it.

Page 17: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

SalesCenter Code Design

The SalesCenter application can be modeled w ith objects for a m an-ager and two associates. The m anager and associate objects are both employee objects. Therefore, an Employee abstract class shou ld be used for subclasses Manager and Associate. The Employee class shou ld define an emplyee’s first and last name and include an abstract class for calcu-lating pay. A manager’s pay is based on a pay period specified in weeks. Associates are paid by the hour. The abs t r act pay() method in Employee w ill have d ifferent implementations in Manager and Associate.

The SalesCenter class designs are:

��������

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

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

- ��������X������%����Y��

����������Y����Y�����Y

��������������������Y���������������Y����Y������Y�������Y��������������������������������������Y����Y������Y���������������������������������������������������������������������������������������K�����������Y��������������������������������������������������Y������������������

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

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

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

Page 18: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Based on the algorithm and the class designs, the SalesCenter code design will include a loop. The pseudocode for the SalesCenter client code follows:

i mpor t j ava. ut i l . Scanner ;i mpor t j ava. t ext . Number For mat ; publ i c cl as s Sal es Cent er { payEmpl oyee( emp, payAr g) { Sys t em. out . pr i nt l n( emp); pay = emp. pay( payAr g) ; Sys t em. out . pr i nt l n( pay) ; }

publ i c s t at i c voi d mai n( St r i ng[ ] ar gs ) { Manager emp1 = new Manager ( "Di ego","Mar t i n", 55000) ; As s oci at e emp2 = new Ass oci at e( "Kyl i e", "Wal t er ", 18. 50) ; As s oci at e emp3 = new Ass oci at e( "Mi chael ", "Ros e", 16.75) ; Empl oyee emp = emp1; / / def aul t empl oyee choi ce

/ * di s pl ay menu of choi ces */ do { pr ompt us er f or empl oyee/ pay/ qui t get us er choi ce; i f ( not qui t ) { pr ompt user f or empl oyee number 1, 2, or 3 get empNum s wi t ch ( empNum) { case 1: emp = emp1; br eak; case 2: emp = emp2; br eak; case 3: emp = emp3; br eak; } i f ( choi ce == empl oyee) { di s pl ay empl oyee name and t i t l e; } el s e i f ( choi ce == pay) { pr ompt user f or hour s or pay per i od; payEmpl oyee( emp, payAr g) ; } } } whi l e ( not qui t ) ; }}

SalesCenter Implementation

The SalesCenter implementation involves creating four files. Three files are the classes and one file is the client code.

Page 19: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The Employee class is implemented below:

/ ** * Empl oyee cl as s . */abs t r act cl as s Empl oyee { St r i ng f i r s t Name, l as t Name;

/ ** * cons t r uct or * pr e: none * pos t : An empl oyee has been cr eat ed. */ publ i c Empl oyee( St r i ng f Name, St r i ng l Name) { f i r s t Name = f Name; l as t Name = l Name; } / ** * Ret ur ns t he empl oyee name. * pr e: none * pos t : The empl oyee name has been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { r et ur n( f i r s t Name + " " + l as t Name) ; } / ** * Ret ur ns t he empl oyee pay. * pr e: none * pos t : The empl oyee pay has been r et ur ned. */ abs t r act doubl e pay( doubl e per i od) ;}

The Manager class is implemented below:

/ ** * Manager cl as s . */cl as s Manager ext ends Empl oyee { doubl e year l ySal ar y;

/ ** * cons t r uct or * pr e: none * pos t : A manager has been cr eat ed. */ publ i c Manager ( St r i ng f Name, St r i ng l Name, doubl e s al ) { s uper ( f Name, l Name) ; year l ySal ar y = s al ; } / ** * Ret ur ns t he manager s al ar y. * pr e: none * pos t : The manager s al ar y has been r et ur ned. */ publ i c doubl e get Sal ar y( ) { r et ur n( year l ySal ar y) ; }

Page 20: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Ret ur ns t he manager pay f or a s peci f i ed per i od. * pr e: none * pos t : The manager pay f or t he s peci f i ed per i od * has been r et ur ned. */ publ i c doubl e pay( doubl e weeks ) { doubl e payEar ned; payEar ned = ( year l ySal ar y / 52) * weeks ; r et ur n( payEar ned) ; } / ** * Ret ur ns t he empl oyee name and t i t l e. * pr e: none * pos t : The empl oyee name and t i t l e has been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { r et ur n( s uper . t oSt r i ng( ) + ", manager ") ; }}

The Associate class is implemented below:

/ ** * Ass oci at e cl as s . */cl as s As soci at e ext ends Empl oyee { doubl e hour l yPayRat e;

/ ** * cons t r uct or * pr e: none * pos t : An as soci at e has been cr eat ed. */ publ i c Ass oci at e( St r i ng f Name, St r i ng l Name, doubl e r at e) { s uper ( f Name, l Name) ; hour l yPayRat e = r at e; } / ** * Ret ur ns t he as s oci at e pay r at e. * pr e: none * pos t : The as s oci at e pay r at e has been r et ur ned. */ publ i c doubl e get Rat e( ) { r et ur n( hour l yPayRat e) ; }

/ ** * Ret ur ns t he as s oci at e pay f or t he hour s wor ked. * pr e: none * pos t : The as s oci at e pay f or t he hour s wor ked * has been r et ur ned. */ publ i c doubl e pay( doubl e hour s ) { doubl e payEar ned; payEar ned = hour l yPayRat e * hour s ; r et ur n( payEar ned) ; }

Page 21: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

/ ** * Ret ur ns t he empl oyee name and t i t l e. * pr e: none * pos t : The empl oyee name and t i t l e has been r et ur ned. */ publ i c St r i ng t oSt r i ng( ) { r et ur n( s uper . t oSt r i ng( ) + ", as s oci at e") ; } }

The SalesCenter client code is implemented below:

i mpor t j ava. ut i l . Scanner ; i mpor t j ava. t ext . Number For mat ; publ i c cl as s Sal esCent er { / ** * Di s pl ays empl oyee name and pay. * pr e: none * pos t : Empl oyee name and pay has been di s pl ayed */ publ i c s t at i c voi d payEmpl oyee( Empl oyee emp, doubl e payAr g) { Number For mat money = Number For mat . get Cur r encyI ns t ance( ) ; doubl e pay; Sys t em. out . pr i nt l n( emp) ; pay = emp. pay( payAr g) ; Sys t em. out . pr i nt l n( money. f or mat ( pay)) ; }

publ i c s t at i c voi d mai n( St r i ng[ ] ar gs ) { Manager emp1 = new Manager ( "Di ego","Mar t i n", 55000) ; As s oci at e emp2 = new As s oci at e( "Kyl i e", "Wal t er ", 18. 50) ; As s oci at e emp3 = new As s oci at e( "Mi chael ", "Ros e", 16.75) ; Scanner i nput = new Scanner ( Sys t em. i n) ; St r i ng act i on; i nt empNum; doubl e payAr g; Empl oyee emp = emp1; / / s et t o def aul t emp1 do { Sys t em. out . pr i nt l n( " \ nEmpl oyee\ \ Pay\ \ Qui t ") ; Sys t em. out . pr i nt ( "Ent er choi ce: ") ; act i on = i nput . next ( ) ; i f ( ! act i on. equal s I gnor eCase( "Q")) { Sys t em. out . pr i nt ( "Ent er empl oyee number ( 1, 2, or 3) :") ; empNum = i nput . next I nt ( ) ; s wi t ch ( empNum) { cas e 1: emp = emp1; br eak; cas e 2: emp = emp2; br eak; cas e 3: emp = emp3; br eak; } i f ( act i on. equal s I gnor eCas e( "E")) { Sys t em. out . pr i nt l n( emp) ; } el s e i f ( act i on. equal s I gnor eCas e( "P")) { Sys t em. out . pr i nt ( "Ent er t he hour s f or as s oci at e or pay per i od f or manager : ") ; payAr g = i nput . next Doubl e( ) ; payEmpl oyee( emp, payAr g) ; } } } whi l e ( ! act i on. equal s I gnor eCas e( "Q")) ; }}

Page 22: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

The SalesCenter application generates output sim ilar to:

SalesCenter Testing and Debugging

Client code shou ld first be w ritten to test each class. Testing shou ld be done for the client code.

Review : SalesCenterModify the SalesCenter application to compensate associates when they have worked more than 40 hours. Associates shou ld be paid their hourly wage when 40 or fewer hours are worked. However, associates earn time and a half for hours over 40. For example, an associate paid $10 per hour w ill earn $300 for 30 hours of work. However, an associate working 42 hours w ill earn $400 + $30, or $430. The overtime pay is calcu lated as (hours over 40) * (1.5 * base hourly rate).

Chapter Summary

This chapter d iscussed inheritance and polymorphism, two key aspects of object-oriented program m ing. Inheritance allows classes to be derived from existing classes. By extend ing an existing class, there is less devel-opment and debugging necessary. A class derived from an existing class demonstrates an is-a relationship.

A superclass is also called a base class, and a subclass is called a derived class. The keyword ext ends is used to create a derived class from a base class. The keyword s uper is used to access members of a base class from the derived class.

Polymorphism is the ability of an object to assume d ifferent types. In OOP, polymorphism is based on inheritance. An object can assu me the type of any of its subclasses.

Abstract classes model abstract concepts. They cannot be instantiated because they shou ld not represent objects. An abstract class is intended to be inherited.

kimberlysmith
Highlight
Page 23: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Abstract classes may or may not contain abstract methods. An abstract method is a method declaration w ith no implementation. If a class con-tains one or more abstract methods, it must be declared abstract. Abstract methods must be implemented in a class that inherits the abstract class.

An interface is a class that contains only abstract methods. An interface can be implemented by a class, but it is not inherited. A class that imple-ments an interface mu st implement each method in the interface. The Comparable interface is part of the java.lang package and is used to add a compareTo() method to classes that implement the interface.

kimberlysmith
Highlight
Page 24: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Vocabulary

Ab stract class A class that models an abstract concept. A class that contains one or more abstract methods must be declared abstract.

Abstract method A method that has been declared, but not implemented. Abstract methods appear in abstract classes. They are implemented in a subclass that inherits the abstract class.

Base class A superclass.

Derived class A subclass.

In heritance The OOP property in which a class can define a specialized type of an already exist-ing class.

Interface A class w ith abstract methods. An inter-face cannot be inherited, but it can be implemented by any number of classes.

Is-a relation sh ip The relationship demonstrated by a class derived from an existing class.

Polymorph ism The OOP property in which objects have the ability to assume d ifferent types.

Java

abs t r act The keyword used for declaring a class or a method as abstract.

Com p a rab le A java.la n g in ter face w it h t h e compareTo() method that can be implemented in classes to provide a means for an object to be com-pared to another object of the same type.

ext ends The keyword used in a class declaration to inherit another class.

i nt er f ace The keyword used in a class declaration to declare it an interface.

s uper The keyword used to call a superclass con-structor or method.

Page 25: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Crit ical Thinking

1. Explain the d ifference between a has-a and is-a relationship among classes.

2. If a base class has a public method go() and a derived class has a public method stop (), which method s w ill be available to an object of the derived class?

3. Compare and contrast implementing an abstract method to overrid ing a method.

4. Compare and contrast an abstract class to an interface.

5. List the method (s) contained in the Comparable interface.

6. Use the following classes to answer the questions below:

i nt er f ace Wo { publ i c i nt doThat ( ) ; }

publ i c cl as s Bo { pr i vat e i nt x;

publ i c Bo( i nt z) { x = z; }

publ i c i nt doThi s( ) { r et ur n( 2) ; }

publ i c i nt doNow( ) { r et ur n( 15) ; } }

publ i c cl as s Roo ext ends Bo i mpl ement s Wo {

publ i c Roo { s uper ( 1) ; }

publ i c i nt doThi s( ) { r et ur n( 10) ; }

pr i vat e i nt doThat ( ) { r et ur n( 20) ; } }

a) What type of method is doThat() in Wo? b) What is Wo? c) Why is doThat() implemented in Roo? d) List the methods available to a Roo object.

e) How does the implementation of doThis() in Roo affect the implementation of doThis() in Bo?

f) What action does the statement s uper ( 1) in Roo perform?

g) Can the doTh is() method in Bo be called from a Roo object? If so, how?

h) Ca n a m ethod in Roo ca ll t h e d oTh is () method in Bo? If so, how?

True/ False

7. Determ ine if each of the follow ing are true or false. If false, explain why.

a) Inheritance allows a class to define a special-ized type of an already existing class.

b) Classes that are derived from existing classes demonstrate a has-a relationship.

c) A c l a s s ca n h a v e o n ly o n e le v e l o f inheritance.

d) A class that inherits another class includes t h e key w ord i n h e r i t a n c e in t h e cla ss declaration.

e) When implem ent ing a su bclass, exist ing m e t h o d s i n t h e b a s e c l a s s c a n b e overridden.

f) Mem bers of a base class that are declared private are accessible to derived classes.

g) Inherited methods are called d irectly from an object.

h) Polymorphism is an OOP property in which objects have the ability to assu me d ifferent types.

i) Abstract classes can be instantiated. j) An abstract class must be implemented in its

subclass. k) An abst ract m ethod con ta in s a m ethod

declaration and a body. l) Inheritance and abstraction allow a hierarchy

of classes to be created. m) An interface can be inherited. n) An interface can add behavior to a class. o) The methods defined in an interface are pri-

vate by defau lt. p) The Comparable interface contain s th ree

methods.

Page 26: Chapter 9 Inheritance and Polymorphism highlight tip fileChapter 9 Inheritance and Polymorphism /** * Ret ur ns t he t hi ckness of t he di sk. * pre: none * post: The t hickness of

Chapter 9 Inheritance and Polymorphism

Exercises

Exercise 1 —————————————— UEmploye e , Faculty, StaffCreate a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class shou ld contain member methods for retu rn ing the employee name and salary.Create Facu lty and Staff classes that inherit the UEmployee class. The Facu lty class shou ld include members for storing and retu rn ing the department name. The Staff class shou ld include members for storing and retu rn ing the job title.

Exercise 2 ———————— Account, Pe rsonalAcct, Busine ssAcctCreate PersonalAcct and BusinessAcct classes that inherit the Accou nt class presented in Chapter 8. A personal accou nt requ ires a m inimum balance of $100. If the balance falls below th is amou nt, then $2.00 is charged (w ithd raw n) to the accou nt. A business accou nt requ ires a m inimum balance of $500, otherw ise the accou nt is charged $10. Create client code to test the classes.

Exercise 3 ———————————— Ve hicle , Car, Truck, MinivanCreate a Vehicle class that is an abstract class defin ing the general details and actions associated w ith a vehicle. Create Car, Truck, and Minivan classes that inherit the Vehicle class. The Car, Truck, and Minivan classes shou ld include add itional members specific to the type of vehicle being represented. Create client code to test the classes.