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 De ne an empty class with the right name Declare which interfaces it will implement Declare instance i e object elds Make them private encapsulaBon Choose a speci c representaBon Don t mix together two di erent approaches De ne the constructor Assignment speci es 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 built in 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 elds 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 de ne one or more simple classes according to some speci caBon May be provided with a parBal implementaBon that you have to complete Topics Concepts Basic Java syntax Variable names built in data types Reference types vs value types Arrays Concepts of abstracBon and encapsulaBon JavaBeans convenBons Interfaces Iterator Recap of Interfaces A contract for behavior De ned by a set of method signatures Acts as a data type No implementaBon Speci c 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 speci ed in Media Is A and casBng A class that implements an interface creates an is a 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 is a relaBonship 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 12 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 object oriented 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 di erent 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 Is A relaBonship is transiBve up the hierarchy Methods for both must be provided Media extends Compressed Media public class Song implements CompressedMedia Song s new Song OK because s is a CompressedMedia cm CompressedMedia s Compressed Media Media m Media s OK because s is a Media by virtue of extension Cas ng from interface back to speci c object type is Song s2 Song m allowed but at run me if the object s type does not 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 Instead of extending Media Compressed is a separate interface and Song implements both public interface Media int getLengthInSeconds double getLengthInMinutes int getRaBng void setRaBng int new raBng String getName public class Song implements Compressed Media 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 speci ed TriangleImpl composes together the interfaces Polygon and Triangle RectangleImpl composes together the interfaces Polygon and Rectangle Since Triangle and Rectangle are by de niBon also Polygons natural to use subinterface De ne Triangle Rectangle as extensions of Polygon
View Full Document
Unlocking...