?c ?¥??? ) ?3 ?¥???à Q[#3 ??0 ?¥1" )D P¨? e?QSPUFDUaGJOBMaBCTUSBDU ) ?3 ?? ) P¨¤ g? =? ? ) ??? 7?¥V?? ?¥ !9e5 6.1 3 ??0 ? Circle Circle Methods Circle Data Superclass Inheritance Cylinder Circle Methods Cylinder Methods Circle Data Cylinder Data Subclass Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume SubclassSuperclass UML Diagram Creating a Subclass Creating a subclass extends data and methods from the superclass. You can also: ) Add new data ) Add new methods ) Override the methods of the superclass Cylinder Class Example 6.1 Testing Inheritance )Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes. TestCylinder Run 6.2 The Object Class )The Object class is the root of all Java classes. )The toString() method returns a string representation of the object. )The equals() method compares the contents of two objects. )The clone() method copy objects 6.3 ? e? The protected Modifier )protected? e??¨? ?¥ ? ?ZE b ?? 1¥ ? ZE V[$] B?¥ ?? ? ?¥0 ?`ù' P0 ??]¥?9 V[b The protected Modifier pacakge p1 class C1 protected int x class C3 C1 c1; c1.x can be read or modified pacakge p2 class C2 extends C1 x can be read or modified in C2 class C4 C1 c1; c1.x cannot be read nor modified C1? C3 ]B ?? C2 ^ C1 ¥0 ? C4D C1 ?] B? ? The final Modifier )The final class cannot be extended: final class Math {...} )The final variable is a constant: final static double PI = 3.14159; )The final method cannot be modified by its subclasses. The abstract Modifier )The abstract class – Cannot be instantiated – Should be extended and implemented in subclasses )The abstract method – Method signature without implementation 6.4  ???? ?t? ) ?? (polymorphism) ^B? ? ?Mi1?? Tb ) Java? V[]B ??] ?? ?l ?M]??T?]¥?ZE  ??·¥ ^? H ???3? '? ZE} ?¥ ? ?b?? ? ?9?S? ? t?b 6.5 The instanceof Operator Use the instanceof operator to test whether an object is an instance of a class: Circle myCircle = new Circle(); if (myCircle instanceof Cylinder) { Cylinder myCylinder = (Cylinder)myCircle; ... } 6.6 ¤ g ? Java? ?×??'B? ?? ? ? ?×??'B? ?? ? μ?3 ?ào ?μB?3 ?b μ?3 ?ào ?μB?3 ?b Java? |}×??¥/ ? ^¤ gb ? |}×??¥/ ? ^¤ gb Java ^¨¤ g/ ?¥ey ^y1× ^¨¤ g/ ?¥ey ^y1× ??1 1 PIr dèˉ1 1r q ??1 1 PIr dèˉ1 1r q ?úb ? ¤ gü ^ZE?l?è ′¥"? ¤ gü ^ZE?l?è ′¥"? b V [ü¤ g ?31B? í ? O ú ?? [ü¤ g ?31B? í ? O ú ?? l BtZE?i? LC?tZE l BtZE?i? LC?tZE 7 ^? LC??¤ g¥ ? ? LCb 7 ^? LC??¤ g¥ ? ? LCb 6.6 ¤ g )μ HA?V+? ?? ? 3B?0 ??? ? ì¥ ? ?ZEb ?7 Java?|?×??b ?T1 o3 extends?l0 ? ?o ?μB?3 ?bμ  ¤ g V[¤?×??¥rTb )¤ g ^B?D ?M ?¥2ocè ?`Z Eb ) Javaü¤ g?TB?+ y¥ ?b ??¤ g?$Ir 1B?? ?¥3? ?óqü^è?¥ ?B"b? ? ?¨ new ??1 7y L èb ¤ g?l modifier interface InterfaceName { constants declarations; methods signatures; } 7y¤ g [public] interface InterfaceName [extends listOfSuperInterface] {……} ) public· ü ?i ? ( V P¨??¤ g ? 8 HV· ü ?i ? ( V P¨??¤ g ? 8 HV üo μD ¤ g ?l ] B??¥ ?? V`ùN¤ üo μD ¤ g ?l ] B??¥ ?? V`ùN¤ gb ) interface?l¤ g ??S M1o3b?l¤ g ??S M1o3b ) InterfaceName  ¤ g 3Yè[  ¤ g 3Yè[ able ible22 áV ü¤ g ???B?¥?1b áV ü¤ g ???B?¥?1b ) extends ^1o3 ^1o3 . ) ListOfInterfaceV UB?¤ g V[μ?3¤ gV UB?¤ g V[μ?3¤ g ¨ù|s 77 ?o ?μB?3 ?b ¨ù|s 77 ?o ?μB?3 ?b )0¤ g??3¤ g? ?μ¥è ?ZEb0¤ g??3¤ g? ?μ¥è ?ZEb ¤ g8 ¤ g8 ) ¤ g8 ?è ?l?ZE?l ?sb¤ g8 ?è ?l?ZE?l ?sb è ?l è ?l type NAME=value  ? ? type Vì ?i ?? Vì ?i ?? NAME1è  Yèv 1è  Yèv value1è ′b 1è ′b ) ¤ g??l¥è  V$ LC?¤ g¥? ? Zb ?D¤ g??l¥è  V$ LC?¤ g¥? ? Zb ?D C ?¨ ?¨ #define[# [# C++? ? const?l¥è M]b ?l¥è M]b ¤ g??l¥è  μ ¤ g??l¥è  μ public,final,static¥ ??b ) ZE?lZE?l returnType methodName([paramlist])  ¤ g?oé?ZE¥ 2 ü7?4ZE¥ LC ?[ ¤ g?oé?ZE¥ 2 ü7?4ZE¥ LC ?[ ZE?l? àμZE8 O¨s|2áb ZE?l? àμZE8 O¨s|2áb ) ¤ g? 2 ü¥ZE μ¤ g? 2 ü¥ZE μ public?? abstract ??b Example of Creating an Interface // This interface is defined in // java.lang package public interface Comparable { public int compareTo(Object o); } ¤ g¥ LC ¤ g¥ LC ) ?¥ 2 ü?¨  ?¥ 2 ü?¨ JNQMFNFOUT0  ?V U 0  B? ? P¨ ?¤ g ?? V[ P¨ B? ? P¨ ?¤ g ?? V[ P¨ ¤ g??l¥è 7 OA? LC¤ g ¤ g??l¥è 7 OA? LC¤ g ??l¥ ?μZEb ??l¥ ?μZEb )B? ? V LC?¤ g B? ? V LC?¤ g JNQMFNFOUT 0 ?¨ù|s?b 0 ?¨ù|s?b ¤ g ?? )¤ g VT1B??¨ ?? ? P¨?3 ¤ g VT1B??¨ ?? ? P¨?3 1 3` ??¥ 8¥ ?"×? 1 3` ??¥ 8¥ ?"×? ?¥?o? ?b ?¥?o? ?b ) ?? LC?¤ g¥ ?¥ L è? V[i% ?? LC?¤ g¥ ?¥ L è? V[i% ?¤ g ??¥M ?YV?tM  ?¤ g ??¥M ?YV?tM  V`ù ? ? LC¥¤ g?¥ZEb V`ù ? ? LC¥¤ g?¥ZEb Java ? H"d? ?1 ??? P¨ '? ?? ? H"d? ?1 ??? P¨ '? ?? ¥ZEb è ? è ? interface Monitor{ void MonitorDisplay( ); } interface Case{ void PCComputing( ); } class Computer implements Monitor,Case{ public void MonitorDisplay( ){ //method body } public void PCComputing( ){ //method body } } Computer?? ? ] H  ! Monitor DD Case?  ?¤ g?  ?¤ g ¥+?b ?T| ? Computer ?¥ `` c·?b?B? ?·?b?B? ? ?? Monitor ¥¥ interface reference(m)?? 5á ì|? ? i |5á ì|? ? i | ?? ?? Case¥ ò[+¥ ò[+ ?DZEb?DZEb public class InterfaceCovert{ public static void main(String args[]){//??3? ???3? ? Computer c = new Computer( ); Monitor m = (Monitor)c; //  c`? ?? `? ?? Monitor interface¥+? ¥+? m.MonitorDisplay( );//N1?Ei | N1?Ei | m.PCComputing( );//N1dEi | N1dEi | } } Ir??CB?pá ì ? k Y V m P¨B?? ?? Monitor interface+?¥+?¥ methodb yN P¨yN P¨ interfaceS ??D V[ |` Z ?¥+? ?S YS ??D V[ |` Z ?¥+? ?S Y ¥) ??i | HA?i ^? P¨ íEi |¥+?b¥) ??i | HA?i ^? P¨ íEi |¥+?b 6.7 èq 7?V? 3 ps "ds "d !9 LC ? k ? ?¨ ?1 ?_`¥ èq 7?V? ?_`¥ èq 7?V? ?_`¥ èq 7?V?v8?s1 ??¨  ?_`¥ èq 7?V?v8?s1 ??¨  ? ?_`¥s ?_`¥s OOA Object-Oriented Analysis ? ?_`¥ !9 ?_`¥ !9   OOD Object-Oriented Design ? ?_`¥ LC ?_`¥ LC OOP Object-Oriented Programming ?_`¥s ?_`¥s (Object-Oriented Analysis) ?_`¥s¥ ?_`¥s¥ ?1T¨ ?1T¨ ^ ^ ? ü ?¨?¥3 p ü ?¨?¥3 p ? ¨S?¥ ?_`¥ ??S1V ?3 p¨S?¥ ?_`¥ ??S1V ?3 p ? ?? ?_`¥s ?'?? ?_`¥s ?' OOA ? ? b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a }1 7?¥?¨ èq ??¥ò?C L }1 7?¥?¨ èq ??¥ò?C L W?¥ L8iV?``? ?b W?¥ L8iV?``? ?b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a 1` 1` —— ?a? | ?¥ò? ??` !9á ? ?a? | ?¥ò? ??` !9á ? ??  ?? ? ì-W¥? ?1"bá ? ?? ^ ? ??  ?? ? ì-W¥? ?1"bá ? ?? ^ ? ` ?c¥ò? ??? L è ?¤58C  ` ?c¥ò? ??? L è ?¤58C  `-Wy+?¥ Yt?5?K?Hq7i¥? ? `-Wy+?¥ Yt?5?K?Hq7i¥? ? 1"b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a ?l `? ?¥? ? ??[#`-W¥hY ?l `? ?¥? ? ??[#`-W¥hY ?b`? ?¥? ? ??ü ^ e ì¥?1ZE ?b`? ?¥? ? ??ü ^ e ì¥?1ZE  ??1??? `? ?¥T¨?? ?  ??1??? `? ?¥T¨?? ? ?`3??t? ?¥ H? ? ì-W|??h ?`3??t? ?¥ H? ? ì-W|??h Y?b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a ?l `? ?¥? ? ??[#`-W¥hY ?l `? ?¥? ? ??[#`-W¥hY ?b`? ?¥? ? ??ü ^ e ì¥?1ZE ?b`? ?¥? ? ??ü ^ e ì¥?1ZE  ??1??? `? ?¥T¨?? ?  ??1??? `? ?¥T¨?? ? ?`3??t? ?¥ H? ? ì-W|??h ?`3??t? ?¥ H? ? ì-W|??h Y?b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a ?l"d? ?μ`? ?-W¥aQ21 ?l"d? ?μ`? ?-W¥aQ21 "b ? : ??|`Wy ? ??|`Wy ? “B? B? —+ y + y ”¥21 ¥21 " c|`Wy ? " c|`Wy ? “?8 ?8 —?s ?s ”¥2 ¥2 1"b OOA ? ? ?aQ ?aQ ? `` — ?a ?a ? á ? ??aá ? ??a ? ?a?a ? 2a2a ? ?5a?5a ?l ????5üμ1`s ?l ????5üμ1`s Y?B?]¥?5 ???5? Y?B?]¥?5 ???5? 1B?0"db 1B?0"db ?_`¥ !9 ?_`¥ !9 (Object-Oriented Design) ? ?_`¥ !9| ?_`¥ !9| OOA ?¥$ ? ?? ? ?¥$ ? ?? ? 5 ?a ?5 ?? ? 5 ? ??s¥ = ?éB 5 ?a ?5 ?? ? 5 ? ??s¥ = ?éB ? ? ? ? OOA ?b? ??sF  OOA ?} ?} V¥ V¥ “ù5 ? ù5 ? ”?sü? K¥ ?sü? K¥ OOD   ? ? b ? a? ^ LC / ? LC¥i ü?'B??s a? ^ LC / ? LC¥i ü?'B??s ¥ 8/ ?%?D LCZEM? ?μ  ??s ¥ 8/ ?%?D LCZEM? ?μ  ??s ^? Vn¥V Z? ^? Vn¥V Z? /?   TV?í ?  ?   TV?í ?  /?   T * 1o31? ?   T * 1o31? OOD ?¥ ?¥ “ ? ? 5 ? 5 ? ”?s7  e¥ ?μ?s5? V[?F? ?s7  e¥ ?μ?s5? V[?F? ?1ר??¥"d?b ?1ר??¥"d?b OOD ? ?  ? ù5 ? ù5 ? ? ? ?5 ?? ?5 ? ? ?5 ? ?5 ? ? ? 5 ? ? 5 ? %? !9 ?á ? ? ??¥ ?? ?ZE '? %? !9 ?á ? ? ??¥ ?? ?ZE '? T¥? ?aRí′a? ??? ? LC¥ ü ????b T¥? ?aRí′a? ??? ? LC¥ ü ????b μ3??"d¥ |? ?¥ !9b μ3??"d¥ |? ?¥ !9b μ3) ?i??T- ?¥"d'÷5 ?? ?yTb μ3) ?i??T- ?¥"d'÷5 ?? ?yTb μ3 !9"dD ? o¥¤ gb μ3 !9"dD ? o¥¤ gb ?_`¥ LC ?_`¥ LC (Object-Oriented Programming) ?_`¥ LCü ^ 8¥I ?¨ b ?1 ? ? ?_`¥ LCü ^ 8¥I ?¨ b ?1 ? ? ? ê4B?? a¥ ?_`¥ê4B?? a¥ ?_`¥ I??yI??y ? ¨ê?¥?yI ? LCW% !9 ?? ?¤ ¥ TamVa¨ê?¥?yI ? LCW% !9 ?? ?¤ a ü??5? a ü??5?  èq"dò` ?¥WD í ?  èq"dò` ?¥WD í ? ? |Iz¥ò? ?} ?  v? ?¥Mo1"|Iz¥ò? ?} ?  v? ?¥Mo1" "?"? ? ?¨ 7? |?4¥ ?¨ 7? |?4¥ ? k? k " è?¨?4¥" è?¨?4¥ ? k? k " èsY" èsY _I ???¥ò?  v??? èq"d _I ???¥ò?  v??? èq"d ? ?1?1 a ü (Illustration) ? ?? ?_`¥à Q ?[÷¤í L=ù5¥e?? ?_`¥à Q ?[÷¤í L=ù5¥e ·? Lé¥Z T ?V ??) ??tù5 ?[ ·? Lé¥Z T ?V ??) ??tù5 ?[ ?_`¥ èq 7?ZE1[a ?_V?¥Z ?_`¥ èq 7?ZE1[a ?_V?¥Z Eμ÷z¥ 2?a Vר?? V ?Z? P Eμ÷z¥ 2?a Vר?? V ?Z? P ¤  ? ¤  ? “s s — !9 !9 — LC LC ”¥ 7?V?9÷F ¥ 7?V?9÷F úra y?b ? ' PCy - ùy??T?a¨?3 p???' PCy - ùy??T?a¨?3 p??? 31Q ?i?? - ???¥ f ?9 ?[ 31Q ?i?? - ???¥ f ?9 ?[ -yT¥$- V ?1??7??J ?. -yT¥$- V ?1??7??J ?. dZE??¤?wJeμ !9×? I n ? dZE??¤?wJeμ !9×? I n ? 2???2¥1?a1b 2???2¥1?a1b ?_` èq 7?¥a? ?_` èq 7?¥a? ? Vר? Vר? ? V ?Z? V ?Z? ? V5 ?? V5 ?? ·B? èq[ "? ? 7?¥  v ???K??? ·B? èq[ "? ? 7?¥  v ???K??? [ "? P¨7 O V[×ˉ1 P¨  e[ "?V7 [ "? P¨7 O V[×ˉ1 P¨  e[ "?V7 ??]¥"d??èT¨b ?_`?? !9¥ ??]¥"d??èT¨b ?_`?? !9¥ `a a??a ??íB è?a 1? ?" `a a??a ??íB è?a 1? ?" Vר?????i1-?b a? ^4ú  7?r Vר?????i1-?b a? ^4ú  7?r q ê  7?? ù??  7??' ???¨Xü q ê  7?? ù??  7??' ???¨Xü $£ ü? ?μr¥  v??é  ?¤? £?1 $£ ü? ?μr¥  v??é  ?¤? £?1 yT 9M?h  ?4??S????C yT 9M?h  ?4??S????C }v?  èq 7?¥3 pb }v?  èq 7?¥3 pb ?_` èq 7?¥a? ?_` èq 7?¥a? ? Vר? Vר? ? V ?Z? V ?Z? ? V5 ?? V5 ?? 1 p?¨ èq ??ZLa ?^1é? ? ????? ? 1 p?¨ èq ??ZLa ?^1é? ? ????? ?  ???¥S??? #? èq¥ = ?9 #? èq¥?  ???¥S??? #? èq¥ = ?9 #? èq¥? T?yT?b?18C+Y a?? y ?e?¥ èq T?yT?b?18C+Y a?? y ?e?¥ èq 7?ZE? P¨   v?/ ?÷μr|?b 7?ZE? P¨   v?/ ?÷μr|?b ?_` èq 7?¥a? ?_` èq 7?¥a? ? Vר? Vר? ? V ?Z? V ?Z? ? V5 ?? V5 ?? ?_`¥ 7?ZE?¨ =d1V??f ?~ ?_`¥ 7?ZE?¨ =d1V??f ?~ ?aˉ¤¥ ?T1q"d¥?q P??[ ?aˉ¤¥ ?T1q"d¥?q P??[ "¥F?÷F? ?aZL ü ? ? ? ¥?T "¥F?÷F? ?aZL ü ? ? ? ¥?T  B  P¤?μ? ?¥μK?ZE? V[?  B  P¤?μ? ?¥μK?ZE? V[? :a?M?t ? b