Chapter 09 - Inheritance and Interfaces

Chapter 09 - Inheritance and Interfaces - &KDSWHU...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU ,QKHULWDQFH DQG ,QWHUIDFHV &RS\ULJKW ,QKHULWDQFH &RQFHSWV 1RQ SURJUDPPLQJ +LHUDUFKLHV 3URJUDPPLQJ ZLWK ,QKHULWDQFH 2YHUULGLQJ 0HWKRGV 3RO\PRUSKLVP 7KH 0HFKDQLFV RI 3RO\PRUSKLVP 'LDJUDPPLQJ 3RO\PRUSKLF &RGH ,QWHUDFWLQJ ZLWK WKH 6XSHUFODVV ,QKHULWHG )LHOGV &DOOLQJ D 6XSHUFODVV V &RQVWUXFWRU &DOOLQJ 2YHUULGGHQ 0HWKRGV $ /DUJHU ([DPSOH 3RLQW ' E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS ,QKHULWDQFH LQ WKH -DYD &ODVV /LEUDULHV *UDSKLFV ' RSWLRQDO ,QSXW 2XWSXW 6WUHDPV ,QWHUIDFHV $Q ,QWHUIDFH IRU 6KDSH &ODVVHV ,PSOHPHQWLQJ WKH 6KDSH ,QWHUIDFH %HQHILWV RI ,QWHUIDFHV ,QWHUIDFHV LQ WKH -DYD &ODVV /LEUDULHV &DVH 6WXG\ 'HVLJQLQJ D +LHUDUFK\ RI )LQDQFLDO &ODVVHV &ODVV 'HVLJQ ,QLWLDO 5HGXQGDQW ,PSOHPHQWDWLRQ $EVWUDFW &ODVVHV ,QWURGXFWLRQ ,Q WKLV FKDSWHU ZH ZLOO H[SORUH WZR RI WKH PRVW LPSRUWDQW WHFKQLTXHV SURYLGHG E\ WKH -DYD ODQJXDJH WR ZULWH EHWWHU VWUXFWXUHG VROXWLRQV ,QKHULWDQFH DOORZV XV WR VKDUH FRGH EHWZHHQ FODVVHV WR UHGXFH UHGXQGDQF\ DV ZHOO DV OHWWLQJ XV WUHDW GLIIHUHQW W\SHV RI REMHFWV LQ WKH VDPH ZD\ ,QWHUIDFHV DOORZ XV WR WUHDW VHYHUDO GLIIHUHQW W\SHV RI REMHFWV WKH VDPH ZD\ ZLWKRXW VKDULQJ FRGH 477 ,QKHULWDQFH &RQFHSWV $V ZH EHJLQ WR OHDUQ DERXW LQKHULWDQFH ZH OO ILUVW ORRN DW WKH JHQHVLV RI KRZ LQKHULWDQFH FDPH DERXW DV ZHOO DV VRPH QRQ SURJUDPPLQJ H[DPSOHV RI WKH LGHDV EHKLQG LQKHULWDQFH 7KLV ZLOO OHDG XV WRZDUG SURJUDPPLQJ ZLWK LQKHULWDQFH LQ -DYD 'LG <RX .QRZ 7KH 6RIWZDUH &ULVLV 6RIWZDUH KDV EHHQ JHWWLQJ PRUH DQG PRUH FRPSOLFDWHG VLQFH WKH DGYHQW RI SURJUDPPLQJ $URXQG WKH EHJLQQLQJ RI WKH V LW ZDV EHFRPLQJ FOHDU WKDW VRPH FRPPRQ SUREOHPV H[LVWHG ZKHQ WHDPV ZURWH VXFK ODUJHU DQG PRUH FRPSOH[ SURJUDPV 3URMHFWV ZHUH UXQQLQJ RYHU EXGJHW WKH\ ZHUH QRW ILQLVKLQJ RQ WLPH WKH VRIWZDUH KDG EXJV GLGQ W GR ZKDW LW ZDV VXSSRVHG WR GR RU ZDV RWKHUZLVH RI ORZ TXDOLW\ &ROOHFWLYHO\ WKHVH SUREOHPV FDPH WR EH FDOOHG WKH VRIWZDUH FULVLV $ SDUWLFXODUO\ VWLFN\ LVVXH ZDV ZLWK SURJUDP PDLQWHQDQFH &RPSDQLHV IRXQG WKDW WKH\ VSHQW PXFK RI WKHLU WLPH QRW ZULWLQJ QHZ SURJUDPV EXW PRGLI\LQJ DQG PDLQWDLQLQJ H[LVWLQJ RQHV 7KLV SURYHG WR EH D GLIILFXOW WDVN EHFDXVH LW ZDV HDV\ WR ZULWH GLVRUJDQL]HG DQG UHGXQGDQW FRGH &KDQJHV SHUIRUPHG GXULQJ PDLQWHQDQFH ZRUN ZHUH OLNHO\ WR WDNH D ORQJ WLPH DQG WR LQWURGXFH QHZ EXJV LQWR WKH V\VWHP 7KH QHJDWLYH HIIHFWV RI WKH VRIWZDUH FULVLV DQG PDLQWHQDQFH SURJUDPPLQJ ZHUH SDUWLFXODUO\ QRWLFHDEOH ZKHQ JUDSKLFDO XVHU LQWHUIDFHV FDPH LQWR SURPLQHQFH LQ WKH V 8VHU LQWHUIDFHV LQ JUDSKLFDO V\VWHPV OLNH 0LFURVRIW :LQGRZV DQG $SSOH V 0DFLQWRVK ZHUH PXFK PRUH VRSKLVWLFDWHG WKDQ WKH WH[W LQWHUIDFHV WKDW SUHFHGHG WKHP 7KH RULJLQDO JUDSKLFDO SURJUDPV ZHUH SURQH WR UHGXQGDQF\ EHFDXVH WKH\ KDG WR GHVFULEH LQ GHWDLO KRZ EXWWRQV WH[W ER[HV DQG RWKHU RQVFUHHQ FRPSRQHQWV ZHUH LPSOHPHQWHG $OVR WKH JUDSKLFDO FRPSRQHQWV WKHPVHOYHV FRQWDLQ D ORW RI FRPPRQ VWDWH DQG EHKDYLRU VXFK DV D VL]H VKDSH FRORU SRVLWLRQ RU VFUROOEDU /DUJH SURJUDPV GHPDQG WKH DELOLW\ WR ZULWH YHUVDWLOH DQG FOHDU FRGH RQ D ODUJH VFDOH ,Q WKLV WH[WERRN VR IDU ZH YH VHHQ VHYHUDO ZD\V WR H[SUHVV SURJUDPV PRUH FRQFLVHO\ DQG HOHJDQWO\ RQ D VPDOO VFDOH )HDWXUHV OLNH VWDWLF PHWKRGV SDUDPHWHUL]DWLRQ ORRSV DQG FODVVHV KHOS XV RUJDQL]H RXU SURJUDPV DQG H[WUDFW FRPPRQ IHDWXUHV WKDW FDQ EH XVHG LQ PDQ\ SODFHV 7KLV JHQHUDO SUDFWLFH LV FDOOHG FRGH UHXVH &RGH 5HXVH 7KH SUDFWLFH RI ZULWLQJ SURJUDP FRGH RQFH DQG XVLQJ LW LQ PDQ\ FRQWH[WV 2EMHFW RULHQWHG SURJUDPPLQJ SURYLGHV XV ZLWK D IHDWXUH FDOOHG LQKHULWDQFH WKDW HQDEOHV XV WR UHXVH FRGH LQFOXGLQJ PDNLQJ HQWLUH FODVVHV UHXVDEOH LQ GLIIHUHQW FRQWH[WV ,QKHULWDQFH DOVR JLYHV XV WKH EHQHILW RI ZULWLQJ SURJUDPV ZLWK KLHUDUFKLHV RI UHODWHG REMHFW W\SHV 1RQ SURJUDPPLQJ +LHUDUFKLHV :KHQ ZH VWDUW WR WKLQN DERXW LQKHULWDQFH ZH OO ZDQW WR EH DEOH WR LGHQWLI\ VLPLODULWLHV EHWZHHQ GLIIHUHQW REMHFWV DQG FODVVHV LQ RXU SURJUDP /HW V VWDUW ZLWK D QRQ SURJUDPPLQJ H[DPSOH $ KLHUDUFK\ RI HPSOR\HHV DW D FRPSDQ\ 478 ,PDJLQH D ODUJH ODZ ILUP WKDW KLUHV VHYHUDO W\SHV RI HPSOR\HHV ODZ\HUV JHQHUDO VHFUHWDULHV OHJDO VHFUHWDULHV DQG PDUNHWHUV 7KH FRPSDQ\ KDV D EXQFK RI HPSOR\HH UXOHV DERXW YDFDWLRQ DQG VLFN GD\V PHGLFDO EHQHILWV KDUDVVPHQW UHJXODWLRQV DQG VR RQ (DFK VXEGLYLVLRQ RI WKH FRPSDQ\ DOVR KDV D IHZ RI LWV RZQ UXOHV IRU H[DPSOH PD\EH ODZ\HUV DVN IRU YDFDWLRQ OHDYH ZLWK D GLIIHUHQW IRUP WKDQ VHFUHWDULHV GR ,PDJLQH WKDW DOO WKH HPSOR\HHV DWWHQG D FRPPRQ RULHQWDWLRQ ZKHOH WKH\ OHDUQ WKH JHQHUDO UXOHV 7KH HPSOR\HH LV JLYHQ D SDJH PDQXDO RI WKHVH UXOHV WR UHDG $ PL[HG JURXS RI HPSOR\HHV FRXOG DWWHQG WKH RULHQWDWLRQ WRJHWKHU ODZ\HUV VHFUHWDULHV DQG PDUNHWHUV DOO PLJKW VLW LQ WKH VDPH RULHQWDWLRQ JURXS $IWHUZDUG WKH HPSOR\HH JRHV WR KLV KHU VXEGLYLVLRQ DQG UHFHLYHV D VHFRQG VPDOOHU RULHQWDWLRQ FRYHULQJ DQ\ UXOHV VSHFLILF WR WKDW GLYLVLRQ 7KH HPSOR\HH UHFHLYHV D VPDOOHU PDQXDO RU SDJHV LQ OHQJWK FRYHULQJ WKDW VXEGLYLVLRQ V VSHFLILF UXOHV 6RPH UXOHV DUH DGGHG WR WKRVH LQ WKH JHQHUDO SDJH PDQXDO DQG D IHZ DUH UHSODFHG )RU H[DPSOH SHUKDSV VHFUHWDULHV JHW ZHHNV RI YDFDWLRQ LQVWHDG RI SHUKDSV ODZ\HUV XVH D SLQN IRUP WR DSSO\ IRU WLPH RII QRW WKH \HOORZ IRUP OLVWHG LQ WKH SDJH PDQXDO (DFK VXEGLYLVLRQ KDV LWV RZQ VXEPDQXDO DQG WKHLU OHQJWKV DQG FRQWHQWV PD\ GLIIHU $Q DOWHUQDWLYH VROXWLRQ ZRXOG EH WR JLYH HYHU\ HPSOR\HH D ODUJH PDQXDO FRQWDLQLQJ ERWK WKH DSSOLFDEOH JHQHUDO UXOHV DQG WKH UXOHV RI WKHLU VXEGLYLVLRQ 7KHUH ZRXOG EH D SDJH PDQXDO IRU WKH ODZ\HUV D SDJH PDQXDO IRU VHFUHWDULHV DQG D SDJH PDQXDO IRU PDUNHWHUV ,Q IDFW WKH FRQVROLGDWLRQ PLJKW HYHQ VDYH D IHZ SDJHV 6R ZK\ GRHV WKH FRPSDQ\ ERWKHU ZLWK WZR PDQXDOV IRU HYHU\ HPSOR\HH" 7KH PDLQ LVVXHV DUH ZLWK UHGXQGDQF\ DQG PDLQWHQDQFH 7KH SDJH ODZ\HU PDQXDO FRQWDLQV D ORW RI WKH VDPH WH[W DV WKH SDJH VHFUHWDU\ PDQXDO ,I D FRPPRQ UXOH LV FKDQJHG DOO WKH PDQXDOV QHHG WR EH XSGDWHG LQGLYLGXDOO\ ZKLFK LV D WHGLRXV DQG HUURU SURQH SURFHVV 7KHUH V DOVR D FHUWDLQ YLVXDO DSSHDO WR WKH SDLU RI PDQXDOV ,I VRPHRQH ZDQWV WR NQRZ DOO UXOHV WKDW DUH VSHFLILF WR ODZ\HUV WKH\ FDQ VLPSO\ UHDG WKH SDJH ODZ\HU PDQXDO UDWKHU WKDQ FRPELQJ WKURXJK D SDJH ODZ\HU PDQXDO WU\LQJ WR VSRW GLIIHUHQFHV 479 7KHUH DUH WZR NH\ LGHDV KHUH ,W V XVHIXO WR EH DEOH WR VSHFLI\ D EURDG VHW RI UXOHV WKDW ZLOO DSSO\ WR PDQ\ UHODWHG JURXSV WKH SDJH PDQXDO ,W V DOVR XVHIXO IRU D SDUWLFXODU JURXS WR EH DEOH WR VSHFLI\ D VPDOOHU VHW RI UXOHV VSHFLILF WR LWVHOI LQFOXGLQJ WKH DELOLW\ WR UHSODFH VRPH UXOHV IURP WKH EURDG VHW 8VH WKH SLQN IRUP LQVWHDG RI WKH \HOORZ IRUP $Q LPSRUWDQW WKLQJ WR QRWLFH DERXW WKH FDWHJRULHV LV WKDW WKH\ QHVW ZLWKLQ HDFK RWKHU )RU H[DPSOH HYHU\ OHJDO VHFUHWDU\ LV DOVR D VHFUHWDU\ DQG HYHU\ PDUNHWHU LV DOVR DQ HPSOR\HH ,Q D SLQFK \RX FRXOG DVN D OHJDO VHFUHWDU\ WR ZRUN DV D VWDQGDUG VHFUHWDU\ IRU D VKRUW SHULRG EHFDXVH D OHJDO VHFUHWDU\ LV D VHFUHWDU\ :H FDOO VXFK D FRQQHFWLRQ DQ LV D UHODWLRQVKLS ,V D 5HODWLRQVKLS $ KLHUDUFKLFDO FRQQHFWLRQ EHWZHHQ WZR FDWHJRULHV ZKHUH RQH W\SH FDQ EH WUHDWHG DV SRVVLEO\ D VSHFLDOL]HG YHUVLRQ RI WKH RWKHU :KHQ DSSO\LQJ WKHVH FRQFHSWV WR SURJUDPPLQJ HDFK W\SH RI HPSOR\HH PLJKW EH UHSUHVHQWHG DV D FODVV 7KH GLIIHUHQW HPSOR\HH JURXSV UHSUHVHQW D VHW RI UHODWHG FODVVHV FRQQHFWHG E\ LV D UHODWLRQVKLSV :H FDOO VXFK D VHW RI FODVVHV DQ LQKHULWDQFH KLHUDUFK\ ,QKHULWDQFH +LHUDUFK\ $ VHW RI KLHUDUFKLFDO UHODWLRQVKLSV EHWZHHQ W\SHV RI REMHFWV $V ZH OO VHH LQKHULWDQFH KLHUDUFKLHV DUH FRPPRQO\ XVHG LQ -DYD WR JURXS UHODWHG W\SHV RI REMHFWV DQG UHXVH FRGH EHWZHHQ WKHP 3URJUDPPLQJ ZLWK ,QKHULWDQFH 7KH SUHYLRXV VHFWLRQ ZDV D QRQ SURJUDPPLQJ H[DPSOH RI KLHUDUFKLHV %XW DV DQ H[HUFLVH ZH FRXOG ZULWH VPDOO -DYD FODVVHV WR UHSUHVHQW WKRVH FDWHJRULHV RI HPSOR\HHV DV FODVVHV 7KH FRGH ZLOO EH D ELW VLOO\ EXW ZLOO LOOXVWUDWH VRPH LPSRUWDQW FRQFHSWV /HW V LPDJLQH WKDW ZH KDYH WKH IROORZLQJ UXOHV IRU RXU HPSOR\HHV $OO HPSOR\HHV ZRUN KRXUV SHU ZHHN $OO HPSOR\HHV PDNH VDODU\ SHU \HDU H[FHSW PDUNHWHUV ZKR PDNH SHU \HDU $OO HPSOR\HHV KDYH ZHHNV RI SDLG YDFDWLRQ OHDYH SHU \HDU H[FHSW ODZ\HUV ZKR KDYH ZHHNV RI YDFDWLRQ OHDYH $OO HPSOR\HHV XVH D \HOORZ IRUP WR DSSO\ IRU YDFDWLRQ OHDYH H[FHSW ODZ\HUV ZKR XVH D VSHFLDO SLQN IRUP (DFK HPSOR\HH W\SH KDV RQH XQLTXH DELOLW\ ODZ\HUV NQRZ KRZ WR VXH PDUNHWHUV NQRZ KRZ WR DGYHUWLVH DQG VHFUHWDULHV NQRZ KRZ WR WDNH GLFWDWLRQ 480 /HW V ZULWH D FODVV WR UHSUHVHQW WKH FRPPRQ EHKDYLRU RI DOO HPSOR\HHV 7KLQN RI WKLV DV WKH SDJH HPSOR\HH PDQXDO :H OO ZULWH PHWKRGV QDPHG VKRZ+RXUV VKRZ6DODU\ VKRZ9DFDWLRQ DQG DSSO\)RU9DFDWLRQ WR UHSUHVHQW WKHVH EHKDYLRUV 7R NHHS WKLQJV VLPSOH HDFK PHWKRG ZLOO MXVW SULQW D VKRUW UHOHYDQW 6WULQJ UHSUHVHQWLQJ WKH GHIDXOW HPSOR\HH EHKDYLRU VXFK DV VDODU\ DQG WKH \HOORZ IRUP IRU YDFDWLRQ OHDYH :H ZRQ W GHFODUH DQ\ GDWD ILHOGV IRU QRZ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // A class to represent employees in general. public class Employee { public void applyForVacation() { System.out.println("Use the yellow form to apply for vacation."); } public void showHours() { System.out.println("I work 40 hours per week."); } public void showSalary() { System.out.println("My salary is $40,000."); } public void showVacation() { System.out.println("I receive 2 weeks of paid vacation."); } } 1RZ OHW V WKLQN DERXW LPSOHPHQWLQJ WKH 6HFUHWDU\ VXEFDWHJRU\ 2QH WKLQJ ZH PHQWLRQHG LQ WKH SUHYLRXV VHFWLRQ ZDV WKDW HYHU\ 6HFUHWDU\ LV DOVR DQ (PSOR\HH DQG WKH\ VKRXOG UHWDLQ WKH DELOLWLHV WKDW (PSOR\HHV KDYH ,I ZH ZURWH 6HFUHWDU\ LQ D VWUDLJKWIRUZDUG ZD\ LWV FRGH SUREDEO\ ZRXOG QRW UHIOHFW WKLV UHODWLRQVKLS YHU\ HOHJDQWO\ 7KH 6HFUHWDU\ FODVV ZRXOG EH IRUFHG WR UHGXQGDQWO\ UHSHDW DOO RI WKH VDPH PHWKRGV IURP (PSOR\HH ZLWK LGHQWLFDO EHKDYLRU 7KH RQO\ FKDQJH WR 6HFUHWDU\ ZRXOG EH WR DGG D WDNH'LFWDWLRQ PHWKRG WR UHSUHVHQW WKH XQLTXH EHKDYLRU RI D 6HFUHWDU\ +HUH LV WKH UHGXQGDQW FODVV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // A redundant class to represent secretaries. public class Secretary { public void applyForVacation() { System.out.println("Use the yellow form to apply for vacation."); } public void showHours() { System.out.println("I work 40 hours per week."); } public void showSalary() { System.out.println("My salary is $40,000."); } public void showVacation() { System.out.println("I receive 2 weeks of paid vacation."); } // This is the only unique behavior. public void takeDictation() { System.out.println("I know how to take dictation."); } } 481 :KDW ZH G UHDOO\ OLNH WR EH DEOH WR GR LV VD\ WKH IROORZLQJ // This is pseudocode. public class Secretary { <copy all the methods from the Employee class.> // This is the only unique behavior. public void takeDictation() { System.out.println("I know how to take dictation."); } } )RUWXQDWHO\ -DYD SURYLGHV D PHFKDQLVP FDOOHG LQKHULWDQFH WKDW FDQ KHOS XV UHPRYH WKLV VRUW RI UHGXQGDQF\ EHWZHHQ VLPLODU FODVVHV RI REMHFWV ,QKHULWDQFH DOORZV WKH SURJUDPPHU WR VSHFLI\ D UHODWLRQVKLS EHWZHHQ WZR FODVVHV ZKHUH RQH FODVV DEVRUEV LQKHULWV WKH VWDWH DQG EHKDYLRU RI DQRWKHU ,QKHULWDQFH LQKHULW $ SURJUDPPLQJ WHFKQLTXH LQ ZKLFK D GHULYHG FODVV H[WHQGV WKH IXQFWLRQDOLW\ RI D EDVH FODVV LQKHULWLQJ DOO RI LWV VWDWH DQG EHKDYLRU 7KH FKLOG FODVV PRUH FRPPRQO\ FDOOHG WKH VXEFODVV LQKHULWV DOO RI WKH VWDWH DQG EHKDYLRU RI LWV SDUHQW W\SH FRPPRQO\ FDOOHG WKH VXSHUFODVV 7KH FKLOG FDQ WKHQ DGG H[LVWLQJ VWDWH DQG EHKDYLRU RI LWV RZQ RU LW FDQ UHSODFH LWV LQKHULWHG EHKDYLRU ZLWK QHZ EHKDYLRU DV QHHGHG 6XSHUFODVV 7KH SDUHQW FODVV LQ DQ LQKHULWDQFH UHODWLRQVKLS 6XEFODVV 7KH FKLOG FODVV LQ DQ LQKHULWDQFH UHODWLRQVKLS $ -DYD FODVV FDQ KDYH RQO\ RQH VXSHUFODVV LW LV QRW SRVVLEOH WR H[WHQG PRUH WKDQ RQH FODVV 7KLV LV FDOOHG VLQJOH LQKHULWDQFH 2QH FODVV PD\ KDYH PDQ\ VXEFODVVHV H[WHQGLQJ LW 7KH JHQHUDO V\QWD[ WR VSHFLI\ RQH FODVV DV WKH VXEFODVV RI DQRWKHU LV WKH IROORZLQJ public class <subclass name> extends <superclass name> { ... } :H FRXOG UHZULWH WKH 6HFUHWDU\ FODVV WR H[WHQG WKH (PSOR\HH FODVV 7KLV ZLOO FUHDWH DQ UHODWLRQVKLS ZKHUH HYHU\ 6HFUHWDU\ DOVR LV DQ (PSOR\HH 7KLV PHDQV WKDW 6HFUHWDU\ REMHFWV LQKHULW FRSLHV RI WKH DSSO\)RU9DFDWLRQ VKRZ+RXUV VKRZ6DODU\ DQG VKRZ9DFDWLRQ PHWKRGV ZRQ W QHHG WR ZULWH WKHVH PHWKRGV LQ WKH 6HFUHWDU\ FODVV DQ\ PRUH 7KLV ZLOO UHPRYH UHGXQGDQF\ EHFDXVH 6HFUHWDULHV SHUIRUP WKHVH DFWLRQ WKH VDPH ZD\ DV JHQHUDO (PSOR\HHV LV D ZLOO :H WKH 482 ,W V OHJDO DQG H[SHFWHG IRU D VXEFODVV WR DGG QHZ EHKDYLRU WKDW ZDVQ W SUHVHQW LQ WKH VXSHUFODVV :H VDLG SUHYLRXVO\ WKDW HPSOR\HHV DGG DQ DELOLW\ QRW VHHQ LQ RWKHU SHUVRQV WKH DELOLW\ WR ZRUN :H FDQ DGG WKLV WR RXU SUHYLRXVO\ HPSW\ (PSOR\HH FODVV 7KHUHIRUH WKH RQO\ FRGH ZH QHHG WR ZULWH LQ WKH (PSOR\HH FODVV LV WKH ZRUN PHWKRG 7KH IROORZLQJ LV WKH FRPSOHWH 6HFUHWDU\ FODVV 1 2 3 4 5 6 // A class to represent secretaries. public class Secretary extends Employee { public void takeDictation() { System.out.println("I know how to take dictation."); } } ,QKHULWDQFH FUHDWHV DQ LV D UHODWLRQVKLS EHFDXVH HDFK REMHFW RI WKH VXEFODVV FDQ EH WUHDWHG DV DQ REMHFW RI WKH VXSHUFODVV 7KH IROORZLQJ FOLHQW FRGH ZRXOG ZRUN ZLWK RXU QHZ 6HFUHWDU\ FODVV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class EmployeeMain { public static void main(String args) { System.out.println("Employee:"); Employee employee1 = new Employee(); employee1.applyForVacation(); employee1.showHours(); employee1.showSalary(); employee1.showVacation(); System.out.println(); System.out.println("Secretary:"); Secretary employee2 = new Secretary(); employee2.applyForVacation(); employee2.showHours(); employee2.showSalary(); employee2.showVacation(); employee2.takeDictation(); } } 7KH FRGH ZRXOG SURGXFH WKH IROORZLQJ RXWSXW 1RWLFH WKDW WKH ILUVW IRXU PHWKRGV SURGXFH WKH VDPH RXWSXW IRU ERWK REMHFWV EHFDXVH 6HFUHWDU\ LQKHULWV WKDW EHKDYLRU IURP (PSOR\HH Employee: Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 2 weeks of paid vacation. Secretary: Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 2 weeks of paid vacation. I know how to take dictation. 483 2YHUULGLQJ 0HWKRGV :H FDQ XVH LQKHULWDQFH LQ RXU RWKHU W\SHV RI (PSOR\HHV PDNLQJ /DZ\HU DQG 0DUNHWHU FODVVHV WKDW DUH VXEFODVVHV RI (PSOR\HH %XW ZKLOH WKH 6HFUHWDU\ RQO\ DGGHG EHKDYLRU WR WKH VWDQGDUG (PSOR\HH EHKDYLRU WKH /DZ\HU DQG 0DUNHWHU QHHG WR UHSODFH VRPH RI WKH VWDQGDUG (PSOR\HH EHKDYLRU ZLWK WKHLU RZQ /DZ\HUV UHFHLYH ZHHNV RI YDFDWLRQ DQG XVH D SLQN IRUP WR DSSO\ IRU YDFDWLRQ 0DUNHWHUV UHFHLYH VDODU\ ,W V OHJDO IRU XV WR UHSODFH VXSHUFODVV EHKDYLRU E\ ZULWLQJ D QHZ YHUVLRQ RI WKH UHOHYDQW PHWKRG V LQ RXU VXEFODVV 7KH QHZ YHUVLRQ ZLOO UHSODFH WKH RQH LQKHULWHG IURP (PSOR\HH 7KLV LGHD RI UHSODFLQJ EHKDYLRU IURP WKH VXSHUFODVV LV FDOOHG RYHUULGLQJ 2YHUULGH 7R LPSOHPHQW D QHZ YHUVLRQ RI D PHWKRG LQKHULWHG IURP D VXSHUFODVV UHSODFLQJ WKH VXSHUFODVV V YHUVLRQ 2YHUULGLQJ UHTXLUHV QR VSHFLDO V\QWD[ 7R RYHUULGH D PHWKRG MXVW ZULWH WKH PHWKRG \RX ZDQW WR UHSODFH LQ WKH VXEFODVV ,WV QDPH DQG VLJQDWXUH PXVW H[DFWO\ PDWFK WKDW RI WKH PHWKRG IURP WKH VXSHUFODVV +HUH DUH WKH /DZ\HU DQG 0DUNHWHU FODVVHV WKDW H[WHQGV (PSOR\HH DQG RYHUULGH WKH UHOHYDQW PHWKRGV 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 // A class to represent lawyers. public class Lawyer extends Employee { public void applyForVacation() { System.out.println("Use the pink form to apply for vacation."); } public void showVacation() { System.out.println("I receive 3 weeks of paid vacation."); } } // A class to represent marketers. public class Marketer extends Employee { public void showSalary() { System.out.println("My salary is $50,000."); } } 7KH IROORZLQJ FOLHQW SURJUDP XVHV RXU /DZ\HU DQG 0DUNHWHU FODVVHV 484 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class EmployeeMain2 { public static void main(String args) { System.out.println("Lawyer:"); Lawyer employee1 = new Lawyer(); employee1.applyForVacation(); employee1.showHours(); employee1.showSalary(); employee1.showVacation(); System.out.println(); System.out.println("Marketer:"); Marketer employee2 = new Marketer(); employee2.applyForVacation(); employee2.showHours(); employee2.showSalary(); employee2.showVacation(); } } 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW Lawyer: Use the pink form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 3 weeks of paid vacation. Marketer: Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $50,000. I receive 2 weeks of paid vacation. %H FDUHIXO QRW WR FRQIXVH RYHUULGLQJ ZLWK RYHUORDGLQJ 2YHUORDGLQJ LQWURGXFHG LQ &KDSWHU LV ZKHUH RQH FODVV FRQWDLQV PXOWLSOH PHWKRGV ZLWK WKH VDPH QDPH EXW ZLWK GLIIHUHQW SDUDPHWHUV 2YHUULGLQJ LV ZKHQ D VXEFODVV UHZULWHV D QHZ YHUVLRQ RI LWV VXSHUFODVV V PHWKRG ZLWK H[DFWO\ WKH VDPH QDPH DQG WKH VDPH SDUDPHWHUV 3RO\PRUSKLVP 2QH YHU\ LQWHUHVWLQJ DQG RGG WKLQJ DERXW LQKHULWHG W\SHV LV WKDW LW V OHJDO IRU D YDULDEOH RI D VXSHUFODVV W\SH WR UHIHU WR DQ REMHFW RI RQH RI LWV VXEFODVV W\SHV )RU H[DPSOH WKH IROORZLQJ LV D OHJDO DVVLJQPHQW VWDWHPHQW Employee employee1 = new Lawyer(); <RX YH DOUHDG\ VHHQ FDVHV ZKHUH RQH W\SH FDQ VWRUH D YDOXH RI DQRWKHU W\SH ZLWK SULPLWLYH YDOXHV )RU H[DPSOH DQ LQW YDOXH FDQ EH VWRUHG LQWR D GRXEOH YDULDEOH ,Q WKRVH FDVHV WKH LQW YDOXH ZDV DXWRPDWLFDOO\ FRQYHUWHG LQWR D GRXEOH ZKHQ LW ZDV DVVLJQHG :KHQ D VXEFODVV REMHFW LV VWRUHG LQWR D VXSHUFODVV YDULDEOH QR VXFK FRQYHUVLRQ RFFXUV 7KH REMHFW UHIHUUHG WR E\ HPSOR\HH UHDOO\ LV D /DZ\HU REMHFW QRW DQ (PSOR\HH REMHFW ,I ZH FDOO PHWKRGV RQ LW LW ZLOO EHKDYH OLNH D /DZ\HU REMHFW )RU H[DPSOH WKH FDOO 485 employee1.applyForVacation(); SURGXFHV WKH IROORZLQJ RXWSXW Use the pink form to apply for vacation. ,W WXUQV RXW WKDW WKLV DELOLW\ IRU YDULDEOHV WR UHIHU WR VXEFODVV REMHFWV LV RQH RI WKH PRVW FUXFLDOO\ LPSRUWDQW LGHDV LQ REMHFW RULHQWHG SURJUDPPLQJ ,W DOORZV XV WR ZULWH IOH[LEOH FRGH WKDW FDQ LQWHUDFW ZLWK PDQ\ W\SHV RI REMHFWV LQ WKH VDPH ZD\ )RU H[DPSOH ZH FDQ ZULWH D PHWKRG WKDW DFFHSWV DQ (PSOR\HH DV D SDUDPHWHU RU UHWXUQV DQ (PSOR\HH RU FUHDWH DQ DUUD\ RI (PSOR\HH REMHFWV ,Q DQ\ RI WKHVH FDVHV ZH FDQ VXEVWLWXWH DQ 6HFUHWDU\ /DZ\HU RU RWKHU VXEFODVV REMHFW RI (PSOR\HH DQG WKH FRGH ZLOO VWLOO ZRUN (YHQ PRUH LPSRUWDQWO\ WKH FRGH ZLOO DFWXDOO\ EHKDYH GLIIHUHQWO\ GHSHQGLQJ RQ ZKLFK W\SH RI REMHFW LV XVHG EHFDXVH HDFK VXEFODVV RYHUULGHV DQG FKDQJHV VRPH RI WKH EHKDYLRU IURP WKH VXSHUFODVV 7KLV LPSRUWDQW DELOLW\ IRU WKH VDPH FRGH WR EH XVHG ZLWK VHYHUDO GLIIHUHQW W\SHV RI REMHFWV LV FDOOHG SRO\PRUSKLVP 3RO\PRUSKLVP 7KH DELOLW\ IRU WKH VDPH FRGH WR EH XVHG ZLWK VHYHUDO GLIIHUHQW W\SHV RI REMHFWV DQG EHKDYH GLIIHUHQWO\ GHSHQGLQJ RQ WKH DFWXDO W\SH RI REMHFW XVHG +HUH LV DQ H[DPSOH WHVW ILOH WKDW XVHV (PSOR\HH REMHFWV SRO\PRUSKLFDOO\ DV SDUDPHWHUV WR D VWDWLF PHWKRG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class EmployeeMain3 { public static void main(String args) { Employee empl = new Employee(); Lawyer law = new Lawyer(); Marketer mark = new Marketer(); Secretary sec = new Secretary(); printInfo(empl); printInfo(law); printInfo(mark); printInfo(sec); } public static void printInfo(Employee employee) { employee.applyForVacation(); employee.showHours(); employee.showSalary(); employee.showVacation(); System.out.println(); } } 1RWLFH WKDW WKH VWDWLF PHWKRG OHWV XV SDVV PDQ\ GLIIHUHQW W\SHV RI HPSOR\HHV DV SDUDPHWHUV DQG LW SURGXFHV GLIIHUHQW GHSHQGLQJ RQ ZKLFK W\SH ZDV SDVVHG 3RO\PRUSKLVP JLYHV XV WKLV IOH[LELOLW\ 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW 486 Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 2 weeks of paid vacation. Use the pink form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 3 weeks of paid vacation. Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $50,000. I receive 2 weeks of paid vacation. Use the yellow form to apply for vacation. I work 40 hours per week. My salary is $40,000. I receive 2 weeks of paid vacation. 7KH 0HFKDQLFV RI 3RO\PRUSKLVP ,QKHULWDQFH DQG SRO\PRUSKLVP LQWURGXFH VRPH FRPSOH[ QHZ PHFKDQLFV DQG EHKDYLRU LQWR RXU SURJUDPV 2QH XVHIXO ZD\ WR JHW WKH KDQJ RI WKHVH PHFKDQLFV LV WR SHUIRUP VRPH H[HUFLVHV WR LQWHUSUHW WKH EHKDYLRU RI LQKHULWDQFH SURJUDPV 7KH PDLQ JRDO RI WKHVH H[HUFLVHV LV WR XQGHUVWDQG LQ GHWDLO ZKDW KDSSHQV ZKHQ D -DYD SURJUDP ZLWK LQKHULWDQFH H[HFXWHV 7KH (PSOR\HH0DLQ SURJUDP LQ WKH ODVW VHFWLRQ VHUYHV DV D WHPSODWH IRU D PRUH JHQHUDO VHW RI TXHVWLRQV ZH PLJKW DVN DERXW LQKHULWDQFH KLHUDUFKLHV 7KH TXHVWLRQV WDNH WKLV IRUP *LYHQ WKH IROORZLQJ KLHUDUFK\ RI FODVVHV ZKDW EHKDYLRU ZRXOG UHVXOW LI ZH FUHDWHG VHYHUDO REMHFWV RI WKH GLIIHUHQW W\SHV DQG FDOOHG YDULRXV PHWKRGV RQ WKHP" 7R DFKLHYH VRPH SRO\PRUSKLVP LQ WKHVH W\SHV RI TXHVWLRQV ZH OO VWRUH WKH REMHFWV EHLQJ H[DPLQHG LQWR DQ DUUD\ ,Q WKH FDVH RI RXU (PSOR\HH KLHUDUFK\ LW V OHJDO IRU DQ /DZ\HU 6HFUHWDU\ RU DQ\ RWKHU VXEFODVV RI (PSOR\HH WR UHVLGH DV DQ HOHPHQW RI DQ (PSOR\HH>@ 7KH IROORZLQJ SURJUDP SURGXFHV WKH VDPH RXWSXW DV WKH (PSOR\HH0DLQ IURP ODVW VHFWLRQ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class EmployeeMain4 { public static void main(String args) { Employee employees = {new Employee(), new Lawyer(), new Marketer(), new Secretary()}; // print information about each employee for (int i = 0; i < employees.length; i++) { employees[i].applyForVacation(); employees[i].showHours(); employees[i].showSalary(); employees[i].showVacation(); System.out.println(); } } } 487 (YHQ LI \RX GLGQ W XQGHUVWDQG LQKHULWDQFH \RX PLJKW EH DEOH WR GHGXFH VRPH WKLQJV DERXW WKH KLHUDUFK\ IURP WKH FODVVHV QDPHV DQG WKH UHODWLRQVKLSV EHWZHHQ HPSOR\HHV LQ WKH UHDO ZRUOG 6R OHW V WDNH WKLV VRUW RI H[HUFLVH RQH VWHS IXUWKHU :H OO PDNH WKH QDPHV RI WKH FODVVHV LQWR QRQVHQVLFDO SKUDVHV WKDW EHDU QR UHVHPEODQFH WR UHDO ZRUOG HQWLWLHV $GPLWWHGO\ WKLV LV QRW VRPHWKLQJ \RX G GR ZKHQ UHDOO\ SURJUDPPLQJ EXW RXU JRDO KHUH LV WR H[DPLQH WKH PHFKDQLFV RI LQKHULWDQFH DQG SRO\PRUSKLVP RQ WKHLU RZQ $VVXPH WKDW WKH IROORZLQJ FODVVHV KDYH EHHQ GHILQHG 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 1 2 3 4 5 6 7 8 9 1 2 3 4 5 public class A { public void method1() { System.out.println("A 1"); } public void method2() { System.out.println("A 2"); } public String toString() { return "A"; } } public class B extends A { public void method2() { System.out.println("B 2"); } } public class C extends A { public void method1() { System.out.println("C 1"); } public String toString() { return "C"; } } public class D extends C { public void method2() { System.out.println("D 2"); } } &RQVLGHU WKH IROORZLQJ FOLHQW FRGH ZKLFK XVHV WKH DERYH FODVVHV :H GHFODUH DQ DUUD\ RI YDULDEOHV RI D VXSHUFODVV W\SH DQG ILOO LW ZLWK REMHFWV RI WKH YDULRXV VXEFODVV W\SHV :KHQ ZH FDOO PHWKRGV RQ WKH HOHPHQWV RI RXU DUUD\ ZH VKRXOG REVHUYH SRO\PRUSKLF EHKDYLRU 488 1 2 3 4 5 6 7 8 9 10 11 12 13 // Client program to use the A, B, C, and D classes. public class ABCDMain { public static void main(String args) { A elements = {new A(), new B(), new C(), new D()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } } } +RZ GR ZH JR DERXW LQWHUSUHWLQJ VXFK FRGH DQG GHWHUPLQLQJ LWV FRUUHFW RXWSXW" 'LDJUDPPLQJ 3RO\PRUSKLF &RGH 7R GHWHUPLQH WKH RXWSXW RI D SRO\PRUSKLF SURJUDP OLNH WKH RQH LQ WKH SUHYLRXV VHFWLRQ ZH PXVW GHWHUPLQH ZKDW KDSSHQV ZKHQ HDFK HOHPHQW LV SULQWHG ZKHQ LWV WR6WULQJ PHWKRG LV FDOOHG DQG ZKHQ LWV PHWKRG DQG PHWKRG PHWKRGV DUH FDOOHG 2QH ZD\ WR JHW VWDUWHG LV WR GUDZ D GLDJUDP OLNH WKRVH VHHQ LQ SUHYLRXV VHFWLRQV :H OO GUDZ HDFK W\SH DV D ER[ OLVWLQJ LWV PHWKRGV LQ WKH ER[ DQG FRQQHFWLQJ VXEFODVVHV WR WKHLU VXSHUFODVVHV E\ DUURZV 7KH SLFWXUHV KHUH DUH VLPSOLILHG YHUVLRQV RI FRPPRQO\ XVHG 223 GUDZLQJV FDOOHG 80/ &ODVV 'LDJUDPV 1RZ OHW V HQKDQFH RXU GLDJUDP E\ ZULWLQJ WKH PHWKRGV RXWSXW QH[W WR WKHLU QDPHV $OVR OHW V QRW RQO\ ZULWH WKH PHWKRGV GHILQHG LQ HDFK FODVV EXW DOVR WKH RQHV WKDW WKH FODVV LQKHULWV :H OO VWDUW E\ ILOOLQJ LQ WKH PHWKRGV IURP WKH $ FODVV ZLWK WKHLU RXWSXW :KHQ VRPHRQH FDOOV PHWKRG RQ DQ $ REMHFW WKH UHVXOWLQJ RXWSXW LV $ :KHQ VRPHRQH FDOOV PHWKRG RQ DQ $ REMHFW WKH UHVXOWLQJ RXWSXW LV $ :KHQ VRPHRQH SULQWV DQ $ REMHFW ZLWK WR6WULQJ WKH UHVXOWLQJ RXWSXW LV $ /HW V ILOO WKHP LQ DV D QRWH RQ RXU GLDJUDP 489 1H[W OHW V ORRN DW WKH % W\SH % LQKHULWV DOO WKH EHKDYLRU IURP $ H[FHSW WKDW LW RYHUULGHV WKH PHWKRG RXWSXW WR VD\ % 6R WKDW PHDQV ZH FDQ ILOO LQ WKH % RXWSXW RQ RXU GLDJUDP LGHQWLFDOO\ WR WKH $ FROXPQ H[FHSW WKDW ZH OO UHSODFH $ ZLWK % 7KLUG OHW V ORRN DW WKH & W\SH & LQKHULWV DOO WKH EHKDYLRU IURP $ H[FHSW WKDW LW RYHUULGHV WKH PHWKRG RXWSXW WR VD\ & DQG RYHUULGHV WKH WR6WULQJ PHWKRG WR VD\ & 6R WKDW PHDQV ZH FDQ ILOO LQ WKH & FROXPQ RI RXU WDEOH LGHQWLFDOO\ WR WKH $ FROXPQ H[FHSW WKDW ZH OO UHSODFH $ ZLWK & DQG $ ZLWK & :H OO WUHDW WKH ' W\SH VLPLODUO\ ' LQKHULWV DOO WKH EHKDYLRU IURP & H[FHSW WKDW LW RYHUULGHV WKH PHWKRG RXWSXW WR VD\ ' )ROORZLQJ RXU SDWWHUQ ZH OO JHW WKLV ILQDO WDEOH 490 $QRWKHU ZD\ WR GR WKLV LV WR PDNH D WDEOH RI EHKDYLRU IRU HDFK W\SH IRU HDFK RI LWV PHWKRGV /LVW HDFK W\SH KRUL]RQWDOO\ DQG HDFK PHWKRG QDPH YHUWLFDOO\ PHWKRG QDPH PHWKRG PHWKRG WR6WULQJ $ $ $ $ % $ % $ & & $ & ' & ' & 1RZ WKDW ZH YH FUHDWHG D GLDJUDP RU WDEOH ZH FDQ ILJXUH RXW WKH RXWSXW RI WKH FRGH 7KH DUUD\ FRQWDLQV D $ REMHFW D % REMHFW D & REMHFW WKHQ D ' REMHFW 2Q HDFK RI WKHVH LW SULQWV WKH WR6WULQJ RXWSXW WKHQ FDOOV PHWKRG WKHQ PHWKRG WKHQ D EODQN OLQH :KHQ D PHWKRG JHWV FDOOHG RQ DQ REMHFW ZH FDQ MXVW ORRN XS WKH RXWSXW RI WKDW PHWKRG IRU WKDW W\SH RQ RXU GLDJUDP RU WDEOH :H FDQ VHH WKDW WKH RXWSXW IRU WKHVH PHWKRGV RQ DQ $ REMHFW HOHPHQW RI WKH DUUD\ LV WKH IROORZLQJ A A 1 A 2 8VLQJ WKH WDEOH LQ WKLV ZD\ IRU DOO W\SHV ZH JHW WKH IROORZLQJ FRPSOHWH RXWSXW IRU WKH H[HUFLVH 491 A A 1 A 2 A A 1 B 2 C C 1 A 2 C C 1 D 2 ,QWHUDFWLQJ ZLWK WKH 6XSHUFODVV 7KH FODVVHV LQ WKH SUHYLRXV VHFWLRQV GHPRQVWUDWHG LQKHULWDQFH ZLWK PHWKRGV EXW QRZ ZH OO ZDQW WR ZULWH PRUH PHDQLQJIXO FODVVHV WKDW XVH LQKHULWDQFH ZLWK ILHOGV PHWKRGV DQG FRQVWUXFWRUV 7KHVH VXEFODVVHV UHTXLUH PRUH FRPSOH[ LQWHUDFWLRQ ZLWK WKH VWDWH DQG EHKDYLRU WKH\ LQKHULW IURP WKHLU VXSHUFODVV 7R SHUIRUP WKLV LQWHUDFWLRQ SURSHUO\ ZH OO QHHG WR H[DPLQH D QHZ NH\ZRUG QDPHG super ,QKHULWHG )LHOGV ,Q WKH SUHYLRXV FKDSWHU V FDVH VWXG\ ZH EXLOW D 6WRFN FODVV UHSUHVHQWLQJ SXUFKDVHG VKDUHV RI D JLYHQ VWRFN +HUH V WKH FRGH IRU WKDW FODVV VKRUWHQHG D ELW IRU WKLV VHFWLRQ DQG ZLWK D JHW7RWDO6KDUHV PHWKRG DGGHG WR UHWXUQ WKH VWRFN RZQHU V QXPEHU RI VKDUHV 492 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // A Stock object represents purchases of shares of a particular stock. public class Stock { private String symbol; // stock symbol, such as "YHOO" private int totalShares; // total number of shares purchased private double totalCost; // total cost for all shares purchased // Creates a new Stock with the given symbol and no shares purchased. public Stock(String theSymbol) { symbol = theSymbol; totalShares = 0; totalCost = 0.00; } // Returns the total number of shares purchased of this stock. public int getTotalShares() { return totalShares; } // Returns the total profit or loss earned on this stock. public double getProfit(double currentPrice) { double marketValue = totalShares * currentPrice; return marketValue - totalCost; } // Records a purchase of the given shares at the given price. public void purchase(int shares, double pricePerShare) { totalShares += shares; totalCost += shares * pricePerShare; } } 1RZ OHW V LPDJLQH WKDW ZH ZDQW WR FUHDWH D W\SH RI REMHFWV IRU VWRFNV WKDW SD\ GLYLGHQGV 'LYLGHQGV DUH SURILW VKDULQJ SD\PHQWV PDGH E\ D FRUSRUDWLRQ WR LWV VKDUHKROGHUV 7KH DPRXQW WKH VKDUHKROGHU UHFHLYHV LV SURSRUWLRQDO WR WKH QXPEHU RI VKDUHV WKH\ RZQ 1RW HYHU\ VWRFN SD\V GLYLGHQGV VR ZH ZRXOGQ W ZDQW WR DGG WKLV IXQFWLRQDOLW\ GLUHFWO\ WR RXU 6WRFN FODVV ,QVWHDG ZH OO FUHDWH D QHZ FODVV FDOOHG 'LYLGHQG6WRFN WKDW H[WHQGV 6WRFN DQG DGGV WKLV QHZ EHKDYLRU (DFK 'LYLGHQG6WRFN REMHFW ZLOO LQKHULW WKH V\PERO WRWDO VKDUHV DQG WRWDO FRVW IURP WKH 6WRFN VXSHUFODVV :H OO DGG D GDWD ILHOG WR UHFRUG WKH DPRXQW RI GLYLGHQGV SDLG public class DividendStock extends Stock { private double dividends; // amount of dividends paid ... } 8VLQJ WKH GLYLGHQGV GDWD ILHOG ZH FDQ ZULWH D PHWKRG LQ WKH 'LYLGHQG6WRFN WKDW OHWV WKH VKDUHKROGHU UHFHLYH D SHU VKDUH GLYLGHQG %XW WKH IROORZLQJ FRGH ZRQ W FRPSLOH // This code does not compile! public void payDividend(double amountPerShare) { dividends += amountPerShare * totalShares; } 493 &RPPRQ 3URJUDPPLQJ (UURU 7U\LQJ WR DFFHVV D SULYDWH GDWD ILHOG IURP D VXEFODVV $ 'LYLGHQG6WRFN FDQQRW DFFHVV WKH WRWDO6KDUHV GDWD ILHOG LW KDV LQKHULWHG EHFDXVH WRWDO6KDUHV LV GHFODUHG SULYDWH LQ 6WRFN $ VXEFODVV PD\ QRW UHIHU GLUHFWO\ WR DQ\ SULYDWH GDWD ILHOGV WKDW ZHUH GHFODUHG LQ LWV VXSHUFODVV <RX OO JHW D FRPSLOHU HUURU OLNH WKH IROORZLQJ DividendStock.java:17: totalShares has private access in Stock 7KH VROXWLRQ LV WR XVH WKH DFFHVVRU RU PXWDWRU PHWKRGV DVVRFLDWHG ZLWK WKRVH GDWD ILHOGV WR DFFHVV RU FKDQJH WKHLU YDOXHV +HUH LV D FRUUHFWHG YHUVLRQ RI WKH SD\'LYLGHQG PHWKRG // Records a payment of a divident of the given dividend // amount for each share purchased of this stock. public void payDividend(double amountPerShare) { dividends += amountPerShare * getTotalShares(); } ,Q D VXEFODVV DFFHVV RU PRGLI\ WKH VXSHUFODVV V GDWD ILHOG XVLQJ D PHWKRG LQVWHDG RI UHIHUULQJ WR LW GLUHFWO\ &DOOLQJ D 6XSHUFODVV V &RQVWUXFWRU &RQVWUXFWRUV XQOLNH PHWKRGV DUH QRW LQKHULWHG VR ZH OO KDYH WR ZULWH RXU RZQ FRQVWUXFWRU IRU WKH 'LYLGHQG6WRFN FODVV 7KH LVVXH ZLWK DFFHVVLQJ SULYDWH GDWD ILHOGV ZLOO DULVH DJDLQ ZKHQ ZH ZULWH D FRQVWUXFWRU IRU 'LYLGHQG6WRFN :H G OLNH WKH 'LYLGHQG6WRFN FRQVWUXFWRU WR DFFHSW WKH VDPH SDUDPHWHU DV WKH 6WRFN FRQVWUXFWRU WKH V\PERO :H ZDQW LW WR KDYH WKH VDPH EHKDYLRU DV WKH 6WRFN FRQVWUXFWRU EXW DGGLWLRQDOO\ ZH ZDQW WR VHW WKH GLYLGHQGV ILHOG WR 7KH IROORZLQJ FRQVWUXFWRU LPSOHPHQWDWLRQ PLJKW VHHP OLNH D JRRG VWDUW EXW LW LV UHGXQGDQW ZLWK 6WRFN V FRQVWUXFWRU DQG DOVR ZRQ W FRPSLOH VXFFHVVIXOO\ // This constructor does not compile. public DividendStock(String theSymbol) { symbol = theSymbol; totalShares = 0; totalCost = 0.00; dividends = 0.00; // this line is the new code } 7KH FRPSLOHU SURGXFHV IRXU HUURUV RQH HUURU IRU HDFK OLQH WKDW WULHV WR DFFHVV DQ LQKHULWHG SULYDWH GDWD ILHOG DQG D PHVVDJH DERXW D PLVVLQJ 6WRFN FRQVWUXFWRU DividendStock.java:5: cannot find symbol symbol : constructor Stock() location: class Stock public DividendStock(String theSymbol) { ^ DividendStock.java:6: symbol has private access in Stock DividendStock.java:7: totalShares has private access in Stock DividendStock.java:8: totalCost has private access in Stock 494 7KH ILUVW SUREOHP LV WKDW HYHQ WKRXJK D 'LYLGHQG6WRFN GRHV FRQWDLQ WKH V\PERO WRWDO6KDUHV DQG WRWDO&RVW GDWD ILHOGV E\ LQKHULWDQFH LW FDQQRW UHIHU WR WKHP GLUHFWO\ EHFDXVH WKH\ ZHUH GHFODUHG SULYDWH LQ WKH 6WRFN FODVV ,W V GRQH WKLV ZD\ VR WKDW LQKHULWDQFH GRHVQ W SURYLGH D ORRSKROH IRU D PDOLFLRXV VXEFODVV WR EUHDN DQ REMHFW V HQFDSVXODWLRQ 7KH VHFRQG SUREOHP DERXW D PLVVLQJ 6WRFN FRQVWUXFWRU LV D VXEWOH DQG FRQIXVLQJ GHWDLO RI LQKHULWDQFH 7KH SUREOHP LV WKDW D VXEFODVV V FRQVWUXFWRU PXVW FDOO D FRQVWUXFWRU IURP WKH VXSHUFODVV (VVHQWLDOO\ WKH 'LYLGHQG6WRFN SDUWLDOO\ FRQVLVWV RI D 6WRFN REMHFW DQG WKH VWDWH RI WKDW 6WRFN REMHFW PXVW EH LQLWLDOL]HG E\ FDOOLQJ D FRQVWUXFWRU IRU LW ,I ZH GRQ W VSHFLI\ KRZ WR GR VR WKH FRPSLOHU DVVXPHV WKDW 6WRFN KDV D SDUDPHWHUOHVV 6WRFN FRQVWUXFWRU DQG WULHV WR LQLWLDOL]H WKH 6WRFN GDWD E\ FDOOLQJ WKLV FRQVWUXFWRU 6LQFH WKH 6WRFN FODVV GRHVQ W DFWXDOO\ KDYH D SDUDPHWHUOHVV FRQVWUXFWRU WKH EL]DUUH HUURU PHVVDJH DERXW D PLVVLQJ FRQVWUXFWRU 6WRFN RFFXUV ,W V D VKDPH WKH HUURU PHVVDJH LVQ W PRUH LQIRUPDWLYH 7KH VROXWLRQ WR WKLV SUREOHP LV WR H[SOLFLWO\ FDOO WKH 6WRFN FRQVWUXFWRU WKDW DFFHSWV D 6WULQJ V\PERO DV LWV SDUDPHWHU -DYD XVHV WKH NH\ZRUG super IRU D VXEFODVV WR UHIHU WR EHKDYLRU IURP LWV VXSHUFODVV 7R FDOO D FRQVWUXFWRU RI D VXSHUFODVV ZULWH WKH NH\ZRUG super IROORZHG E\ WKH FRQVWUXFWRU V SDUDPHWHU YDOXHV LQ SDUHQWKHVHV super(<expression>, <expression>, ..., <expression>); ,Q WKH FDVH RI RXU 'LYLGHQG6WRFN FRQVWUXFWRU WKH IROORZLQJ FRGH GRHV WKH WULFN :H OO XVH WKH super NH\ZRUG WR FDOO WKH VXSHUFODVV FRQVWUXFWRU SDVVLQJ LW WKH VDPH WKH6\PERO YDOXH WKDW ZDV SDVVHG WR RXU 'LYLGHQG6WRFN FRQVWUXFWRU 7KLV ZLOO LQLWLDOL]H WKH V\PERO WRWDO VKDUHV DQG WRWDO FRVW 7KHQ ZH VHW WKH LQLWLDO GLYLGHQGV WR RXUVHOYHV // Constructs a new dividend stock with the given symbol // and no shares purchased. public DividendStock(String theSymbol) { super(theSymbol); // call Stock constructor this.dividends = 0.00; } 7KH FDOO WR WKH VXSHUFODVV V FRQVWUXFWRU XVLQJ super PXVW EH WKH ILUVW VWDWHPHQW LQ D VXEFODVV V FRQVWUXFWRU ,I \RX UHYHUVH WKH RUGHU RI WKH VWDWHPHQWV LQ 'LYLGHQG6WRFN V FRQVWUXFWRU DQG VHW WKH GLYLGHQGV EHIRUH FDOOLQJ super \RX OO JHW D FRPSLOHU HUURU OLNH WKH IROORZLQJ DividendStock.java:7: call to super must be first statement in constructor super(theSymbol); // call Stock constructor ^ +HUH V RXU 'LYLGHQG6WRFN FODVV VR IDU 7KH FODVV LVQ W FRPSOHWH \HW EHFDXVH ZH KDYH QRW LPSOHPHQWHG WKH EHKDYLRU WR PDNH GLYLGHQG SD\PHQWV 495 // Represents a stock purchase that also pays dividends. public class DividendStock extends Stock { private double dividends; // amount of dividends paid // Constructs a new dividend stock with the given symbol // and no shares purchased. public DividendStock(String theSymbol) { super(theSymbol); // call Stock constructor this.dividends = 0.00; } $OWKRXJK WKH super NH\ZRUG KHOSHG XV VROYH WKH LVVXH RI LQLWLDOL]LQJ RXU 'LYLGHQG6WRFN LW V VWLOO D JRRG LGHD WR ZULWH DFFHVVRU PHWKRGV IRU WKH RWKHU ILHOGV LQ WKH VXSHUFODVV VXFK DV JHW6\PERO DQG JHW7RWDO&RVW LQ FDVH WKH 'LYLGHQG6WRFN VXEFODVV RU RWKHU FODVVHV QHHG WR XVH WKHLU YDOXHV ODWHU &DOOLQJ 2YHUULGGHQ 0HWKRGV 7R LPSOHPHQW GLYLGHQG SD\PHQWV ZH OO EHJLQ E\ ZULWLQJ D PHWKRG QDPHG SD\'LYLGHQG WKDW DFFHSWV D GLYLGHQG DPRXQW SHU VKDUH DQG DGGV WKH SURSHU DPRXQW WR RXU 'LYLGHQG6WRFN V GLYLGHQGV GDWD ILHOG 7KH DPRXQW SHU VKDUH LV PXOWLSOLHG E\ WKH QXPEHU RI VKDUHV KHOG // Records a payment of a divident of the given dividend // amount for each share purchased of this stock. public void payDividend(double amountPerShare) { dividends += amountPerShare * getTotalShares(); } 7KH GLYLGHQG SD\PHQWV EHLQJ UHFRUGHG VKRXOG EH FRQVLGHUHG SURILW IRU WKH VWRFN KROGHU 7KH RYHUDOO SURILW RI D 'LYLGHQG6WRFN LV HTXDO WR WKH SURILW RI WKH UHJXODU 6WRFN SOXV WKH GLYLGHQGV 7KLV LV FRPSXWHG DV WKH PDUNHW YDOXH QXPEHU RI VKDUHV WLPHV FXUUHQW SULFH PLQXV WKH WRWDO FRVW SDLG IRU WKH VKDUHV SOXV WKH DPRXQW RI GLYLGHQGV SDLG %HFDXVH WKH SURILW RI D 'LYLGHQG6WRFN LV FRPSXWHG GLIIHUHQWO\ WKDQ WKDW RI D UHJXODU 6WRFN ZH VKRXOG RYHUULGH WKH JHW3URILW PHWKRG LQ WKH 'LYLGHQG6WRFN FODVV WR LPSOHPHQW WKLV QHZ EHKDYLRU $Q LQFRUUHFW LQLWLDO DWWHPSW PLJKW ORRN OLNH WKLV // This code does not compile. public double getProfit(double currentPrice) { double marketValue = totalShares * currentPrice; return marketValue - totalCost + dividends; } 7KH SUHFHGLQJ FRGH KDV WZR SUREOHPV )RU RQH ZH FDQQRW UHIHU GLUHFWO\ WR WKH YDULRXV GDWD ILHOGV WKDW ZHUH GHFODUHG LQ 6WRFN :H FRXOG DGG DFFHVVRU PHWKRGV IRU HDFK GDWD ILHOG WR JHW DURXQG WKLV 7KH VHFRQG SUREOHP LV WKDW WKH FRGH LV UHGXQGDQW LW GXSOLFDWHV PXFK RI WKH IXQFWLRQDOLW\ IURP 6WRFN V JHW3URILW PHWKRG VKRZQ HDUOLHU 7KH RQO\ QHZ EHKDYLRU LV WKH DGGLQJ RI GLYLGHQGV LQWR WKH WRWDO 496 7R UHPRYH WKLV UHGXQGDQF\ ZH FDQ KDYH 'LYLGHQG6WRFN V JHW3URILW PHWKRG FDOO 6WRFN V JHW3URILW PHWKRG DV SDUW RI LWV FRPSXWDWLRQ +RZHYHU VLQFH WKH WZR PHWKRGV VKDUH WKH VDPH QDPH ZH PXVW GLVDPELJXDWH WKHP E\ H[SOLFLWO\ WHOOLQJ WKH FRPSLOHU ZH ZLVK WR FDOO 6WRFN V YHUVLRQ RI WKH JHW3URILW PHWKRG :H GR WKLV XVLQJ super NH\ZRUG ZKLFK ZH DOVR XVHG LQ WKH SUHYLRXV VHFWLRQ WR FDOO D VXSHUFODVV V FRQVWUXFWRU 7KH JHQHUDO V\QWD[ IRU FDOOLQJ DQ RYHUULGGHQ PHWKRG XVLQJ WKH VXSHU NH\ZRUG LV super.<method name>(<expression>, <expression>, ..., <expression>) +HUH LV WKH FRUUHFWHG FRGH ZKLFK GRHV FRPSLOH DQG HOLPLQDWHV WKH SUHYLRXV UHGXQGDQF\ // Returns the total profit or loss earned on this stock, // including profits made from dividends. public double getProfit(double currentPrice) { return super.getProfit(currentPrice) + dividends; } +HUH LV WKH FRPSOHWHG 'LYLGHQG6WRFN FODVV EXLOW LQ WKH SUHFHGLQJ VHFWLRQV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Represents a stock purchase that also pays dividends. public class DividendStock extends Stock { private double dividends; // amount of dividends paid // Constructs a new dividend stock with the given symbol // and no shares purchased. public DividendStock(String theSymbol) { super(theSymbol); // call Stock constructor this.dividends = 0.00; } // Returns the total profit or loss earned on this stock, // including profits made from dividends. public double getProfit(double currentPrice) { return super.getProfit(currentPrice) + dividends; } // Records a payment of a divident of the given dividend // amount for each share purchased of this stock. public void payDividend(double amountPerShare) { dividends += amountPerShare * getTotalShares(); } } ,W V SRVVLEOH WR KDYH D GHHSHU LQKHULWDQFH KLHUDUFK\ ZLWK PXOWLSOH OD\HUV RI LQKHULWDQFH DQG RYHUULGLQJ +RZHYHU WKH super NH\ZRUG RQO\ UHDFKHV RQH OHYHO XSZDUG WR WKH PRVW UHFHQWO\ RYHUZULWWHQ YHUVLRQ RI WKH PHWKRG ,W V QRW OHJDO WR ZULWH VXSHU PRUH WKDQ RQFH LQ D URZ \RX FDQQRW ZULWH FDOOV OLNH super.super.getProfit ,I \RX QHHG VXFK D VROXWLRQ \RX OO KDYH WR ILQG D ZRUNDURXQG VXFK DV XVLQJ GLIIHUHQW PHWKRG QDPHV 497 $ /DUJHU ([DPSOH 3RLQW ' /HW V FRQVLGHU DQRWKHU H[DPSOH RI H[WHQGLQJ D FODVV ZKHUH super LV XVHIXO ,PDJLQH WKDW ZH G OLNH WR ZULWH D SURJUDP WKDW GHDOV ZLWK SRLQWV LQ GLPHQVLRQDO VSDFH VXFK DV D ' JDPH UHQGHULQJ SURJUDP RU WHUUDLQ VLPXODWLRQ $ 3RLQW ' FODVV ZRXOG EH XVHIXO IRU ZULWLQJ VXFK SURJUDPV ,Q &KDSWHU ZH DOUHDG\ ZURWH D 3RLQW FODVV WR UHSUHVHQW D SRLQW LQ ' VSDFH :H FDQ ZULWH RXU 3RLQW ' FODVV WR H[WHQG 3RLQW DQG DGG D ] FRRUGLQDWH 3RLQW ' V FRQVWUXFWRU QHHGV WR VHW WKH [ DQG \ ILHOGV MXVW DV WKH 3RLQW FRQVWUXFWRU GLG %XW 3RLQW ' DGGV D ] ILHOG ZKLFK PXVW DOVR EH VHW &DOOLQJ super(x, y) VHWV WKH ILUVW WZR ILHOGV WKH WKLUG LV VHW VHSDUDWHO\ +HUH V DQ LQLWLDO YHUVLRQ RI WKH 3RLQW ' FODVV ZLWK D ] GDWD ILHOG FRQVWUXFWRU DQG DFFHVVRU IRU WKH ] YDOXH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // A Point3D object represents an ordered trio of coordinates (x, y, z). public class Point3D extends Point { private int z; // Constructs a new Point3D object with the given coordinates. public Point3D(int x, int y, int z) { super(x, y); this.z = z; } // Returns the z-coordinate of this Point3D. public int getZ() { return z; } } 2QH SLHFH RI XVHIXO EHKDYLRU WR DGG WR RXU 3RLQW ' REMHFWV LV D QHZ WR6WULQJ PHWKRG WKDW LQFOXGHV WKH ] FRRUGLQDWH DV SDUW RI WKH 6WULQJ :H FDQ RYHUULGH WKH WR6WULQJ PHWKRG WR LPSOHPHQW WKLV EHKDYLRU :H OO KDYH WR FDOO JHW; DQG JHW< WR DFFHVV RXU SULYDWH [ DQG \ GDWD ILHOGV ZKHQ EXLOGLQJ WKH 6WULQJ // Returns a String representation of this Point3D. public String toString() { return "(" + getX() + ", " + getY() + ", " + z + ")"; } $QRWKHU XVHIXO SLHFH RI EHKDYLRU LV D WUDQVODWH PHWKRG WKDW DFFHSWV G[ G\ DQG G] DV SDUDPHWHUV DQG VKLIWV WKH SRLQW V ' SRVLWLRQ ,Q WKLV FDVH ZH G OLNH WR FDOO WKH VXSHUFODVV V YHUVLRQ RI WUDQVODWH WR DGMXVW WKH [ DQG \ FRRUGLQDWHV WKHQ DGMXVW WKH ] FRRUGLQDWH RXUVHOYHV %XW RXU QHZ WUDQVODWH PHWKRG KDVQ W UHDOO\ RYHUULGGHQ WKH RQH IURP 3RLQW 7KH WUDQVODWH PHWKRG LV QRW EHLQJ RYHUULGGHQ EXW RYHUORDGHG 3RLQW ' V FRGH ZLOO EH D VHFRQG YHUVLRQ RI WKH VDPH PHWKRG EXW ZLWK GLIIHUHQW SDUDPHWHUV 7KH FRQVHTXHQFH RI WKLV LV WKDW ZKHQ FDOOLQJ WKH VXSHUFODVV V YHUVLRQ RI WUDQVODWH ZH ZRXOGQ W QHHG WR XVH WKH VXSHU NH\ZRUG WR GLVDPELJXDWH WKH WZR PHWKRGV 7KH FRPSLOHU FDQ WHOO ZKLFK PHWKRG ZH UH FDOOLQJ E\ ORRNLQJ DW KRZ PDQ\ SDUDPHWHUV ZH SDVV 498 // Shifts the position of this Point3D object by the given amount. public void translate(int dx, int dy, int dz) { translate(dx, dy); z += dz; } <RX GR QHHG WR XVH WKH VXSHU NH\ZRUG WR FDOO PHWKRGV WKDW \RX KDYH RYHUULGGHQ LQ \RXU VXEFODVV EXW \RX GR QRW QHHG LW WR FDOO PHWKRGV IURP WKH VXSHUFODVV WKDW KDYH QRW EHHQ RYHUULGGHQ 7KH IROORZLQJ LV WKH FRPSOHWH FRGH IRU RXU 3RLQW ' FODVV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // A Point3D object represents an ordered trio of coordinates (x, y, z). public class Point3D extends Point { private int z; // Constructs a new Point3D object with the given coordinates. public Point3D(int x, int y, int z) { super(x, y); this.z = z; } // Returns the z-coordinate of this Point3D. public int getZ() { return z; } // Returns a String representation of this Point3D. public String toString() { return "(" + getX() + ", " + getY() + ", " + z + ")"; } // Shifts the position of this Point3D object by the given amount. public void translate(int dx, int dy, int dz) { translate(dx, dy); z += dz; } } ,QKHULWDQFH LQ WKH -DYD &ODVV /LEUDULHV ,QKHULWDQFH LVQ W RQO\ XVHG LQ \RXU FRGH LW V DOVR SUHYDOHQW LQ WKH -DYD FODVV OLEUDULHV ,Q WKLV VHFWLRQ ZH OO ORRN DW WZR LPSRUWDQW XVHV RI LQKHULWDQFH LQ WKH FODVV OLEUDULHV IRU GUDZLQJ ' JUDSKLFV DQG SHUIRUPLQJ LQSXW DQG RXWSXW *UDSKLFV ' RSWLRQDO 7KH -DYD FODVV OLEUDULHV XVH LQKHULWDQFH LQ WKH GUDZLQJ RI ' JUDSKLFV ,Q &KDSWHU V VXSSOHPHQW RQ *UDSKLFV ZH VDZ DQ REMHFW QDPHG *UDSKLFV WKDW DFWV OLNH D SHQ WR GUDZ VKDSHV DQG OLQHV RQWR D ZLQGRZ :KHQ -DYD V GHVLJQHUV ZDQWHG DGGLWLRQDO JUDSKLFDO IXQFWLRQDOLW\ WKH\ H[WHQGHG WKH *UDSKLFV FODVV LQWR D PRUH SRZHUIXO FODVV QDPHG *UDSKLFV ' 7KLV LV D JRRG H[DPSOH RI RQH RI WKH PRUH FRPPRQ XVHV RI LQKHULWDQFH WR H[WHQG DQG UHXVH IXQFWLRQDOLW\ IURP D SRZHUIXO H[LVWLQJ REMHFW 499 :K\ GLGQ W 6XQ VLPSO\ DGG WKHVH QHZ PHWKRGV LQWR WKH H[LVWLQJ *UDSKLFV" 7KHUH DUH VHYHUDO UHDVRQV )RU RQH LW V SUHIHUDEOH QRW WR GLVWXUE D ZRUNLQJ SLHFH RI FRGH LI \RX FDQ DYRLG LW *UDSKLFV DOUHDG\ ZRUNHG SURSHUO\ VR LW ZDV EHVW QRW WR SHUIRUP XQQHFHVVDU\ VXUJHU\ RQ LW 6HFRQG PDNLQJ *UDSKLFV ' H[WHQG *UDSKLFV SURYLGHV EDFNZDUG FRPSDWLELOLW\ %DFNZDUG FRPSDWLELOLW\ LV WKH DELOLW\ IRU QHZ FRGH WR ZRUN FRUUHFWO\ ZLWK ROG FRGH ZLWKRXW PRGLILFDWLRQ %\ OHDYLQJ *UDSKLFV XQWRXFKHG ROG SURJUDPV DUH JXDUDQWHHG WR UHPDLQ ZRUNLQJ SURSHUO\ ZKLOH QHZ SURJUDPV FDQ FKRRVH WR XVH WKH QHZ *UDSKLFV ' IXQFWLRQDOLW\ LI WKH\ ZLVK 6XQ V GRFXPHQWDWLRQ IRU *UDSKLFV ' GHVFULEHV WKH SXUSRVH RI WKH FODVV 7KLV *UDSKLFV ' FODVV H[WHQGV WKH *UDSKLFV FODVV WR SURYLGH PRUH VRSKLVWLFDWHG FRQWURO RYHU JHRPHWU\ FRRUGLQDWH WUDQVIRUPDWLRQV FRORU PDQDJHPHQW DQG WH[W OD\RXW 7KLV LV WKH IXQGDPHQWDO FODVV IRU UHQGHULQJ GLPHQVLRQDO VKDSHV WH[W DQG LPDJHV RQ WKH -DYD WP SODWIRUP 7R EH VSHFLILF *UDSKLFV ' DGGV WKH DELOLW\ WR SHUIRUP WUDQVIRUPDWLRQV VXFK DV VFDOLQJ DQG URWDWLRQ ZKHQ \RX UH GUDZLQJ 7KHVH FDQ OHDG WR VRPH IXQ DQG LQWHUHVWLQJ LPDJHV RQ WKH VFUHHQ ,I \RX XVH WKH 'UDZLQJ3DQHO FODVV IURP &KDSWHU V JUDSKLFDO VXSSOHPHQW \RX SUHYLRXVO\ ZURWH VWDWHPHQWV OLNH WKH IROORZLQJ WR JHW DFFHVV WR WKH SDQHO V *UDSKLFV REMHFW Graphics g = panel.getGraphics(); ,W WXUQV RXW WKDW WKH JHW*UDSKLFV PHWKRG UHDOO\ GRHVQ W UHWXUQ D *UDSKLFV REMHFW DW DOO EXW UDWKHU D *UDSKLFV ' %HFDXVH RI SRO\PRUSKLVP LW ZDV OHJDO IRU \RXU SURJUDP WR WUHDW LW DV D *UDSKLFV EHFDXVH HYHU\ *UDSKLFV ' LV D *UDSKLFV 7R XVH LW DV D *UDSKLFV ' REMHFW LQVWHDG VLPSO\ VD\ Graphics2D g2 = panel.getGraphics(); +HUH V D SDUWLDO OLVW RI VRPH RI *UDSKLFV ' V H[WUD PHWKRGV 8VHIXO 0HWKRGV RI *UDSKLFV ' 2EMHFWV public void rotate(double angle) 5RWDWHV VXEVHTXHQWO\ GUDZQ LWHPV E\ WKH JLYHQ DQJOH LQ UDGLDQV ZLWK UHVSHFW WR WKH RULJLQ public void scale(double sx, double sy) $GMXVWV WKH VL]H RI DQ\ VXEVHTXHQWO\ GUDZQ LWHPV E\ WKH JLYHQ IDFWRUV public void shear(double shx, double shy) PHDQV HTXDO VL]H *LYHV D VODQW WR DQ\ VXEVHTXHQWO\ GUDZQ LWHPV public void translate(double dx, double dy) 6KLIWV WKH RULJLQ E\ G[ G\ DPRXQW LQ WKH FXUUHQW FRRUGLQDWH V\VWHP 7KH IROORZLQJ SURJUDP GHPRQVWUDWHV *UDSKLFV ' 7KH URWDWH PHWKRG V SDUDPHWHU LV DQ DQJOH RI URWDWLRQ PHDVXUHG LQ UDGLDQV $ UDGLDQ LV DQ DQJXODU XQLW RI PHDVXUH HTXDO WR WKH YDOXH RI GLYLGHG E\ 7KH 0DWK FODVV KDV D VWDWLF PHWKRG QDPHG WR5DGLDQV WKDW FRQYHUWV D QXPEHU RI GHJUHHV LQ RXU SURJUDP V FDVH LQWR WKH HTXLYDOHQW QXPEHU RI UDGLDQV 500 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // Draws a picture of rotating squares using Graphics2D. import java.awt.*; public class FancyPicture { public static void main(String args) { DrawingPanel panel = new DrawingPanel(250, 220); Graphics2D g2 = panel.getGraphics(); g2.translate(100, 120); g2.fillRect(-5, -5, 10, 10); for (int i = 0; i <= 12; i++) { g2.setColor(Color.BLUE); g2.fillRect(20, 20, 20, 20); g2.setColor(Color.BLACK); g2.drawString("" + i, 20, 20); g2.rotate(Math.toRadians(30)); g2.scale(1.1, 1.1); } } } 7KH SURJUDP GUDZV WKH IROORZLQJ ILJXUH DV LWV RXWSXW ,QSXW 2XWSXW 6WUHDPV ,Q SUHYLRXV FKDSWHUV ZH YH VHHQ KRZ WR UHDG DQG ZULWH GDWD WR WKH FRQVROH DV ZHOO DV UHDGLQJ DQG ZULWLQJ IURP H[WHUQDO ILOHV 7KHUH DUH RWKHU SRWHQWLDOO\ LQWHUHVWLQJ VRXUFHV RI GDWD WKDW \RX PLJKW ZLVK WR XVH LQ \RXU SURJUDPV VXFK DV IURP FRQQHFWLRQV WR DQRWKHU FRPSXWHU RU D FRQQHFWLRQ WR D ZHE SDJH -DYD KDV D KLHUDUFK\ RI REMHFWV FDOOHG VWUHDPV WKDW UHSUHVHQW RQH ZD\ IORZV RI GDWD $ VWUHDP FRXOG UHSUHVHQW WKH E\WHV IURP D ILOH IURP WKH FRQVROH RU HOVHZKHUH 7KH VWUHDP FODVVHV DUH IRXQG LQ WKH java.io SDFNDJH :KHQ LW FRPHV WR UHDGLQJ GDWD WKHUH V DQ RYHUDOO VXSHUFODVV QDPHG InputStream DQG PDQ\ VXEFODVVHV UHSUHVHQWLQJ WKH GLIIHUHQW SRVVLEOH VRXUFHV RI GDWD 3DUW RI WKH KLHUDUFK\ ORRNV OLNH WKLV 501 ,W WXUQV RXW WKDW System.in LV D YDULDEOH LQ WKH System FODVV RI W\SH InputStream :KHQ ZH FRQVWUXFW D Scanner WR UHDG IURP WKH FRQVROH ZH UH DFWXDOO\ LQYRNLQJ D FRQVWUXFWRU WKDW DFFHSWV DQ InputStream DV D SDUDPHWHU public Scanner(InputStream in) 7KH RWKHU FRQVWUXFWRU ZH VDZ WKDW DFFHSWV D File REMHFW DFWXDOO\ XVHV WKDW ILOH WR FUHDWH D FileInputStream XQGHU WKH KRRG ,W UHDGV WKH ILOH V GDWD IURP WKLV FileInputStream 2QH LQWHUHVWLQJ VRXUFH RI GDWD ZH KDYHQ W \HW H[SORUHG LV ZHE SDJHV ,W WXUQV RXW WKDW LW V QRW YHU\ GLIILFXOW WR RSHQ D Scanner WR UHDG GDWD IURP D ZHE SDJH EXW ZH ZLOO QHHG WR OHDUQ EULHIO\ DERXW VRPH QHZ REMHFWV IURP WKH -DYD FODVV OLEUDULHV URL DQG IOException $ URL REMHFW IURP WKH java.net SDFNDJH UHSUHVHQWV D 8QLIRUP 5HVRXUFH /RFDWRU IRU D ZHE SDJH VXFK DV KWWS ZZZ DSSOH FRP LWXQHV :H FDQ XVH URL REMHFWV WR UHSUHVHQW D FRQQHFWLRQ WR D ZHE SDJH LQ RXU SURJUDPV 7R XVH URL ZH OO QHHG WR LPSRUW WKH java.net SDFNDJH LQWR RXU SURJUDP import java.net.*; // for URL 7KH VLPSOHVW ZD\ WR FRQVWUXFW D URL REMHFW LV E\ SDVVLQJ D String DV WKH SDUDPHWHU URL website = new URL("http://www.google.com/"); 2QFH ZH FUHDWH D URL REMHFW ZH FDQ DVN LW IRU DQ LQSXW VWUHDP IURP ZKLFK WR UHDG WKH FKDUDFWHUV RI WKDW ZHE SDJH $ URL REMHFW KDV D PHWKRG FDOOHG openStream WKDW UHWXUQV DQ InputStream InputStream stream = website.openStream(); 1RZ WKDW ZH KDYH WKH InputStream WR UHDG WKH ZHE VLWH ZH FDQ FRQVWUXFW D Scanner WR UHDG WKH GDWD 7KH Scanner V XVXDO PHWKRGV VXFK DV nextLine DQG hasNext VWLOO ZRUN DV ZH H[SHFW Scanner in = new Scanner(stream); 502 8QIRUWXQDWHO\ WKH DFW RI FRQVWUXFWLQJ D 85/ REMHFW DQG RSHQLQJ LWV LQSXW VWUHDP FDQ SRWHQWLDOO\ WKURZ H[FHSWLRQV VXFK DV LI WKH 85/ 6WULQJ LV EDGO\ IRUPDWWHG RU LI WKH ZHE VLWH FRQQHFWLRQ LVQ W DYDLODEOH 7KH W\SH RI WKHVH H[FHSWLRQV LV FDOOHG ,2([FHSWLRQ &RQVWUXFWLQJ D 85/ REMHFW DFWXDOO\ WKURZV D VXEFODVV RI ,2([FHSWLRQ FDOOHG 0DOIRUPHG85/([FHSWLRQ ([FHSWLRQV LQ -DYD XVH LQKHULWDQFH WRR $ SURJUDP WU\LQJ WR XVH 85/V ZRXOG UHFHLYH HUURUV OLNH WKHVH DisplayWebPage.java:7: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown URL website = new URL("http://www.google.com/"); ^ DisplayWebPage.java:8: unreported exception java.io.IOException; must be caught or declared to be thrown InputStream stream = website.openStream(); ^ 2 errors 6R ZH FDQ HLWKHU WU\ FDWFK WKH H[FHSWLRQV WR KDQGOH WKHP RU ZH PXVW GHFODUH RXU PDLQ PHWKRG ZLWK WKURZV ,2([FHSWLRQ LQ LWV KHDGHU 7KH IROORZLQJ SURJUDP UHDGV DQG SULQWV D ZHE SDJH 7KH SURJUDP LV GHFODUHG WR WKURZ WKH H[FHSWLRQ VR LI WKH ZHE VLWH RU FRQQHFWLRQ LV GRZQ WKH SURJUDP ZLOO WHUPLQDWH ZLWK DQ H[FHSWLRQ HUURU PHVVDJH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // This program reads the HTML text of a web page // and prints it on the console. import java.io.*; import java.net.*; import java.util.*; // for URL public class DisplayWebPage { public static void main(String args) throws IOException { URL website = new URL("http://www.homestarrunner.com/"); InputStream stream = website.openStream(); Scanner in = new Scanner(stream); while (in.hasNextLine()) { String line = in.nextLine(); System.out.println(line); } } } 7KH SURJUDP SURGXFHV RXWSXW VXFK DV WKH IROORZLQJ 503 <HTML> <HEAD> <TITLE>Everybody! Everybody!</TITLE> </HEAD> <BODY bgcolor="black"> <center><OBJECT classid="clsid:D27CDB6EAE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash. cab#version=4,0,0,0" ID=newintro WIDTH=550 HEIGHT=400> <PARAM NAME=movie VALUE="welcome.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#000000> <EMBED src="welcome.swf" quality=high WIDTH=550 HEIGHT=400 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/i ndex.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED> </OBJECT></center> ... </BODY> </HTML> ,QWHUIDFHV ,QKHULWDQFH LV D YHU\ XVHIXO WRRO EHFDXVH LW SURYLGHV SRO\PRUSKLVP DQG FRGH VKDULQJ EXW LW GRHV KDYH VHYHUDO OLPLWDWLRQV )RU RQH EHFDXVH -DYD XVHV VLQJOH LQKHULWDQFH D FODVV FDQ RQO\ H[WHQG RQH VXSHUFODVV 7KLV PDNHV LW LPSRVVLEOH WR XVH LQKHULWDQFH WR VHW XS PXOWLSOH LV D FRQQHFWLRQV IRU W\SHV WKDW ZLVK WR VKDUH PXOWLSOH FKDUDFWHULVWLFV VXFK DV REMHFWV WKDW DUH ERWK FRORUHG DQG FRPSDUDEOH 7KHUH DUH DOVR VLWXDWLRQV ZKHUH \RX ZDQW SRO\PRUSKLVP ZLWKRXW VKDULQJ FRGH LQ ZKLFK FDVH LQKHULWDQFH LVQ W WKH ULJKW WRRO IRU WKH MRE -DYD KDV D FRQVWUXFW FDOOHG DQ LQWHUIDFH WKDW FDQ UHSUHVHQW D FRPPRQ VXSHUW\SH EHWZHHQ VHYHUDO FODVVHV ZLWKRXW FRGH VKDULQJ $Q LQWHUIDFH LV OLNH D FODVV EXW LW RQO\ FRQWDLQV PHWKRG KHDGHUV ZLWKRXW PHWKRG ERGLHV $Q LQWHUIDFH LV OLNH D VHW RI TXDOLILFDWLRQV WKDW FDQ EH LPSRVHG RQ D FODVV RU PRUH VSHFLILFDOO\ D VHW RI PHWKRGV WKDW D FODVV FDQ SURPLVH WR LPSOHPHQW ,QWHUIDFH $ VHW RI PHWKRGV WKDW FODVVHV FDQ SURPLVH WR LPSOHPHQW DOORZLQJ WKRVH FODVVHV WR EH WUHDWHG VLPLODUO\ LQ \RXU FRGH :H VD\ WKDW DQ LQWHUIDFH GHILQHV D UROH WKDW DQ REMHFW FDQ SOD\ IRU H[DPSOH D 5HFWDQJOH FODVV PLJKW LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH WR LQGLFDWH WKDW 5HFWDQJOH REMHFWV FDQ EH FRPSDUHG WR HDFK RWKHU RU D 3RLQW FODVV PLJKW LPSOHPHQW WKH &ORQHDEOH LQWHUIDFH WR LQGLFDWH WKDW D 3RLQW REMHFW FDQ EH UHSOLFDWHG 504 $Q ,QWHUIDFH IRU 6KDSH &ODVVHV ,Q WKLV VHFWLRQ ZH OO XVH DQ LQWHUIDFH WR GHILQH D SRO\PRUSKLF KLHUDUFK\ RI VKDSH FODVVHV ZLWKRXW VKDULQJ FRGH EHWZHHQ WKHP ,PDJLQH WKDW ZH DUH FUHDWLQJ FODVVHV WR UHSUHVHQW PDQ\ GLIIHUHQW W\SHV RI VKDSHV VXFK DV UHFWDQJOHV FLUFOHV DQG WULDQJOHV :H PLJKW EH WHPSWHG WR XVH LQKHULWDQFH ZLWK WKHVH VKDSH FODVVHV EHFDXVH WKH\ VHHP WR VKDUH VRPH FRPPRQ EHKDYLRU $OO VKDSHV KDYH DQ DUHD DQG SHULPHWHU IRU H[DPSOH %XW HDFK VKDSH FRPSXWHV LWV DUHD DQG SHULPHWHU LQ D WRWDOO\ GLIIHUHQW ZD\ DV OLVWHG EHORZ 7KH Z DQG K UHSUHVHQW WKH UHFWDQJOH V ZLGWK DQG KHLJKW WKH U UHSUHVHQWV WKH FLUFOH V UDGLXV DQG WKH D E DQG F UHSUHVHQW WKH OHQJWKV RI WKH WULDQJOH V WKUHH VLGHV :H OO GHIHU WKH IRUPXOD IRU D WULDQJOH V DUHD IRU D PRPHQW VKDSH &LUFOH DUHD U SHULPHWHU Z U E F K 5HFWDQJOH Z K 7ULDQJOH VHH WH[W D ,W VWLOO VHHPV DV WKRXJK WKHUH LV DQ LV D UHODWLRQVKLS KHUH EHFDXVH D UHFWDQJOH FLUFOH RU WULDQJOH LV D VKDSH %XW FRGH VKDULQJ LVQ W XVHIXO LQ WKLV FDVH EHFDXVH WKH\ DOO LPSOHPHQW WKHLU EHKDYLRU LQ FRPSOHWHO\ GLIIHUHQW ZD\V 7KHUH V DQRWKHU PRUH VXEWOH SUREOHP ZLWK KDYLQJ D 6KDSH VXSHUFODVV LW LQWURGXFHV D QHZ XQZDQWHG W\SH LQWR WKH V\VWHP :H ZRXOGQ W ZDQW D FOLHQW SURJUDP WR EH DEOH WR FRQVWUXFW D new Shape() EHFDXVH WKHUH V QR UHDO ZRUOG HQWLW\ FDOOHG 6KDSH (YHU\ VKDSH REMHFW VKRXOG EH RQH RI WKH W\SHV ZH QDPHG RU VRPH RWKHU SRO\JRQ $ EHWWHU VROXWLRQ ZRXOG EH WR ZULWH DQ LQWHUIDFH WR UHSUHVHQW WKH FRPPRQ IXQFWLRQDOLW\ RI DOO VKDSHV WKH DELOLW\ WR DVN IRU WKHLU DUHD DQG SHULPHWHU 7KH YDULRXV VKDSH FODVVHV FDQ VSHFLI\ WKDW WKH\ LPSOHPHQW DOO PHWKRGV RI WKH 6KDSH LQWHUIDFH :H VD\ WKDW WKH FODVVHV LPSOHPHQW WKH LQWHUIDFH EHFDXVH WKH\ ZLOO LPSOHPHQW WKH PHWKRGV VSHFLILHG E\ WKH LQWHUIDFH ,Q RXU LQWHUIDFH ZH ZULWH D KHDGHU 7R ZULWH DQ LQWHUIDFH ZH FUHDWH D QHZ MDYD ILOH ZLWK WKH VDPH QDPH DV WKH LQWHUIDFH V QDPH RXU 6KDSH LQWHUIDFH ZRXOG EH VWRUHG LQ 6KDSH MDYD :H JLYH WKH LQWHUIDFH D KHDGHU ZLWK WKH NH\ZRUG interface LQ SODFH RI WKH ZRUG class public interface Shape { <contents of the interface> } ,QVLGH WKH LQWHUIDFH ZH ZULWH KHDGHUV IRU HDFK PHWKRG ZH ZDQW VKDSHV WR FRQWDLQ %XW LQVWHDG RI ZULWLQJ PHWKRG ERGLHV ZLWK ^ ` ZH VLPSO\ SODFH D VHPLFRORQ DW WKH HQG RI WKH KHDGHU :H GRQ W VSHFLI\ KRZ WKH PHWKRGV DUH LPSOHPHQWHG ,QVWHDG ZH UH GHPDQGLQJ WKDW DQ\ FODVV WKDW ZDQWV WR EH FRQVLGHUHG D 6KDSH PXVW LPSOHPHQW WKHVH PHWKRGV ,Q IDFW LW LVQ W OHJDO IRU DQ LQWHUIDFH WR FRQWDLQ PHWKRG ERGLHV DQ LQWHUIDFH FDQ RQO\ FRQWDLQ PHWKRG KHDGHUV DQG FODVV FRQVWDQWV 505 7KH IROORZLQJ LV WKH FRPSOHWH FRGH IRU RXU 6KDSH LQWHUIDFH ,W GHFODUHV WKDW VKDSHV KDYH PHWKRGV WR FRPSXWH WKHLU DUHD DQG SHULPHWHU DV W\SH GRXEOH 1 2 3 4 5 // A general interface for shape classes. public interface Shape { public double getArea(); public double getPerimeter(); } 7KH PHWKRGV RI DQ LQWHUIDFH DUH VRPHWLPHV FDOOHG DEVWUDFW PHWKRGV EHFDXVH ZH DUH RQO\ GHFODULQJ WKHLU QDPHV DQG VLJQDWXUHV QRW VSHFLI\LQJ KRZ WKH\ ZLOO EH LPSOHPHQWHG $EVWUDFW 0HWKRG $ PHWKRG WKDW LV GHFODUHG DV LQ DQ LQWHUIDFH EXW QRW LPSOHPHQWHG $EVWUDFW PHWKRGV UHSUHVHQW WKH EHKDYLRU D FODVV SURPLVHV WR LPSOHPHQW ZKHQ LW LPSOHPHQWV DQ LQWHUIDFH :ULWLQJ WKH public NH\ZRUG RQ DQ LQWHUIDFH V PHWKRG KHDGHUV LV RSWLRQDO :H FKRRVH WR LQFOXGH WKH public NH\ZRUG VR WKDW WKH GHFODUDWLRQV LQ WKH LQWHUIDFH PDWFK WKH KHDGHUV RI WKH PHWKRG LPSOHPHQWDWLRQV LQ WKH FODVVHV 7KH JHQHUDO V\QWD[ ZH OO XVH IRU GHFODULQJ DQ LQWHUIDFH LV WKH IROORZLQJ public interface <name> { public <type> <name>(<type> <name>, ..., <type> <name>); public <type> <name>(<type> <name>, ..., <type> <name>); ... public <type> <name>(<type> <name>, ..., <type> <name>); } 'HFODULQJ DQ LQWHUIDFH GRHVQ W DFWXDOO\ GHILQH D QHZ FODVV RI REMHFWV LI WKH FOLHQW FRGH WULHG WR FUHDWH D new Shape() LW ZRXOG QRW FRPSLOH $Q LQWHUIDFH RQO\ HQXPHUDWHV FRQVWUDLQWV WR EH SODFHG RQ RWKHU W\SHV ,PSOHPHQWLQJ WKH 6KDSH ,QWHUIDFH 1RZ WKDW ZH YH ZULWWHQ D 6KDSH LQWHUIDFH ZH ZDQW WR FRQQHFW WKH YDULRXV FODVVHV RI VKDSHV WR LW ,I ZH ZDQW WR FRQQHFW D FODVV WR RXU LQWHUIDFH ZLWK DQ LV D UHODWLRQVKLS WKHUH DUH WZR WKLQJV ZH KDYH WR GR 'HFODUH WKDW WKH FODVV LPSOHPHQWV WKH LQWHUIDFH ,PSOHPHQW HDFK RI WKH LQWHUIDFH V PHWKRGV LQ WKH FODVV 7KH JHQHUDO V\QWD[ IRU GHFODULQJ WKDW D FODVV LPSOHPHQWV DQ LQWHUIDFH LQ D FODVV LV WKH IROORZLQJ public class <name> implements <interface> { ... } ,Q RXU YDULRXV VKDSH FODVVHV ZH PRGLI\ WKHLU KHDGHUV WR LQGLFDWH WKDW WKH\ LPSOHPHQW DOO RI WKH PHWKRGV LQ WKH 6KDSH LQWHUIDFH 7KH ILOH 5HFWDQJOH MDYD IRU H[DPSOH PLJKW EHJLQ OLNH WKLV 506 public class Rectangle implements Shape { ... } :KHQ ZH FODLP WKDW RXU 5HFWDQJOH LPSOHPHQWV 6KDSH ZH DUH SURPLVLQJ WKDW WKH 5HFWDQJOH FODVV ZLOO FRQWDLQ LPSOHPHQWDWLRQV RI WKH JHW$UHD DQG JHW3HULPHWHU PHWKRGV ,I D FODVV FODLPV WR EH D 6KDSH EXW GRHV QRW KDYH D VXLWDEOH JHW$UHD RU JHW3HULPHWHU PHWKRG LW ZLOO QRW FRPSLOH )RU H[DPSOH LI ZH OHDYH WKH ERG\ RI RXU 5HFWDQJOH FODVV HPSW\ DQG WU\ WR FRPSLOH LW WKH FRPSLOHU ZRXOG JLYH HUURUV OLNH WKH IROORZLQJ Rectangle.java:2: Rectangle is not abstract and does not override abstract method getPerime public class Rectangle implements Shape { ^ 1 error 7KH VROXWLRQ LV WR LPSOHPHQW WKH JHW$UHD DQG JHW3HULPHWHU PHWKRGV LQ RXU 5HFWDQJOH FODVV /HW V GHILQH D 5HFWDQJOH REMHFW E\ D ZLGWK DQG KHLJKW 6LQFH WKH DUHD RI D UHFWDQJOH LV HTXDO WR LWV ZLGWK WLPHV LWV KHLJKW ZH OO LPSOHPHQW WKH JHW$UHD PHWKRG E\ PXOWLSO\LQJ LWV GDWD ILHOGV :H OO XVH WKH SUHFHGLQJ SHULPHWHU IRUPXOD Z K WR LPSOHPHQW JHW3HULPHWHU +HUH LV WKH FRPSOHWH 5HFWDQJOH FODVV WKDW LPSOHPHQWV WKH 6KDSH LQWHUIDFH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // Represents rectangle shapes. public class Rectangle implements Shape { private double width; private double height; // Constructs a new rectangle with the given dimensions. public Rectangle(double width, double height) { this.width = width; this.height = height; } // Returns the area of this rectangle. public double getArea() { return width * height; } // Returns the perimeter of this rectangle. public double getPerimeter() { return 2.0 * (width + height); } } 7KH RWKHU FODVVHV RI VKDSHV DUH LPSOHPHQWHG LQ D VLPLODU IDVKLRQ :H OO GHILQH D &LUFOH REMHFW WR KDYH D ILHOG QDPHG UDGLXV 1RWLFH WKDW ZH GRQ W KDYH DQ\ FRPPRQ FRGH EHWZHHQ &LUFOH DQG 5HFWDQJOH VR LQKHULWDQFH LV XQQHFHVVDU\ +HUH LV WKH FRPSOHWH &LUFOH FODVV 507 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Represents circle shapes. public class Circle implements Shape { private double radius; // Constructs a new circle with the given radius. public Circle(double radius) { this.radius = radius; } // Returns the area of this circle. public double getArea() { return Math.PI * radius * radius; } // Returns the perimeter of this circle. public double getPerimeter() { return 2.0 * Math.PI * radius; } } $V IRU LPSOHPHQWLQJ WKH 7ULDQJOH ZH OO VSHFLI\ WKH VKDSH E\ LWV WKUHH VLGH OHQJWKV D E DQG F 7KH SHULPHWHU RI WKH WULDQJOH LV VLPSO\ WKH VXP RI WKH WKUHH VLGH OHQJWKV 7KH JHW$UHD PHWKRG LV D ELW WULFNLHU 7KHUH LV D XVHIXO JHRPHWULF IRUPXOD QDPHG +HURQ V IRUPXOD ZKLFK VD\V WKDW WKH DUHD RI D WULDQJOH ZLWK VLGHV RI OHQJWKV LV UHODWHG WR D YDOXH V HTXDO WR KDOI WKH WULDQJOH V SHULPHWHU ZKHUH +HUH LV D FRPSOHWH YHUVLRQ RI WKH 7ULDQJOH FODVV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // Represents triangle shapes. public class Triangle implements Shape { private double a; private double b; private double c; // Constructs a new Triangle with sides of the given lengths. public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } // Returns the area of this triangle using Heron's formula. public double getArea() { double s = (a + b + c) / 2.0; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } // Returns the perimeter of this triangle. public double getPerimeter() { return a + b + c; } } 508 %HQHILWV RI ,QWHUIDFHV &ODVVHV WKDW LPSOHPHQW D FRPPRQ LQWHUIDFH IRUP D W\SH KLHUDUFK\ VLPLODU WR WKRVH FUHDWHG E\ LQKHULWDQFH 7KH LQWHUIDFH VHUYHV DV D SDUHQW W\SH IRU WKH FODVVHV WKDW LPSOHPHQW LW $IWHU RXU PRGLILFDWLRQV WKH W\SH KLHUDUFK\ ORRNV OLNH WKLV 7KH PDMRU EHQHILW RI LQWHUIDFHV LV WKDW ZH FDQ XVH WKHP WR DFKLHYH SRO\PRUSKLVP :H FDQ QRZ FUHDWH DQ DUUD\ RI 6KDSHV SDVV D 6KDSH DV D SDUDPHWHU WR D PHWKRG UHWXUQ D 6KDSH IURP D PHWKRG DQG VR RQ 7KH IROORZLQJ SURJUDP XVHV WKH VKDSH FODVVHV LQ DQ H[DPSOH SURJUDP VLPLODU WR WKH SRO\PRUSKLVP H[HUFLVHV LQ 6HFWLRQ 1 2 3 4 5 6 7 8 9 10 11 12 13 public class ShapesMain { public static void main(String args) { Shape shapes = new Shape[3]; shapes[0] = new Rectangle(18, 18); shapes[1] = new Triangle(30, 30, 30); shapes[2] = new Circle(12); for (int i = 0; i < shapes.length; i++) { System.out.println("area=" + shapes[i].getArea() + ", perimeter=" + shapes[i].getPerimeter()); } } } 7KLV SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW area=324.0, perimeter=72.0 area=389.7114317029974, perimeter=90.0 area=452.3893421169302, perimeter=75.39822368615503 ,W PD\ VHHP RGG WKDW ZH FDQ KDYH LQWHUIDFH YDULDEOHV DUUD\V DQG SDUDPHWHUV ZKHQ LW LVQ W SRVVLEOH WR FRQVWUXFW DQ REMHFW RI DQ LQWHUIDFH W\SH ,W VLPSO\ PHDQV WKDW DQ\ REMHFW RI D W\SH WKDW LPSOHPHQWV WKDW LQWHUIDFH PD\ EH XVHG ,Q RXU FDVH DQ\ W\SH WKDW LPSOHPHQWV 6KDSH VXFK DV &LUFOH 5HFWDQJOH RU 7ULDQJOH PD\ EH XVHG 509 $OVR UHFDOO WKDW LQWHUIDFHV KHOS XV FRSH ZLWK WKH OLPLWDWLRQV RI VLQJOH LQKHULWDQFH $ FODVV PD\ RQO\ H[WHQG RQH VXSHUFODVV EXW PD\ LPSOHPHQW DUELWUDULO\ PDQ\ LQWHUIDFHV 7KH IROORZLQJ LV WKH D JHQHUDO V\QWD[ IRU KHDGHUV RI FODVVHV WKDW H[WHQG D VXSHUFODVV DQG LPSOHPHQW RQH RU PRUH LQWHUIDFHV public class <name> extends <superclass name> implements <interface name>, <interface name>, ..., <interface name> { ... } 7KHUH DUH PDQ\ FODVVHV LQ WKH -DYD FODVV OLEUDULHV WKDW ERWK H[WHQG D VXSHUFODVV DQG LPSOHPHQW RQH RU PRUH LQWHUIDFHV )RU H[DPSOH WKH 3ULQW6WUHDP FODVV RI ZKLFK 6\VWHP RXW LV DQ LQVWDQFH KDV WKH IROORZLQJ KHDGHU public class PrintStream extends FilterOutputStream implements Appendable, Closeable, Flushable ,QWHUIDFHV LQ WKH -DYD &ODVV /LEUDULHV ,QWHUIDFHV DUH XVHG LQ PDQ\ RWKHU SODFHV LQ -DYD V FODVV OLEUDULHV +HUH DUH MXVW D IHZ RI -DYD V LPSRUWDQW LQWHUIDFHV 7KH ActionListener LQWHUIDFH LQ WKH java.awt SDFNDJH LV XVHG WR DVVLJQ EHKDYLRU WR HYHQWV ZKHQ D XVHU FOLFNV RQ D EXWWRQ RU RWKHU JUDSKLFDO FRQWURO 7KH Serializable LQWHUIDFH LQ WKH java.io SDFNDJH GHQRWHV FODVVHV ZKRVH REMHFWV DUH DEOH WR EH VDYHG WR ILOHV DQG WUDQVIHUUHG RYHU D QHWZRUN 7KH Comparable LQWHUIDFH DOORZV \RX WR GHVFULEH KRZ WR FRPSDUH REMHFWV RI \RXU W\SH WR VHH ZKLFK DUH OHVV WKDQ JUHDWHU RU HTXDO WR HDFK RWKHU 7KLV FDQ EH XVHG WR VHDUFK RU VRUW D FROOHFWLRQ RI \RXU REMHFWV 7KH Formattable LQWHUIDFH OHWV REMHFWV GHVFULEH GLIIHUHQW ZD\V WKH\ FDQ EH SULQWHG E\ WKH System.out.printf FRPPDQG 7KH Runnable LQWHUIDFH LV XVHG IRU PXOWL WKUHDGLQJ ZKLFK DOORZV D SURJUDP WR H[HFXWH WZR SLHFHV RI FRGH DW WKH VDPH WLPH ,QWHUIDFHV VXFK DV List Set Map DQG Iterator LQ WKH java.util SDFNDJH GHVFULEH GDWD VWUXFWXUHV \RX FDQ XVH WR VWRUH FROOHFWLRQV RI REMHFWV :H ZLOO FRYHU VRPH RI WKH SUHFHGLQJ LQWHUIDFHV LQ ODWHU FKDSWHUV 510 &DVH 6WXG\ 'HVLJQLQJ D +LHUDUFK\ RI )LQDQFLDO &ODVVHV $V \RX ZULWH ODUJHU DQG PRUH FRPSOH[ SURJUDPV \RX ZLOO HQG XS ZLWK PRUH FODVVHV DQG PRUH RSSRUWXQLWLHV WR XVH LQKHULWDQFH DQG LQWHUIDFHV ,W LV LPSRUWDQW WR JHW SUDFWLFH GHYLVLQJ VHQVLEOH KLHUDUFKLHV RI W\SHV VR WKDW \RX ZLOO EH DEOH WR VROYH ODUJH SUREOHPV E\ EUHDNLQJ WKHP GRZQ LQWR JRRG FODVVHV LQ WKH IXWXUH :KHQ GHVLJQLQJ DQ REMHFW RULHQWHG V\VWHP \RX VKRXOG DVN \RXUVHOI WKH IROORZLQJ TXHVWLRQV :KDW FODVVHV RI REMHFWV VKRXOG , ZULWH" :KDW EHKDYLRU GRHV WKH FOLHQW ZLVK HDFK RI WKHVH REMHFWV WR KDYH" :KDW GDWD GR WKH REMHFWV QHHG WR VWRUH LQ RUGHU WR LPSOHPHQW WKLV EHKDYLRU" $UH WKH FODVVHV UHODWHG" ,I VR ZKDW LV WKH QDWXUH RI WKH UHODWLRQVKLSV" +DYLQJ JRRG DQVZHUV WR WKH DERYH TXHVWLRQV DORQJ ZLWK D JRRG NQRZOHGJH RI WKH QHFHVVDU\ -DYD V\QWD[ LV D JRRG VWDUW WRZDUG GHVLJQLQJ DQ REMHFW RULHQWHG V\VWHP 6XFK D SURFHVV LV FDOOHG REMHFW RULHQWHG GHVLJQ 2EMHFW 2ULHQWHG 'HVLJQ 22' 0RGHOLQJ D SURJUDP RU V\VWHP DV D FROOHFWLRQ RI FRRSHUDWLQJ REMHFWV DQG LQGLYLGXDO REMHFWV ZKLFK DUH WUHDWHG DV LQVWDQFHV RI FODVVHV ZLWKLQ FODVV KLHUDUFKLHV /HW V FRQVLGHU WKH SUREOHP RI JDWKHULQJ LQIRUPDWLRQ DERXW D SHUVRQ V ILQDQFLDO LQYHVWPHQWV :H DOUHDG\ KDYH H[SORUHG D 6WRFN H[DPSOH LQ WKLV FKDSWHU DQG WKH SUHYLRXV FKDSWHU 7KHUH DUH DOVR VWRFNV WKDW SD\ GLYLGHQGV ZKLFK RXU 'LYLGHQG6WRFN ZDV ZULWWHQ WR KDQGOH %XW 6WRFNV DUH QRW WKH RQO\ W\SH RI ILQDQFLDO DVVHW WKDW LQYHVWRUV PLJKW KDYH LQ WKHLU ILQDQFLDO SRUWIROLRV 2WKHU LQYHVWPHQWV PLJKW LQFOXGH PXWXDO IXQGV UHDO HVWDWH RU FDVK +RZ VKRXOG ZH GHVLJQ WKLV V\VWHP" :KDW QHZ W\SHV RI REMHFWV VKRXOG ZH ZULWH" 7DNH D PRPHQW WR FRQVLGHU LW \RXUVHOI :H OO GLVFXVV DQ H[DPSOH GHVLJQ QH[W &ODVV 'HVLJQ (DFK W\SH RI DVVHW GHVHUYHV LWV RZQ FODVV :H KDYH WKH 6WRFN FODVV IURP WKH ODVW FKDSWHU DQG LWV 'LYLGHQG6WRFN VXEFODVV IURP HDUOLHU LQ WKLV FKDSWHU :H FDQ DGG FODVVHV OLNH 0XWXDO)XQG DQG &DVK (DFK REMHFW RI HDFK RI WKHVH W\SHV ZLOO UHSUHVHQW D VLQJOH LQYHVWPHQW RI WKDW W\SH IRU H[DPSOH D 0XWXDO)XQG REMHFW ZLOO UHSUHVHQW D SXUFKDVH RI D PXWXDO IXQG DQG D &DVK REMHFW ZLOO UHSUHVHQW D VXP RI PRQH\ LQ WKH XVHU V SRUWIROLR :KDW GDWD DQG EHKDYLRU LV QHFHVVDU\ LQ HDFK RI WKHVH W\SHV RI REMHFWV" 7DNH D PRPHQW WR FRQVLGHU LW 511 ,Q WHUPV RI EHKDYLRU WKRXJK HDFK W\SH RI DVVHW LV XQLTXH WKH W\SHV GR KDYH VRPH FRPPRQ EHKDYLRU (DFK DVVHW VKRXOG EH DEOH WR FRPSXWH LWV FXUUHQW PDUNHW YDOXH DQG SURILW LI DQ\ 7KHVH YDOXHV DUH FRPSXWHG LQ GLIIHUHQW ZD\V IRU GLIIHUHQW DVVHW W\SHV $ VWRFN V PDUNHW YDOXH LV WKH WRWDO QXPEHU RI VKDUHV SXUFKDVHG WLPHV WKH FXUUHQW SULFH SHU VKDUH 5HDO HVWDWH V PDUNHW YDOXH PLJKW GHSHQG RQ WKH FXUUHQW LQWHUHVW UDWH &DVK LV DOZD\V ZRUWK H[DFWO\ LWV RZQ DPRXQW ,Q WHUPV RI GDWD ZH GHFLGHG LQ WKH SUHYLRXV FKDSWHU WKDW D 6WRFN REMHFW VKRXOG VWRUH WKH VWRFN V V\PERO WKH QXPEHU RI VKDUHV WKH WRWDO FRVW SDLG IRU DOO VKDUHV DQG WKH FXUUHQW SULFH RI WKH VWRFN 'LYLGHQG VWRFNV DOVR QHHG WR VWRUH DQ DPRXQW RI GLYLGHQGV SDLG $ 0XWXDO)XQG REMHFW VWRUHV WKH VDPH EXW PXWXDO IXQGV FDQ KROG SDUWLDO VKDUHV &DVK RQO\ QHHGV WR VWRUH LWV DPRXQW :H PLJKW XSGDWH RXU GLDJUDP RI W\SHV WR UHIOHFW WKH SUHFHGLQJ GDWD DQG EHKDYLRU $UH WKH DVVHW W\SHV UHODWHG" ,W VHHPV VR 3HUKDSV ZH G ZDQW WR JDWKHU DQG VWRUH D SHUVRQ V SRUWIROLR RI DVVHWV LQ DQ DUUD\ ,W ZRXOG EH FRQYHQLHQW WR EH DEOH WR WUHDW DQ\ DVVHW WKH VDPH ZD\ LQVRIDU DV WKH\ VKDUH VLPLODU IXQFWLRQDOLW\ )RU H[DPSOH HYHU\ DVVHW KDV D PDUNHW YDOXH VR LW ZRXOG EH QLFH WR FRPSXWH WKH VXP PDUNHW YDOXH RI DOO DVVHWV LQ DQ LQYHVWRU V SRUWIROLR DUUD\ ZLWKRXW ZRUU\LQJ DERXW WKH H[DFW W\SH RI HDFK DVVHW 2QH KXUGOH LV WKDW GLIIHUHQW DVVHWV FRPSXWH WKHLU PDUNHW YDOXH LQ GLIIHUHQW ZD\V 6WRFNV YDOXHV DUH EDVHG RQ WKH FXUUHQW VKDUH SULFH ZKLOH WKH YDOXH RI FDVK GHSHQGV RQO\ RQ KRZ PXFK FDVK WKH SHUVRQ KDV 7KLV LPSOLHV WKDW ZH VKRXOG XVH DQ LQWHUIDFH WR UHSUHVHQW WKH QRWLRQ RI DQ DVVHW DQG KDYH HYHU\ FODVV SUHYLRXVO\ QDPHG LPSOHPHQW WKH DVVHW LQWHUIDFH 2XU LQWHUIDFH ZLOO GHPDQG WKDW DOO DVVHWV KDYH PHWKRGV WR JHW WKH PDUNHW YDOXH DQG SURILW 7KH LQWHUIDFH LV D ZD\ RI VD\LQJ FODVVHV WKDW ZDQW WR FRQVLGHU WKHPVHOYHV DVVHWV PXVW KDYH D JHW0DUNHW9DOXH DQG JHW3URILW PHWKRG 2XU LQWHUIDFH IRU ILQDQFLDO DVVHWV ZRXOG EH VDYHG LQWR D ILOH QDPHG $VVHW MDYD DQG PLJKW ORRN OLNH WKLV 1 2 3 4 5 6 7 8 // This interface represents financial assets that investors hold. public interface Asset { // how much the asset is worth public double getMarketValue(); // how much money has been made on this asset public double getProfit(); } :H OO KDYH RXU YDULRXV FODVVHV FHUWLI\ WKDW WKH\ DUH $VVHWV E\ PDNLQJ WKHP LPSOHPHQW WKH $VVHW LQWHUIDFH )RU H[DPSOH OHW V ORRN DW WKH &DVK FODVV :H GLGQ W ZULWH D JHW3URILW PHWKRG LQ RXU SUHYLRXV GLDJUDP RI &DVK EHFDXVH FDVK GRHVQ W UHDOO\ FKDQJH YDOXH DQG WKHUHIRUH GRHVQ W KDYH D SURILW %XW ZH FDQ MXVW ZULWH D JHW3URILW IRU &DVK WKDW UHWXUQV WR LQGLFDWH QR SURILW 512 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Represents an amount of money held by an investor. public class Cash implements Asset { private double amount; // amount of money held // Constructs a cash investment of the given amount. public Cash(double amount) { this.amount = amount; } // Returns this cash investment's market value, which is // equal to the amount of cash. public double getMarketValue() { return amount; } // Since cash is a fixed asset, it never has a profit or loss. public double getProfit() { return 0.00; } // Sets the amount of cash invested to the given value. public void setAmount(double amount) { this.amount = amount; } } $V GLVFXVVHG HDUOLHU LQ WKH FKDSWHU D 'LYLGHQG6WRFN LV YHU\ VLPLODU WR D QRUPDO 6WRFN RQO\ ZLWK D VPDOO DPRXQW RI EHKDYLRU DGGHG /HW V GLVSOD\ 'LYLGHQG6WRFN DV D VXEFODVV RI 6WRFN WKURXJK LQKHULWDQFH VR WKDW FRGH ZRQ W EH UHGXQGDQW 2XU W\SH KLHUDUFK\ ZRXOG QRZ ORRN OLNH WKLV 513 :KDW DERXW WKH VLPLODULW\ EHWZHHQ PXWXDO IXQGV DQG VWRFNV" 7KH\ ERWK VWRUH DVVHWV EDVHG RQ VKDUHV ZLWK D V\PERO WRWDO FRVW DQG FXUUHQW SULFH ,W ZRXOGQ W ZRUN YHU\ ZHOO WR PDNH RQH RI WKHP WKH VXEFODVV RI WKH RWKHU EHFDXVH WKH W\SH RI VKDUHV LQWHJHU RU UHDO QXPEHU LVQ W WKH VDPH DQG DOVR EHFDXVH LW V QRW D VHQVLEOH LV D UHODWLRQVKLS VWRFNV DUHQ W UHDOO\ PXWXDO IXQGV DQG YLFH YHUVD /HW V WU\ D GLIIHUHQW DOWHUQDWLYH E\ PDNLQJ D QHZ FRPPRQ VXSHUFODVV QDPHG 6KDUH$VVHW UHSUHVHQWLQJ DQ\ DVVHW WKDW KDV VKDUHV WKDW FRQWDLQV WKH FRPPRQ EHKDYLRU RI 6WRFN DQG 0XWXDO)XQG 7KHQ ZH FDQ KDYH ERWK 6WRFN DQG 0XWXDO)XQG H[WHQG 6KDUH$VVHW WR UHGXFH UHGXQGDQF\ ,Q RUGHU WR LPSOHPHQW WKH SDUDPHWHUOHVV JHW0DUNHW9DOXH PHWKRG IURP WKH $VVHW LQWHUIDFH ZH OO DGG WKH FXUUHQW SULFH DV D GDWD ILHOG DORQJ ZLWK PHWKRGV WR JHW DQG VHW LWV YDOXH ,QLWLDO 5HGXQGDQW ,PSOHPHQWDWLRQ +HUH V VRPH SRWHQWLDO FRGH IRU WKH 6KDUH$VVHW FODVV 514 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Represents a general asset that has a symbol and holds shares. public class ShareAsset { private String symbol; private double totalCost; private double currentPrice; // Constructs a new share asset with the given symbol and current price. public ShareAsset(String symbol, double currentPrice) { this.symbol = symbol; this.currentPrice = currentPrice; totalCost = 0.0; } // Adds a cost of the given amount to this asset. public void addCost(double cost) { totalCost += cost; } // Returns the given price per share of this asset. public double getCurrentPrice() { return currentPrice; } // Returns this asset's total cost. public double getTotalCost() { return totalCost; } // Sets the current share price of this asset to the given amount. public void setCurrentPrice(double currentPrice) { this.currentPrice = currentPrice; } } :H VWROH VRPH FRGH IURP ODVW FKDSWHU V 6WRFN FODVV EXW WR ILW WKLV LQWHUIDFH WKH FRGH XQGHUZHQW D IHZ FKDQJHV 2XU ROG 6WRFN FRGH DVNHG IRU WKH FXUUHQW VKDUH SULFH DV D SDUDPHWHU WR LWV JHW3URILW PHWKRG 6LQFH WKH JHW3URILW PHWKRG FDQQRW DFFHSW DQ\ SDUDPHWHUV LI ZH ZLVK WR LPSOHPHQW WKH LQWHUIDFH ZH OO LQVWHDG VWRUH WKH FXUUHQW VKDUH SULFH DV D GDWD ILHOG LQ WKH 6KDUH$VVHW REMHFW :H VXSSO\ D VHW&XUUHQW3ULFH PXWDWRU PHWKRG WKDW FDQ EH FDOOHG WR VHW LWV SURSHU YDOXH :H DOVR JLYH D FRQVWUXFWRU WKDW FDQ LQLWLDOL]H ZLWK DQ\ QXPEHU RI VKDUHV DQG WRWDO FRVW 2QH ODVW PRGLILFDWLRQ ZH PDGH WR 6KDUH$VVHW ZDV WR LQFOXGH DQ DGG&RVW PHWKRG ZKLFK ZH OO XVH WR DGG D JLYHQ DPRXQW WR WKH DVVHW V WRWDO FRVW :H ZLOO QHHG WKLV EHFDXVH SXUFKDVHV RQ 6WRFNV DQG 0XWXDO)XQGV QHHG WR XSGDWH WKH WRWDO&RVW GDWD ILHOG EXW WKH\ FDQQRW GR VR GLUHFWO\ VLQFH LW LV SULYDWH 7KH 6WRFN FODVV FDQ QRZ H[WHQG 6KDUH$VVHW WR LPSOHPHQW LWV UHPDLQLQJ IXQFWLRQDOLW\ 1RWLFH WKDW ZH ERWK H[WHQG 6KDUH$VVHW DQG LPSOHPHQW WKH $VVHW LQWHUIDFH 515 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // A Stock object represents purchases of shares of a particular stock. public class Stock extends ShareAsset implements Asset { private int totalShares; // Constructs a new Stock with the given symbol and current // price per share. public Stock(String symbol, double currentPrice) { super(symbol, currentPrice); // call Asset c'tor totalShares = 0; } // Returns the market value of this stock, which is // equal to the number of total shares times the share price. public double getMarketValue() { return totalShares * getCurrentPrice(); } // Returns the total number of shares purchased. public int getTotalShares() { return totalShares; } // Returns the profit made on this stock. public double getProfit() { return getMarketValue() - getTotalCost(); } // Records a purchase of the given number of shares of // stock at the given price per share. public void purchase(int shares, double pricePerShare) { totalShares += shares; addCost(shares * pricePerShare); } } 7KH 0XWXDO)XQG FODVV UHFHLYHV D VLPLODU WUHDWPHQW EXW ZLWK D GRXEOH IRU LWV WRWDO VKDUHV 7KH WZR FODVVHV DUH DZIXOO\ UHGXQGDQW ZH OO LPSURYH WKHP LQ WKH QH[W VHFWLRQ 516 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // Represents a mutual fund asset. public class MutualFund extends ShareAsset implements Asset { private double totalShares; // Constructs a new MutualFund investment with the given // symbol and price per share. public MutualFund(String symbol, double currentPrice) { super(symbol, currentPrice); // call Asset constructor totalShares = 0.0; } // Returns the market value of this mutual fund, which // is equal to the number of shares times the price per share. public double getMarketValue() { return totalShares * getCurrentPrice(); } // Returns the number of shares of this mutual fund. public double getTotalShares() { return totalShares; } // Returns the profit made on this mutual fund. public double getProfit() { return getMarketValue() - getTotalCost(); } // Records a purchase of the given number of shares for the // given price per share. public void purchase(double shares, double pricePerShare) { totalShares += shares; addCost(shares * pricePerShare); } } 7KH 'LYLGHQG6WRFN VLPSO\ DGGV DQ DPRXQW RI GLYLGHQG SD\PHQWV WR D QRUPDO 6WRFN ZKLFK DIIHFWV LWV PDUNHW YDOXH 7KH JHW3URILW PHWKRG GRHVQ W QHHG WR EH RYHUULGGHQ LQ 'LYLGHQG6WRFN EHFDXVH 'LYLGHQG6WRFN DOUHDG\ LQKHULWV D JHW3URILW PHWKRG ZLWK WKH IROORZLQJ ERG\ return getMarketValue() - getTotalCost(); 1RWLFH WKDW JHW3URILW V ERG\ FDOOV JHW0DUNHW9DOXH 6LQFH ZH RYHUURGH WKH JHW0DUNHW9DOXH PHWKRG LQ 'LYLGHQG6WRFN D FRQYHQLHQW VLGH HIIHFW LV WKDW DQ\ RWKHU PHWKRG WKDW FDOOV JHW0DUNHW9DOXH VXFK DV JHW3URILW ZLOO DOVR EHKDYH GLIIHUHQWO\ 7KLV RFFXUV EHFDXVH RI SRO\PRUSKLVP VLQFH JHW0DUNHW9DOXH LV RYHUULGGHQ JHW3URILW FDOOV WKH QHZ YHUVLRQ RI WKH PHWKRG 7KH SURILW ZLOO EH FRUUHFWO\ FRPSXWHG ZLWK GLYLGHQGV EHFDXVH WKHVH DUH DGGHG WR WKH PDUNHW YDOXH 7KH IROORZLQJ FRGH LPSOHPHQWV WKH 'LYLGHQG6WRFN FODVV 517 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // A DividendStock object models shares of stocks with dividend payments. public class DividendStock extends Stock { private double dividends; // amount of dividends paid // Constructs a new DividendStock with the given symbol and price. public DividendStock(String symbol, double currentPrice) { super(symbol, currentPrice); // call Stock constructor dividends = 0.00; } // Returns this DividendStock's market value, which is // a normal stock's market value plus any dividends. public double getMarketValue() { return super.getMarketValue() + dividends; } // Records a dividend payment of the given amount. public void payDividend(double amountPerShare) { dividends += amountPerShare * getTotalShares(); } } $EVWUDFW &ODVVHV ,I LQKHULWDQFH OHWV XV VSHFLI\ FODVVHV DV SDUHQWV DQG FKLOGUHQ LQWHUIDFHV PD\ EH OLNH GLVWDQW FRXVLQV UHODWHG EXW ZLWKRXW PDQ\ GHWDLOV LQ FRPPRQ 7KHUH DUH VLWXDWLRQV ZKHUH ZH ZDQW PRUH RI D VLEOLQJ UHODWLRQVKLS FODVVHV ZLWK FRPPRQ PHWKRG QDPHV OLNH ZH JHW ZLWK LQWHUIDFHV EXW DOVR ZLWK VRPH FRGH VKDUHG EHWZHHQ WKHP OLNH ZH JHW ZLWK LQKHULWDQFH 2QH WKLQJ \RX PD\ QRWLFH DERXW WKH 6WRFN DQG 0XWXDO)XQG FRGH LQ WKH ODVW VHFWLRQ LV WKDW WKHUH LV VWLOO D ORW RI UHGXQGDQF\ )RU H[DPSOH WKH JHW0DUNHW9DOXH DQG JHW3URILW PHWKRGV ZKLOH WKH\ KDYH LGHQWLFDO FRGH FDQ W EH PRYHG XS LQWR WKH 6KDUH$VVHW VXSHUFODVV EHFDXVH WKH\ GHSHQG RQ WKH QXPEHU RI VKDUHV ZKLFK LV GLIIHUHQW LQ HDFK FKLOG FODVV :H G OLNH WR JHW ULG RI WKH UHGXQGDQF\ VRPHKRZ 7KHUH LV DQRWKHU SUREOHP $ 6KDUH$VVHW LVQ W UHDOO\ D W\SH RI DVVHW WKDW D SHUVRQ FDQ EX\ LW V MXVW D FRQFHSW WKDW KDSSHQV WR EH UHSUHVHQWHG LQ RXU FRGH ,W ZRXOG EH XQGHVLUDEOH LI D SHUVRQ DFWXDOO\ WULHG WR FRQVWUXFW D 6KDUH$VVHW REMHFW ZH RQO\ ZURWH WKDW FODVV WR HOLPLQDWH UHGXQGDQF\ QRW WR EH XVHG GLUHFWO\ ,I ZH ZDQW WR FUHDWH FODVVHV WKDW VKRXOGQ W EH XVHG GLUHFWO\ E\ RXWVLGH FRGH EXW VKRXOG RQO\ VHUYH DV D VXSHUFODVV IRU LQKHULWDQFH ZH FDQ GHVLJQDWH WKH FODVV DV DEVWUDFW :ULWLQJ abstract LQ WKH FODVV V KHDGHU ZLOO VSHFLI\ WKDW D FODVV FDQQRW EH XVHG GLUHFWO\ WR FRQVWUXFW REMHFWV public abstract class ShareAsset implements Asset { ... } $Q DEVWUDFW FODVV LV RQH WKDW FDSWXUHV FRPPRQ FRGH WR DYRLG UHGXQGDQF\ ZLWKRXW LQWURGXFLQJ D QHZ LQVWDQWLDEOH W\SH LQWR D V\VWHP $Q DWWHPSW WR FUHDWH D 6KDUH$VVHW REMHFW ZLOO SURGXFH D FRPSLOHU HUURU VXFK DV WKH IROORZLQJ LI 6KDUH$VVHW ZDV GHFODUHG WR EH DEVWUDFW 518 C:\document\StockManager.java:55: ShareAsset is abstract; cannot be instantiated ShareAsset asset = new ShareAsset("MSFT", 20, 27.46); ^ 1 error $EVWUDFW &ODVV $ -DYD FODVV WKDW VHUYHV RQO\ DV D VXSHUFODVV IRU RWKHU FODVVHV EXW FDQQRW DFWXDOO\ EH XVHG WR FRQVWUXFW REMHFWV RI LWV W\SH $ LQWHUHVWLQJ WKLQJ DERXW DEVWUDFW FODVVHV LV WKDW WKH\ FDQ FODLP WR LPSOHPHQW DQ LQWHUIDFH ZLWKRXW DFWXDOO\ ZULWLQJ DOO RI WKH PHWKRGV LQ WKDW LQWHUIDFH 7KLV SDVVHV WKH EXFN WR DQ\ VXEFODVV WKDW H[WHQGV WKH DEVWUDFW FODVV UHTXLULQJ WKH VXEFODVV WR ILQLVK ZULWLQJ WKH UHPDLQLQJ PHWKRGV $ VWUDQJH EHQHILW RI WKLV LV WKDW WKH DEVWUDFW FODVV FDQ DFWXDOO\ FDOO DQ\ RI WKH LQWHUIDFH V PHWKRGV RQ LWVHOI HYHQ LI WKH\ GRQ W H[LVW LQ WKDW ILOH EHFDXVH LW FDQ FRXQW RQ LWV VXEFODVVHV WR ZULWH WKH UHPDLQLQJ PHWKRGV )RU H[DPSOH LI ZH PDNH 6KDUH$VVHW DEVWUDFW DQG GHFODUH WKDW LW LPSOHPHQWV $VVHW WKH KHDGHU ORRNV OLNH WKH IROORZLQJ public abstract class ShareAsset implements Asset { 7KH JHQHUDO V\QWD[ IRU GHFODULQJ DQ DEVWUDFW FODVV LV WKH IROORZLQJ public abstract class <name> { ... } 1RZ WKDW 6KDUH$VVHW LV DQ $VVHW ZH FDQ PRYH WKH FRPPRQ UHGXQGDQW JHW3URILW FRGH XS WR 6KDUH$VVHW DQG RXW RI 6WRFN DQG 0XWXDO)XQG 6KDUH$VVHW REMHFWV FDQ QRZ FDOO JHW0DUNHW9DOXH HYHQ WKRXJK WKDW PHWKRG LVQ W SUHVHQW LQ 6KDUH$VVHW 7KH FRGH FRPSLOHV EHFDXVH WKH FRPSLOHU NQRZV WKDW ZKDWHYHU FODVV H[WHQGV 6KDUH$VVHW ZLOO KDYH WR LPSOHPHQW JHW0DUNHW9DOXH // This method calls an abstract getMarketValue method // (the subclass will provide its implementation) public double getProfit() { return getMarketValue() - totalCost; } ,I QHFHVVDU\ DEVWUDFW FODVVHV FDQ DOVR EHKDYH OLNH LQWHUIDFHV DQG FDQ GHPDQG WKDW FHUWDLQ PHWKRGV EH LPSOHPHQWHG E\ DOO RI WKHLU VXEFODVVHV 6XFK UHTXLUHG VXEFODVV EHKDYLRUV DUH FDOOHG DEVWUDFW PHWKRGV ,I ZH KDGQ W KDG WKH $VVHW LQWHUIDFH EXW VWLOO ZDQWHG HYHU\ 6KDUH$VVHW VXEFODVV WR KDYH D JHW0DUNHW9DOXH PHWKRG ZH FRXOG KDYH GHFODUHG DQ DEVWUDFW JHW0DUNHW9DOXH PHWKRG public abstract double getMarketValue(); $Q DEVWUDFW PHWKRG LV GHFODUHG ZLWK D KHDGHU VLPLODU WR UHJXODU PHWKRGV EXW ZLWK WKH NH\ZRUG abstract DIWHU WKH SXEOLF PRGLILHU 7KH RWKHU GLIIHUHQFH DERXW DEVWUDFW PHWKRGV LV WKDW WKH\ GR QRW KDYH PHWKRG ERGLHV WKH\ RQO\ KDYH D VHPLFRORQ ZKLFK PHDQV WKDW WKH EHKDYLRU LV VSHFLILHG LQ HDFK VXEFODVV $Q DEVWUDFW FODVV FDQ FRQWDLQ ERWK DEVWUDFW DQG QRQ DEVWUDFW PHWKRGV 7HFKQLFDOO\ 519 HYHU\ PHWKRG LQ DQ LQWHUIDFH LV DEVWUDFW DV ZHOO 7KH PHWKRGV LQ DQ LQWHUIDFH FDQ EH GHFODUHG ZLWK WKH abstract NH\ZRUG LI VR GHVLUHG EXW WKH SUHVHQFH RU DEVHQFH RI WKH DEVWUDFW NH\ZRUG KDV QR HIIHFW 7KH JHQHUDO V\QWD[ IRU DEVWUDFW PHWKRG GHFODUDWLRQV LV WKH IROORZLQJ public abstract <type> <name>(<type> <name>, ..., <type> <name>); %HFDXVH DQ DEVWUDFW FODVV FDQ FRQWDLQ QRUPDO GDWD ILHOGV DQG PHWKRGV ZLWK LPSOHPHQWDWLRQ DQG LW FDQ DOVR FRQWDLQ DEVWUDFW PHWKRGV ZLWKRXW LPSOHPHQWDWLRQ DEVWUDFW FODVVHV FDQ EH WKRXJKW RI DV K\EULGV EHWZHHQ FODVVHV DQG LQWHUIDFHV 2QH LPSRUWDQW GLIIHUHQFH EHWZHHQ LQWHUIDFHV DQG DEVWUDFW FODVVHV LV WKDW D FODVV PD\ FKRRVH WR LPSOHPHQW DUELWUDULO\ PDQ\ LQWHUIDFHV EXW LW FDQ RQO\ H[WHQG RQH DEVWUDFW FODVV :H FDQ PRGLI\ WKH 6WRFN DQG 0XWXDO)XQG FODVVHV WR WDNH DGYDQWDJH RI 6KDUH$VVHW DQG UHGXFH WKH UHGXQGDQF\ 1RWLFH WKDW ZH PXVW LPSOHPHQW JHW0DUNHW9DOXH RU HOVH ZH UHFHLYH DQ HUURU +HUH DUH WKH ILQDO YHUVLRQV RI DOO WKUHH FODVVHV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // Represents a general asset that has a symbol and holds shares. public abstract class ShareAsset implements Asset { private String symbol; private double totalCost; private double currentPrice; // Constructs a new share asset with the given symbol and current price. public ShareAsset(String symbol, double currentPrice) { this.symbol = symbol; this.currentPrice = currentPrice; totalCost = 0.0; } // Adds a cost of the given amount to this asset. public void addCost(double cost) { totalCost += cost; } // Returns the given price per share of this asset. public double getCurrentPrice() { return currentPrice; } // This method calls an abstract getMarketValue method // (the subclass will provide its implementation) public double getProfit() { return getMarketValue() - totalCost; } // Returns this asset's total cost. public double getTotalCost() { return totalCost; } 520 35 36 37 38 39 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Sets the current share price of this asset to the given amount. public void setCurrentPrice(double currentPrice) { this.currentPrice = currentPrice; } } // A Stock object represents purchases of shares of a particular stock. public class Stock extends ShareAsset implements Asset { private int totalShares; // Constructs a new Stock with the given symbol and current // price per share. public Stock(String symbol, double currentPrice) { super(symbol, currentPrice); // call Asset c'tor totalShares = 0; } // Returns the market value of this stock, which is // equal to the number of total shares times the share price. public double getMarketValue() { return totalShares * getCurrentPrice(); } // Returns the total number of shares purchased. public int getTotalShares() { return totalShares; } // Records a purchase of the given number of shares of // stock at the given price per share. public void purchase(int shares, double pricePerShare) { totalShares += shares; addCost(shares * pricePerShare); } } 521 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Represents a mutual fund asset. public class MutualFund extends ShareAsset { private double totalShares; // Constructs a new MutualFund investment with the given // symbol and price per share. public MutualFund(String symbol, double currentPrice) { super(symbol, currentPrice); // call Asset constructor totalShares = 0.0; } // Returns the market value of this mutual fund, which // is equal to the number of shares times the price per share. public double getMarketValue() { return totalShares * getCurrentPrice(); } // Returns the number of shares of this mutual fund. public double getTotalShares() { return totalShares; } // Records a purchase of the given number of shares for the // given price per share. public void purchase(double shares, double pricePerShare) { totalShares += shares; addCost(shares * pricePerShare); } } :H KDYH DFKLHYHG D VROLG REMHFW RULHQWHG GHVLJQ IRU WKHVH WULFN\ LQYHVWPHQW W\SHV ZKLFK FDQ QRZ EH XVHG E\ RWKHU FODVVHV WR PDQDJH WKH XVHU V ILQDQFHV &KDSWHU 6XPPDU\ ,QKHULWDQFH DOORZV WKH FUHDWLRQ RI D SDUHQW FKLOG UHODWLRQVKLS EHWZHHQ WZR W\SHV 7KH FKLOG FODVV RI DQ LQKHULWDQFH UHODWLRQVKLS FRPPRQO\ FDOOHG D VXEFODVV ZLOO UHFHLYH D FRS\ RI LQKHULW HYHU\ ILHOG DQG PHWKRG IURP WKH SDUHQW FODVV VXSHUFODVV 7KH VXEFODVV H[WHQGV WKH VXSHUFODVV EHFDXVH LW FDQ DGG QHZ ILHOGV DQG PHWKRGV WR WKH RQHV LW LQKHULWV IURP WKH VXSHUFODVV $ VXEFODVV FDQ RYHUULGH PHWKRGV IURP WKH VXSHUFODVV E\ ZULWLQJ LWV RZQ YHUVLRQ RI WKHP ZKLFK ZLOO UHSODFH WKH RQH WKDW ZDV LQKHULWHG 7UHDWLQJ REMHFWV RI GLIIHUHQW W\SHV LQWHUFKDQJHDEO\ LV FDOOHG SRO\PRUSKLVP 6XEFODVVHV FDQ UHIHU WR WKH VXSHUFODVV V FRQVWUXFWRUV RU PHWKRGV XVLQJ WKH super NH\ZRUG $Q LQWHUIDFH LV D OLVW RI PHWKRG GHFODUDWLRQV $Q LQWHUIDFH VSHFLILHV PHWKRG QDPHV SDUDPHWHUV DQG UHWXUQ W\SHV EXW GRHV QRW LQFOXGH WKH ERGLHV RI WKH PHWKRGV &ODVVHV FDQ SURPLVH WR LPSOHPHQW DOO RI WKH PHWKRGV LQ DQ LQWHUIDFH ,PSOHPHQWLQJ DQ LQWHUIDFH LV GRQH ZLWK WKH implements NH\ZRUG 7KH DGYDQWDJH RI LQWHUIDFHV LV WKDW ZH FDQ XVH WKHP WR DFKLHYH SRO\PRUSKLVP VR WKDW ZH FDQ WUHDW VHYHUDO GLIIHUHQW FODVVHV WKH VDPH ZD\ 522 ,I WZR RU PRUH FODVVHV ERWK LPSOHPHQW WKH VDPH LQWHUIDFH ZH FDQ XVH HLWKHU RI WKHP LQWHUFKDQJHDEO\ DQG FDOO DQ\ RI WKH LQWHUIDFH V PHWKRGV RQ WKHP $Q DEVWUDFW FODVV LV D FODVV WKDW FDQ QRW EH LQVWDQWLDWHG 1R REMHFWV RI WKH DEVWUDFW W\SH FDQ EH FRQVWUXFWHG $Q DEVWUDFW FODVV LV XVHIXO EHFDXVH LW FDQ EH XVHG DV D VXSHUFODVV DQG H[WHQGHG $Q DEVWUDFW FODVV FDQ FRQWDLQ DEVWUDFW PHWKRGV ZKLFK DUH GHFODUHG EXW GR QRW KDYH D ERG\ $OO VXEFODVVHV RI DQ DEVWUDFW FODVV PXVW LPSOHPHQW WKH DEVWUDFW VXSHUFODVV V DEVWUDFW PHWKRGV 6HOI &KHFN 3UREOHPV 6HFWLRQ ,QKHULWDQFH &RQFHSWV :KDW LV FRGH UHXVH" +RZ GRHV LQKHULWDQFH KHOS DFKLHYH FRGH UHXVH" 6HFWLRQ 3URJUDPPLQJ ZLWK ,QKHULWDQFH :KDW LV WKH GLIIHUHQFH EHWZHHQ RYHUORDGLQJ DQG RYHUULGLQJ D PHWKRG" &RQVLGHU WKH IROORZLQJ FODVVHV public class Vehicle { ... } public class Car extends Vehicle { ... } public class SUV extends Car { ... } :KLFK RI WKH IROORZLQJ DUH OHJDO VWDWHPHQWV" Vehicle v = new Car(); Vehicle v = new SUV(); Car c = new SUV(); SUV s = nev SUV(); SUV s = nev Car(); Car c = new Vehicle(); 6HFWLRQ 7KH 0HFKDQLFV RI 3RO\PRUSKLVP 8VLQJ WKH $ % & DQG ' FODVVHV IURP WKLV VHFWLRQ ZKDW LV WKH RXWSXW RI WKH IROORZLQJ FRGH IUDJPHQW" 523 public static void main(String args) { A elements = {new B(), new D(), new A(), new C()}; for (int i = 0; i < elements.length; i++) { elements[i].method2(); System.out.println(elements[i]); elements[i].method1(); System.out.println(); } } $VVXPH WKH IROORZLQJ FODVVHV KDYH EHHQ GHILQHG 1 2 3 4 5 6 7 8 9 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Flute extends Blue { public void method2() { System.out.println("flute 2"); } public String toString() { return "flute"; } } public class Blue extends Moo { public void method1() { System.out.println("blue 1"); } } public class Shoe extends Flute { public void method1() { System.out.println("shoe 1"); } } public class Moo { public void method1() { System.out.println("moo 1"); } public void method2() { System.out.println("moo 2"); } public String toString() { return "moo"; } } :KDW LV WKH RXWSXW SURGXFHG E\ WKH IROORZLQJ FRGH IUDJPHQW" 524 public static void main(String args) { Moo elements = {new Shoe(), new Flute(), new Moo(), new Blue()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } } 8VLQJ WKH VDPH FODVVHV IURP WKH SUHYLRXV SUREOHP ZKDW LV WKH RXWSXW SURGXFHG E\ WKH IROORZLQJ FRGH IUDJPHQW" public static void main(String args) { Moo elements = {new Blue(), new Moo(), new Shoe(), new Flute()}; for (int i = 0; i < elements.length; i++) { elements[i].method2(); elements[i].method1(); System.out.println(elements[i]); System.out.println(); } } $VVXPH WKH IROORZLQJ FODVVHV KDYH EHHQ GHILQHG 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } 525 1 2 3 4 5 6 7 8 9 public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } :KDW LV WKH RXWSXW SURGXFHG E\ WKH IROORZLQJ FRGH IUDJPHQW" public static void main(String args) { SeaCreature elements = {new Squid(), new Whale(), new SeaCreature(), new Mammal()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } } 8VLQJ WKH VDPH FODVVHV IURP WKH SUHYLRXV SUREOHP ZKDW LV WKH RXWSXW SURGXFHG E\ WKH IROORZLQJ FRGH IUDJPHQW" public static void main(String args) { SeaCreature elements = {new SeaCreature(), new Squid(), new Mammal(), new Whale()}; for (int i = 0; i < elements.length; i++) { elements[i].method2(); System.out.println(elements[i]); elements[i].method1(); System.out.println(); } } 6HFWLRQ ,QWHUDFWLQJ ZLWK WKH 6XSHUFODVV ([SODLQ WKH GLIIHUHQFH EHWZHHQ WKH this NH\ZRUG DQG WKH super NH\ZRUG :KHQ VKRXOG HDFK EH XVHG" &RQVLGHU WKH IROORZLQJ WZR FODVVHV 526 public class Student { private String name; private int age; ... public int setAge(int age) { this.age = age; } } public class Freshman extends Student { ... } &DQ WKH FRGH LQ WKH )UHVKPDQ FODVV DFFHVV WKH QDPH DQG DJH GDWD ILHOGV LW LQKHULWV IURP 6WXGHQW" &DQ LW FDOO WKH VHW$JH PHWKRG" $GG WKH IROORZLQJ FRQVWUXFWRU WR WKH 3RLQW ' FODVV VKRZQ LQ WKLV VHFWLRQ public Point3D() &RQVWUXFWV D QHZ ' SRLQW DW WKH RULJLQ RI NH\ZRUG DV SDUW RI \RXU VROXWLRQ 8VH WKH super NH\ZRUG RU WKH this 6HFWLRQ ,QWHUIDFHV :KDW LV WKH GLIIHUHQFH EHWZHHQ LPSOHPHQWLQJ DQ LQWHUIDFH DQG H[WHQGLQJ D FODVV" &RQVLGHU WKH IROORZLQJ LQWHUIDFH DQG FODVV public interface I { public void m1(); public void m2(); } public class C implements I { // code for class C } :KDW PXVW EH WUXH DERXW WKH FRGH IRU FODVV & LQ RUGHU IRU LW WR FRPSLOH VXFFHVVIXOO\" :KDW V ZURQJ ZLWK WKH FRGH IRU WKH IROORZLQJ LQWHUIDFH" :KDW VKRXOG EH FKDQJHG WR PDNH D YDOLG LQWHUIDFH IRU REMHFWV WKDW KDYH D FRORU" public interface Colored { private Color color; public Color getColor() { return this.color; } } 527 0RGLI\ WKH 3RLQW FODVV IURP &KDSWHU VR WKDW LW KDV D FRORU DQG LPSOHPHQWV WKH &RORUHG LQWHUIDFH <RX PD\ ZLVK WR FUHDWH D &RORUHG3RLQW FODVV WKDW H[WHQGV 3RLQW 6HFWLRQ &DVH 6WXG\ 'HVLJQLQJ D +LHUDUFK\ RI )LQDQFLDO &ODVVHV :KDW LV DQ DEVWUDFW FODVV" +RZ LV DQ DEVWUDFW FODVV OLNH D QRUPDO FODVV DQG KRZ GRHV LW GLIIHU" +RZ LV LW OLNH DQ LQWHUIDFH" &RQVLGHU ZULWLQJ D SURJUDP WR EH XVHG WR PDQDJH D FROOHFWLRQ RI PRYLHV 7KHUH DUH WKUHH NLQGV RI PRYLHV LQ WKH FROOHFWLRQ GUDPDV FRPHGLHV DQG GRFXPHQWDULHV 7KH FROOHFWRU ZRXOG OLNH WR NHHS WUDFN RI HDFK PRYLH V QDPH WKH QDPH RI WKH GLUHFWRU DQG WKH GDWH ZKHQ LW ZDV PDGH 6RPH RSHUDWLRQV DUH WR EH LPSOHPHQWHG IRU DOO PRYLHV DQG WKHUH ZLOO DOVR EH VSHFLDO RSHUDWLRQV IRU HDFK RI WKH WKUHH GLIIHUHQW NLQGV RI PRYLHV +RZ ZRXOG \RX GHVLJQ WKH FODVV HV WR UHSUHVHQW WKLV V\VWHP RI PRYLHV" ([HUFLVHV )RU WKH QH[W IRXU SUREOHPV FRQVLGHU WKH WDVN RI UHSUHVHQWLQJ W\SHV RI WLFNHWV WR VFKRRO FDPSXV HYHQWV (DFK WLFNHW KDV D XQLTXH QXPEHU DQG D SULFH 7KHUH DUH WKUHH W\SHV RI WLFNHWV ZDON XS WLFNHWV DGYDQFH WLFNHWV DQG VWXGHQW DGYDQFH WLFNHWV :DON XS WLFNHWV DUH SXUFKDVHG WKH GD\ RI WKH HYHQW DQG FRVW $GYDQFH WLFNHWV SXUFKDVHG RU PRUH GD\V EHIRUH WKH HYHQW FRVW DQG DGYDQFH WLFNHWV SXUFKDVHG IHZHU WKDQ GD\V EHIRUH WKH HYHQW FRVW 6WXGHQW DGYDQFH WLFNHWV DUH VROG DW KDOI WKH SULFH RI QRUPDO DGYDQFH WLFNHWV RQHV GD\V HDUO\ FRVW DQG IHZHU WKDQ GD\V HDUO\ FRVW ,PSOHPHQW D FODVV QDPHG Ticket WKDW ZLOO VHUYH DV WKH VXSHUFODVV IRU DOO WKUHH W\SHV RI WLFNHWV 'HILQH DOO FRPPRQ RSHUDWLRQV LQ WKLV FODVV DQG VSHFLI\ DOO GLIIHULQJ RSHUDWLRQV LQ VXFK D ZD\ WKDW HYHU\ VXEFODVV PXVW LPSOHPHQW WKHP 1R DFWXDO REMHFWV RI W\SH Ticket ZLOO EH FUHDWHG HDFK DFWXDO WLFNHW ZLOO EH DQ REMHFW RI D VXEFODVV W\SH 'HILQH WKH IROORZLQJ RSHUDWLRQV 7KH DELOLW\ WR FRQVWUXFW D WLFNHW E\ QXPEHU 528 7KH DELOLW\ WR DVN IRU D WLFNHW V SULFH 7KH DELOLW\ WR println D WLFNHW REMHFW DV D String $Q H[DPSOH VWULQJ ZRXOG EH "Number: 17, Price: 50.0" ,PSOHPHQW D FODVV QDPHG WalkupTicket WR UHSUHVHQW D ZDON XS HYHQW WLFNHW :DONXS WLFNHWV DUH DOVR FRQVWUXFWHG E\ QXPEHU DQG WKH\ KDYH D SULFH RI ,PSOHPHQW D FODVV QDPHG AdvanceTicket WR UHSUHVHQW WLFNHWV SXUFKDVHG LQ DGYDQFH $Q DGYDQFH WLFNHW LV FRQVWUXFWHG ZLWK D WLFNHW QXPEHU DQG DOVR KRZ PDQ\ GD\V LQ DGYDQFH WKH WLFNHW ZDV SXUFKDVHG $GYDQFH WLFNHWV SXUFKDVHG RU PRUH GD\V EHIRUH WKH HYHQW FRVW DQG DGYDQFH WLFNHWV SXUFKDVHG IHZHU WKDQ GD\V EHIRUH WKH HYHQW FRVW ,PSOHPHQW D FODVV QDPHG StudentAdvanceTicket WR UHSUHVHQW WLFNHWV SXUFKDVHG LQ DGYDQFH E\ VWXGHQWV $ VWXGHQW DGYDQFH WLFNHW LV FRQVWUXFWHG ZLWK D WLFNHW QXPEHU DQG DOVR KRZ PDQ\ GD\V LQ DGYDQFH WKH WLFNHW ZDV SXUFKDVHG 6WXGHQW DGYDQFH WLFNHWV SXUFKDVHG RU PRUH GD\V EHIRUH WKH HYHQW FRVW DQG DGYDQFH WLFNHWV SXUFKDVHG IHZHU WKDQ GD\V EHIRUH WKH HYHQW FRVW KDOI RI D QRUPDO DGYDQFH WLFNHW :KHQ D VWXGHQW DGYDQFH WLFNHW LV SULQWHG WKH VWULQJ VKRXOG PHQWLRQ WKDW WKH VWXGHQW PXVW VKRZ WKHLU VWXGHQW ,' )RU H[DPSOH "Number: 17, Price: 15.0 (ID required)" )RU WKH QH[W VL[ SUREOHPV FRQVLGHU WKH WDVN RI UHSUHVHQWLQJ W\SHV RI ELUGV LQ DQ DYLDU\ VLPXODWLRQ (DFK ELUG KDV DQ [ \ SRVLWLRQ DQG D FRORU DQG HDFK ELUG FDQ IO\ 'LIIHUHQW W\SHV RI ELUGV KDYH GLIIHUHQW IOLJKW EHKDYLRU :ULWH DQ LQWHUIDFH QDPHG Bird WR UHSUHVHQW GLIIHUHQW W\SHV RI ELUGV 7KH LQWHUIDFH VKRXOG KDYH PHWKRGV WR GR WKH IROORZLQJ *HW WKH ELUG V FRORU *HW WKH ELUG V [ \ SRVLWLRQ DV D 3RLQW 7HOO D ELUG WR IO\ (DFK WLPH D ELUG LV WROG WR IO\ LW ZLOO PRYH LWV SRVLWLRQ RQFH <RXU Bird LQWHUIDFH VKRXOG ZRUN ZLWK WKH IROORZLQJ FOLHQW SURJUDP 529 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.awt.*; public class Aviary { public static final int SIZE = 20; public static final int PIXELS = 10; public static void main(String args) { // create a drawing panel DrawingPanel panel = new DrawingPanel(SIZE * PIXELS, SIZE * PIXELS); Graphics g = panel.getGraphics(); // create several birds Bird birds = { new Cardinal(7, 4), new Cardinal(3, 8), new Hummingbird(2, 9), new Hummingbird(16, 11), new Bluebird(4, 15), new Bluebird(8, 1), new Vulture(3, 2), new Vulture(18, 14), }; while (true) { // clear screen g.setColor(Color.WHITE); g.fillRect(0, 0, SIZE * PIXELS, SIZE * PIXELS); // tell each bird to fly, and draw the bird in its new position for (Bird bird : birds) { bird.fly(); g.setColor(bird.getColor()); Point pos = bird.getPosition(); g.fillOval(pos.getX() * PIXELS, pos.getY() * PIXELS, PIXELS, PIXE } panel.sleep(500); } } } :ULWH D FODVV QDPHG Cardinal WKDW UHSUHVHQWV FDUGLQDO ELUGV $ FDUGLQDO LV UHG LQ FRORU 7KH FDUGLQDO V PRYHPHQW LV YHUWLFDO ,QLWLDOO\ D FDUGLQDO LV PRYLQJ XS (DFK WLPH WKH FDUGLQDO LV WROG WR IO\ LW ZLOO PRYH LWV SRVLWLRQ RQH XQLW XSZDUG RQ WKH \ D[LV UHPHPEHU WKDW XSZDUG LV QHJDWLYH ,I WKH FDUGLQDO KLWV WKH HGJH RI WKH DYLDU\ D \ FRRUGLQDWH RI RU LW WXUQV DURXQG DQG IOLHV LQ WKH RSSRVLWH GLUHFWLRQ $VVXPH WKDW WKH DYLDU\ V VL]H RI LV VWRUHG LQ D VWDWLF FRQVWDQW QDPHG Aviary.SIZE <RX PD\ ZLVK WR LQWURGXFH DQ DEVWUDFW FODVV WR KROG EHKDYLRU WKDW ZLOO EH FRPPRQ WR DOO ELUG FODVVHV :ULWH D FODVV QDPHG Hummingbird WKDW UHSUHVHQWV KXPPLQJELUGV $ KXPPLQJELUG LV PDJHQWD LQ FRORU 7KH KXPPLQJELUG V PRYHPHQW LV UDQGRP HDFK WLPH WKH KXPPLQJELUG LV WROG WR IO\ LW ZLOO SLFN D QHZ UDQGRP [ \ SRVLWLRQ LQ WKH UDQJH RI WR 530 :ULWH D FODVV QDPHG Bluebird WKDW UHSUHVHQWV EOXHELUGV $ EOXHELUG LV EOXH LQ FRORU 7KH EOXHELUG V PRYHPHQW LV LQ D ]LJ ]DJ SDWWHUQ ,QLWLDOO\ WKH EOXHELUG IDFHV ULJKW 7KH EOXHELUG PRYHV LQ DQ DOWHUQDWLQJ SDWWHUQ RI XS ULJKW GRZQ ULJKW XS ULJKW GRZQ ULJKW DQG VR RQ XQWLO LW KLWV WKH ULJKW HGJH RI WKH DYLDU\ [ FRRUGLQDWH RI DW ZKLFK SRLQW LW WXUQV DURXQG 6XEVHTXHQW FDOOV WR fly ZLOO FDXVH WKH ELUG WR PRYH XS OHIW GRZQ OHIW XS OHIW GRZQ OHIW DQG VR RQ XQWLO LW KLWV WKH OHIW HGJH RI WKH DYLDU\ :ULWH D FODVV QDPHG Vulture WKDW UHSUHVHQWV YXOWXUHV $ YXOWXUH LV EODFN LQ FRORU 7KH YXOWXUH V PRYHPHQW LV LQ D FRXQWHU FORFNZLVH FLUFOH SDWWHUQ ,QLWLDOO\ WKH YXOWXUH IDFHV XS 7KH ILUVW WLPH WKH YXOWXUH IOLHV LW PRYHV XS E\ RQH WKHQ LW WXUQV WR IDFH OHIW ,WV VHFRQG PRYH LW PRYHV OHIW E\ RQH DQG WXUQV WR IDFH GRZQ ,WV WKLUG PRYH LW PRYHV GRZQ DQG WXUQV WR IDFH ULJKW ,WV IRXUWK PRYH LW PRYHV ULJKW E\ RQH DQG WXUQV WR IDFH XS 7KH SDWWHUQ UHSHDWV LQ WKLV IDVKLRQ :KDW FKDQJHV VKRXOG EH PDGH LI ZH ZDQW WR FRXQW WKH QXPEHU RI WLPHV HDFK ELUG KDV IORZQ" 3URJUDPPLQJ 3URMHFWV :ULWH DQ LQKHULWDQFH KLHUDUFK\ RI GLPHQVLRQDO VKDSHV 0DNH D WRS OHYHO VKDSH LQWHUIDFH WKDW KDV PHWKRGV IRU JHWWLQJ LQIRUPDWLRQ VXFK DV WKH YROXPH DQG VXUIDFH DUHD RI D ' VKDSH 7KHQ PDNH FODVVHV DQG VXEFODVVHV WKDW LPSOHPHQW YDULRXV VKDSHV VXFK DV FXEHV UHFWDQJXODU SULVPV VSKHUHV WULDQJXODU SULVPV DQG F\OLQGHUV 3ODFH FRPPRQ EHKDYLRU LQ VXSHUFODVVHV ZKHQHYHU SRVVLEOH DQG XVH DEVWUDFW FODVVHV DV DSSURSULDWH $GG PHWKRGV WR WKH VXEFODVVHV WR UHSUHVHQW WKH XQLTXH EHKDYLRU RI HDFK ' VKDSH VXFK DV D PHWKRG WR JHW D VSKHUH V UDGLXV :ULWH D VHW RI FODVVHV WKDW GHILQH WKH EHKDYLRU RI FHUWDLQ DQLPDOV 7KH\ FDQ EH XVHG LQ D VLPXODWLRQ RI D ZRUOG ZLWK PDQ\ DQLPDOV PRYLQJ DURXQG LQ LW 'LIIHUHQW NLQGV RI DQLPDOV ZLOO PRYH LQ GLIIHUHQW ZD\V \RX DUH GHILQLQJ WKRVH GLIIHUHQFHV $V WKH VLPXODWLRQ UXQV DQLPDOV FDQ GLH E\ HQGLQJ XS RQ WKH VDPH ORFDWLRQ LQ ZKLFK FDVH WKH VLPXODWRU UDQGRPO\ VHOHFWV RQH DQLPDO WR VXUYLYH WKH FROOLVLRQ 7KH EHKDYLRU RI HDFK DQLPDO FODVV LV WKH IROORZLQJ &ODVV %LUG )URJ JHW&KDU JHW0RYH % ) 5DQGRPO\ VHOHFWV RQH RI WKH IRXU GLUHFWLRQV HDFK WLPH 3LFNV D UDQGRP GLUHFWLRQ PRYHV LQ WKDW GLUHFWLRQ UHSHDW VDPH DV ELUG EXW VWD\LQJ LQ D VLQJOH GLUHFWLRQ ORQJHU :HVW 6RXWK QRUWK ZHVW UHSHDW ]LJ ]DJ WR WKH 1: QRUWK HDVW UHSHDW FORFNZLVH ER[ 0RXVH 0 7XUWOH 7 :ROI : <RX GHILQH WKLV WR KDYH DQ\ FXVWRP EHKDYLRU \RX ZDQW <RXU FODVVHV VKRXOG EH VWRUHG LQ ILOHV FDOOHG %LUG MDYD )URJ MDYD 0RXVH MDYD 7XUWOH MDYD DQG :ROI MDYD 531 :ULWH DQ LQKHULWDQFH KLHUDUFK\ WKDW PRGHOV VSRUWV SOD\HUV &UHDWH D FRPPRQ VXSHUFODVV DQG RU LQWHUIDFH WR VWRUH LQIRUPDWLRQ FRPPRQ WR DQ\ SOD\HU UHJDUGOHVV RI VSRUW VXFK DV QDPH QXPEHU DQG VDODU\ 7KHQ FUHDWH VXEFODVVHV IRU SOD\HUV RI \RXU IDYRULWH VSRUWV VXFK DV EDVNHWEDOO VRFFHU RU WHQQLV 3ODFH VSRUW VSHFLILF LQIRUPDWLRQ DQG EHKDYLRU VXFK DV NLFNLQJ RU YHUWLFDO MXPS KHLJKW LQWR VXEFODVVHV ZKHQHYHU SRVVLEOH :ULWH DQ LQKHULWDQFH KLHUDUFK\ WR PRGHO LWHPV DW D OLEUDU\ ,QFOXGH ERRNV PDJD]LQHV MRXUQDO DUWLFOHV YLGHRV DQG HOHFWURQLF PHGLD VXFK DV &'V ,QFOXGH FRPPRQ LQIRUPDWLRQ LQ D VXSHUFODVV DQG RU LQWHUIDFH WKDW WKH OLEUDU\ PXVW KDYH IRU HYHU\ LWHP VXFK DV D XQLTXH LGHQWLILFDWLRQ QXPEHU DQG WLWOH 3ODFH LWHP W\SH VSHFLILF EHKDYLRU DQG LQIRUPDWLRQ VXFK DV D YLGHR V UXQWLPH OHQJWK RU D &' V PXVLFDO JHQUH LQWR WKH VXEFODVVHV 6WXDUW 5HJHV 0DUW\ 6WHSS 532 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online