Inheritance,+part+1+COMP+401,+Spring+2013+Lecture+8+2/4/2013+Assignment+1+• If+you+sBll+have+a+0+for+this+assignment,+keep+working+on+it.+– Free+pass+on+Assignment+1+– We’ll+conBnue+to+accept+submissions+and+resubmissions+for+full+credit++Assignment+2+• Passing+all+of+the+tests+will+get+you+at+least+7+out+of+10+points.+• Having+clear+and+organized+code+will+get+you+up+to+another+3.+– Do+not+think+of+this+as+30%+– Recall+explanaBon+about+how+grades+are+assigned.+• So+what+is+clear+and+organized?+– This+does+not+mean+“having+a+lot+of+comments”.+– As+best+you+can,+code+should+speak+for+itself+• Variable+names+should+be+descripBve+and+scoped+appropriately+• Extraneous+code+that+is+commented+out+should+be+removed+• Logic+of+the+code+should+be+easy+to+follow+– Elegance+Assignment+2+Tips+and+Hints+• Keep+working+on+it.+– If+it+passes+most+/+some+of+the+tests,+submit+now+and+then+submit+again+if+you+can+get+more+working.+• Follow+the+steps:+– Define+an+empty+class+with+the+right+name+• Declare+which+interfaces+it+will+implement+– Declare+instance+(i.e.,+object)+fields+• Make+them+private+(encapsulaBon)+• Choose+a+specific+representaBon+– Don’t+mix+together+two+different+approaches+– Define+the+constructor+• Assignment+specifies+form+that+must+be+supported.+– Write+each+of+the+required+public+methods+• One+at+a+Bme.+Write+a+tester+as+you+go+along.+Midterm+• Part+1:+MulBple+choice,+true/false+– Basic+Java+language+syntax+– OO+concepts+– Example:+• Which+of+these+is+NOT+a+valid+builtein+Java+data+type?+(More+than+one+answer+may+be+correct)+– a)+int+– b)+unsigned+short+– c)+char+– d)+double+– e)+true+Midterm+• Part+2:+Short+answer+/+calculaBon+– Evaluate+small+snippets+of+code++• Example:+– What+is+the+value+of+the+variable+bar+afer+the+following+code+executes?+int+bar;++int[]+a+=+{1,+2,+3,+4,+5};++int[]+b+=+a;++b[2]+=+a[1];++bar+=+a[2];++Midterm+• Part+III:+Understanding+Code+• I’ll+provide+the+code+for+one+or+more+classes+and+then+ask+quesBons+like:+– IdenBfy+all+of+the+instance+fields+of+class+A.+– IdenBfy+all+of+the+class+methods+of+class+A.+– What+is+the+return+type+of+method+m+of+class+B?+Midterm+• Part+IV:+WriBng+Code+• Asked+to+define+one+or+more+simple+classes+according+to+some+specificaBon.+– May+be+provided+with+a+parBal+implementaBon+that+you+have+to+complete.+Topics+/+Concepts+• Basic+Java+syntax+– Variable+names,+builtein+data+types+• Reference+types+vs.+value+types+• Arrays+• Concepts+of+abstracBon+and+encapsulaBon+• JavaBeans+convenBons+• Interfaces+• Iterator+Recap+of+Interfaces+• A+“contract”+for+behavior.+– Defined+by+a+set+of+method+signatures.+– Acts+as+a+data+type.+– No+implementaBon.+• Specific+classes+implement+the+interface+Song+and+Video+as+Media+public+interface+Media+{+++++int+getLengthInSeconds();+++++double+getLengthInMinutes();+++++int+getRaBng();+++++void+setRaBng(int+new_raBng);+++++String+getName();+}+public+class+Song+implements+Media+{+....+public+class+Video+implements+Media+{+....+We+expect+Song+and+Video+to+have+methods+matching+those+specified+in+Media.+IseA+and+casBng+• A+class+that+implements+an+interface+creates+an+“isea”+relaBonship+between+class+data+type+and+the+interface+data+type.+– A+implements+B+=>+A+“is+a”+B+• Song+is+a+Media+• Video+is+a+Media+• CasBng+allowed+across+an+isea+relaBonship.+12+Song+s+=+new+Song();+Media+m;++m+=+(Media)+s;+Video+v+=+new+Video();+Media+m;++m+=+(Media)+v;+Video+v+=+new+Video();+Song+s;++s+=+(Song)+v;+Inheritance+• What+is+inheritance+in+real+life?+– CharacterisBcs+/+resources+that+you+receive+from+your+parents+• Get+these+automaBcally.+• Part+of+who+you+are.+• Similar+idea+in+objecteoriented+programming.+– In+Java,+concept+of+inheritance+applied+to+both+interfaces+and+classes.+• Both+signaled+by+the+keyword+“extends”+• Similar+in+concept,+but+details+are+disBnctly+different.+– Class+inheritance+more+complex.+Extending+Interfaces+• Adds+methods+to+contract.+– Original:++• parent+interface,+super+interface+– New:+• subinterface,+child+interface,+extended+interface+• Created+by+using+the+“extends”+keyword.+public+interface+CompressedMedia+extends+Media+{++++int+getCompressedSize();++++int+getUncompressedSize();++++Media+uncompress();+}+Extension+Creates+Hierarchy+• IseA+relaBonship+is+transiBve+up+the+hierarchy.+Media+Compressed+Media+extends+public+class+Song+implements+CompressedMedia+{+...+Methods+for+both+must+be+provided.+Song+s+=+new+Song();++CompressedMedia+cm+=+(CompressedMedia)+s;++Media+m+=+(Media)+s;++Song+s2+=+(Song)+m;+OK&because&s&“is&a”&Compressed&Media&OK&because&s&is&a&Media&by&virtue&of&extension.&Cas;ng&from&interface&back&to&specific&object&type&is&&allowed,&but&at&run;me,&if&the&object’s&type&does¬&actually&match,&a&run;me&excep;on&will&be&thrown.&Extension+vs.+ComposiBon+• Interface+extension+appropriate+when+addiBonal+methods+make+no+sense+without+methods+of+the+parent+interface+• AlternaBvely,+can+compose+mulBple+interfaces+together+as+facets+of+an+object.+Extension+vs.+ComposiBon+public+interface+Compressed+{++++int+getCompressedSize();++++int+getUncompressedSize();++++Media+uncompress();+}+public+interface+Media+{+++++int+getLengthInSeconds();+++++double+getLengthInMinutes();+++++int+getRaBng();+++++void+setRaBng(int+new_raBng);+++++String+getName();+}+public+class+Song+implements+Compressed,+Media+{+...+Instead&of&extending&Media,&Compressed&is&a&separate&interface&and&Song&implements&both.&Song+s+=+new+Song();++Media+m+=+(Media)+s;++Compressed+c+=+(Compressed)+s;+Song&“is&a”&Media&AND&Song&“is&a”&Compressed.&Assignment+2+Revisited+• As+currently+specified+– TriangleImpl+composes+together+the+interfaces+Polygon+and+Triangle+– RectangleImpl+composes+together+the+interfaces+Polygon+and+Rectangle+• Since+Triangle+and+Rectangle+are+by+definiBon+also+Polygons,+natural+to+use+subinterface+–
View Full Document