《COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt》由会员分享,可在线阅读,更多相关《COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt(49页珍藏版)》请在金锄头文库上搜索。
1、1 COS240O OLanguagesAUBG COSdept Lecture15Title AbstractClassesandInterfacesinJavaReference COS240Syllabus 2 LectureContents ReviewohInheritanceandPolymorphismAbstractClassesWhyAbstractMethods InterfacesExample theComparableInterfaceExample theComparableInterfaceInterfacesvs AbstractClasses 3 Inheri
2、tance Review 4 Polymorphism Review 5 Interfaces AbstractClasses Briefpresentation 6 Interfaces AbstractClasses TheInterfaceconcept 7 Interface publicinterfaceDriver voidturnWheel doubleangle voidpressAccelerator doubleamount voidpressBrake doubleamount publicclassBusDriverimplementsDriver mustinclud
3、eimplementationforeachofthethreemethodsfromDriver Nomethodbodies justmethodheader Mustprovideimplementationwhenused publicinterfaceDriver voidturnWheel doubleangle voidpressAccelerator doubleamount voidpressBrake doubleamount 8 publicclassBusDriverextendsPersonimplementsDriver mustincludeimplementat
4、ionforeachofthethreemethodsfromDriver Mayalsohave Thisisa backdoor approachtomultipleinheritanceforJavaviaextendsandimplements Singleinheritanceviaextendsandextrainheritanceviaimplements 9 Interfaces AbstractClasses TheAbstractClassconcept 10 AbstractClasses Aclasswheresomemethodsareunspecified like
5、inaninterface Theunspecifiedmethodsaredeclaredabstract Theclassalsoisdeclaredabstract Anabstractclassmustbesub classed i e extended youcannotinstantiateobjectsofanabstracttype Usefulifyouwanttospecifyan interface alongwithsomedefaultbehaviors SimilarforC 11 AbstractClassExample abstractclassCacheFil
6、e Stringfilename byte contents voidflush Writefiletodisk voidrefresh Loadfilefromdisk abstractStringdeserialize abstractbyte serialize Strings Thesemethodsaredefined Thesemethodsareabstractbecausehowyouwanttostoredataintothefileisapplication dependent Mayhavedefinedmethodsandabstractmethods 12 Abstr
7、actClasses Interfaces Comprehensivepresentation 13 Inheritancehierarchy Ifyoumovefromsuperclassdowntosubclass classesbecomemorespecificandmoreconcrete Ifyoumovefromsubclassuptosuperclass classesbecomemoregeneralandlessspecificSometimessuperclassissoabstractthatitcannothaveanyspecificinstances Suchac
8、lassisreferredtoasanABSTRACTCLASS 14 GeometricObject Circle Rectangle Object GeometricObject CircleRectangleCommonproperties Color filled nofilled dateCreatedCircle specificproperties radiusRectangle specificproperties width height 15 GeometricObject Circle Rectangle GeometricObject CircleRectangleC
9、ommonproperties Color filled nofilled dateCreatedCircle specificpropertiesandbehavior Radius getArea getPerimeter Rectangle specificpropertiesandbehavior width height getArea getPerimeter 16 Inheritancehierarchy IsitreasonabletogeneralizegetArea getPerimeter methods Fromoneside Sincewecancomputearea
10、andperimeterforallgeometricobjects itisbettertodefinegetArea getPerimeter asmethodsinsuperclassGeometricObject Fromotherside Bothmethodscannotbeimplementedinthebaseclassbecausetheirimplementationdependsonspecificproperties radiusorheight width ofthegeometricobjectdefinedinthesubclasses Suchmethodsar
11、ereferredtoasabstractmethodsandaredenotedinthesuperclassusingmodifierabstract Classwithabstractmethodsbecomesanabstractclassandismusttobealsodenotedabstract Seenextslide 17 OpenfileProgGeometricObjectAbstractClass java abstractclassGeometricObject publicabstractdoublegetArea publicabstractdoublegetP
12、erimeter Youcannotcreateinstancesofabstractclassesusingnewoperator Constructorsinabstractclassesareprotectedbecausetheyareusedonlybysubclasses Superclassdefinescommonfeatures incl methodsgetArea andgetPerimeter Becauseyoudon tknowhowtocomputeareaandperimeterofgeometricobjects bothmethodsaredefinedas
13、abstractmethods Thesemethodsareimplemented overriddeninthesubclasses 18 OpenfileProgGeometricObjectAbstractClass java classCircleextendsGeometricObject publicdoublegetArea returnMath PI radius radius publicdoublegetPerimeter return2 Math PI radius publicStringtoString overriddenmethodreturn CircleCi
14、rcle n super toString 19 OpenfileProgGeometricObjectAbstractClass java classRectangleextendsGeometricObject publicdoublegetArea returnwidth height publicdoublegetPerimeter return2 width height publicStringtoString overriddenmethodreturn RectangleRectangle n super toString 20 OpenfileProgGeometricObj
15、ectAbstractClass java Whydoweneedabstractmethods Whatbenefitsifany Browsethemain method Itcreatestwogeometricobjects circle rectangleInvokesequalArea method haveacarefullookattheformalparameterstypeInvokesdisplayGeometricObject method haveacarefullookattheformalparametertype 21 OpenfileProgGeometric
16、ObjectAbstractClass java publicclassProgGeometricObjectAbstractClass publicstaticvoidmain Stringargs GeometricObjectgeo1 newCircle 5 GeometricObjectgeo2 newRectangle 5 3 System out println Thetwoobjecthavesamearea equalArea geo1 geo2 displayGeometricObject geo1 displayGeometricObject geo2 endofmainpublicstaticbooleanequalArea GeometricObjecto1 GeometricObjecto2 returno1 getArea o2 getArea publicstaticvoiddisplayGeometricObject GeometricObjecto System out println Theareais o getArea System out pr