Inheritance,++part+2:++Subclassing+COMP+401,+Spring+2013+Lecture+9+2/6/2013+MoBvaBng+Example+• lec9.v1+• Suppose+we’re+wriBng+a+university+management+system.+• Interfaces:+– Person+• get+first+and+last+name+• get/set+address+– Student+• add+credits+• get+/+set+status+(i.e.,+freshman,+sophomore,+junior,+senior)+– Professor+• promote+• get+rank+(i.e.,+assistant,+associate,+full)+• Classes:+– StudentImpl+implements+Person,+Student+– ProfessorImpl+implements+Person,+Professor+lec9.v1+Notes+• Student+and+Professor+interfaces+should+be+subinterfaces+of+Person+– Presumably+implemenBng+Student+or+Professor+implies+also+being+a+Person+lec9.v2+Notes+• CasBng+no+longer+necessary+– Anything+that+is+a+Student+is+also+a+Person+– Anything+that+is+a+Professor+is+also+a+Person+• NoBce+how+StudentImpl+and+ProfessorImpl+implement+the+Person+part+of+Student+and+Professor+– EssenBally+the+same+implementaBon+• Private+fields+for+first,+last,+and+address+• Same+definiBons+for+Person+methods+– When+two+or+more+classes+implement+the+same+interface+in+the+same+way,+then+subclassing+can+help.+Extending+Classes+• Declared+with+“extends”+keyword+• Original+class+– Parent,+parent+class,+superclass+• New+class+– Child,+child+class,+subclass,+extended+class+• Subclasses+inherit+fields+and+methods+from+the+parent+class.+– Collect+the+common+implementaBon+details+from+related+classes+into+a+single+parent+class.+– Define+each+related+class+as+a+subclass+that+just+adds+the+details+that+are+not+in+common.+lec9.v3+Notes+• NoBce+parallel+with+interface+structure+– PersonImpl+implements+Person+– StudentImpl+extends+PersonImpl+• and+implements+Student+which+extends+Person+– ProfessorImpl+extends+PersonImpl+• and+implements+Professor+which+extends+Person+• Subclass+constructor+calls+superclass+constructor+using+“super”+– Must+be+first+line+of+subclass+constructor+– Can+sBll+chain+to+a+different+constructor+Subinterface+vs.+Subclass+• Extending+interface+only+added+behavior+to+contract.+– Since+interfaces+don’t+specify+(and+don’t+care)+how+contract+is+fulfilled.+• Extending+class+creates+a+new+class+that+shares+internal+implementaBon+details+of+its+super+class.+COMP+401+::+Spring+2012+ 7+public+class+Bicycle+{++++++++++++++//+the+Bicycle+class+has+three+fields+++++public+int+cadence;+++++public+int+gear;+++++public+int+speed;++++++++++++++//+the+Bicycle+class+has+one+constructor+++++public+Bicycle(int+startCadence,++++++++++++++++++++int+startSpeed,++++++++++++++++++++int+startGear)+{+++++++++gear+=+startGear;+++++++++cadence+=+startCadence;+++++++++speed+=+startSpeed;+++++}++++++++++++++//+the+Bicycle+class+has+four+methods+++++public+void+setCadence(int+newValue)+{+++++++++cadence+=+newValue;+++++}++++++++++++++public+void+setGear(int+newValue)+{+++++++++gear+=+newValue;+++++}++++++++++++++public+void+applyBrake(int+decrement)+{+++++++++speed+a=+decrement;+++++}++++++++++++++public+void+speedUp(int+increment)+{+++++++++speed++=+increment;+++++}++++++++++}+public+class+MountainBike+extends+Bicycle+{++++++++++++++//+the+MountainBike+subclass+adds+one+field+++++public+int+seatHeight;++++++//+the+MountainBike+subclass+has+one+constructor+++++public+MountainBike(int+startHeight,+++++++++++++++++++++++++int+startCadence,+++++++++++++++++++++++++int+startSpeed,+++++++++++++++++++++++++int+startGear)+{+++++++++cadence+=+startCadence;+++++++++speed+=+startSpeed;+++++++++gear+=+startGear;+++++++++seatHeight+=+startHeight;+++++}+++++++++++++++++//+the+MountainBike+subclass+adds+one+method+++++public+void+setHeight(int+newValue)+{+++++++++seatHeight+=+newValue;+++++}++++}+MountainBike+m+=+new+MountainBike(10,+15,+0,+1);++m.setCadence(20);+m.setGear(2);+m.applyBreak(5);+m.speedUp(8);+m.setHeight(12);+hgp://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html+COMP+401+::+Spring+2012+ 9+public+class+MountainBike+{+++++public+int+cadence;+++++public+int+gear;+++++public+int+speed;++++++public+int+seatHeight;+++++++++++++public+void+setCadence(int+newValue)+{+++++++++cadence+=+newValue;+++++}++++++++++++++public+void+setGear(int+newValue)+{+++++++++gear+=+newValue;+++++}++++++++++++++public+void+applyBrake(int+decrement)+{+++++++++speed+a=+decrement;+++++}++++++++++++++public+void+speedUp(int+increment)+{+++++++++speed++=+increment;+++++}+++++public+void+setHeight(int+newValue)+{+++++++seatHeight+=+newValue;++++}++++++++++}+public+class+MountainBike+extends+Bicycle+{+....++++}+Extending+a+class+is+like+wriBng+a+new+class+that+has+all+the+same+details+of+the+original+class...+...+plus+adding+addiBonal+stuff+specific+to+subclass+IsaA+RelaBonships+for+Subclasses+• Like+interfaces,+“is+a”+relaBonship+is+transiBve+up+the+subclass+hierarchy.+– A+MountainBike+object+“is+a”+Bicycle+• Because+you+inherit+everything+your+parent+provides,+by+definiBon+you+also+implement+any+interfaces+your+parent+implements.+– And+so+on+all+the+way+up+the+hierarchy.+COMP+401+::+Spring+2012+ 10+IsaA+For+Subclasses+COMP+401+::+Spring+2012+ 11+class+A+implements+InterA+{+...+}+++class+B+extends+A+implements+InterB+{+...+}++++class+C+extends+B+implements+InterC+{+...+}+Objects+of+type+A,+implement+interface+InterA.++A+“is+a”+Inter+A+Objects+of+type+B,+implement+interface+InterB+and+InterA.++B+“is+a”+A++B+“is+a”+InterA++B+“is+a”+InterB+++Objects+of+type+C,+implement+interface+InterC,+InterB,+and+InterA.++C+“is+a”+A++C+“is+a”+B++C+“is+a”+InterA++C+“is+a”+InterB++C+“is+a”+InterC+++Object+• All+classes+inherit+from+Object+– Top+of+the+class+hierarchy.+– Since+every+class+must+inherit+from+Object,+don’t+actually+need+to+specify+it.+COMP+401+::+Spring+2012+ 12+public+class+MyClass+{+...+}+So+when+we+say+this:+ We+were+implicitly+doing+this:+public+class+MyClass+extends+Object+{+...+}+Object,+cont’d.+• Because+all+classes+implicitly+have+Object+as+a+superclass+ancestor...+– A+variable+with+data+type+Object+can+hold+anything.+• But+then+restricted+to+just+the+methods+that+are+defined+at+the+level+of+Object+• Public+methods+that+all+objects+have:+– public+boolean+equals(Object+o)+– public+String+toString()+COMP+401+::+Spring+2012+ 13+Instance+Fields+•
View Full Document