Lecture 15 Inheritance
The DoME example "Database of Multimedia Entertainment" stores details about CDs and videos CD: title, artist, # tracks, playing time, got-it, comment Video: title, director, playing time, got-it, comment allows (later) to search for information or print lists database to store details of all CDs and videos I know
DoME objects one object per CD or video; each object stores details for one item.
DoME classes add the obvious methods (getters and setters); not complete here - just examples add a print method to print out the details
DoME object model database object will hold two collections: one for CDs, one for videos
Class diagram class diagram is simple (ArrayList not shown in BlueJ)
CD source code public class CD { private String title; private String artist; private String comment; CD(String theTitle, String theArtist) { title = theTitle; artist = theArtist; comment = " "; } void setComment(String newComment) { ... } String getComment() void print() ... CD source code extract from CD code - nothing unusual
Video source code public class Video { private String title; private String director; private String comment; Video(String theTitle, String theDirect) { title = theTitle; director = theDirect; comment = " "; } void setComment(String newComment) { ... } String getComment() void print() ... Video source code extract from video code (how does it differ from CD code?)
Database source code class Database { private ArrayList cds; private ArrayList videos; ... public void list() { for(Iterator iter = cds.iterator(); iter.hasNext(); ) { CD cd = (CD)iter.next(); cd.print(); System.out.println(); // empty line between items } for(Iterator iter = videos.iterator(); iter.hasNext(); ) { Video video = (Video)iter.next(); video.print(); Database source code extract from database code.
Critique of DoME code duplication CD and Video classes very similar (large part are identical) makes maintenance difficult/more work introduces danger of bugs through incorrect maintenance code duplication also in Database class What if I want to add another class? we note a lot of code duplication. this is one problem with this solution (there are others)
** ‘IS-A’ relationship Using inheritance solution: inheritance. make superclass with common attributes, make subclasses ** ‘IS-A’ relationship
Using inheritance define one superclass : Item define subclasses: Video and CD the superclass defines common attributes the subclasses inherit the superclass attributes the subclasses add own attributes
Inheritance in Java no change here change here public class Item { ... } change here public class Video extends Item { ... } syntax for inheritance: extends keyword public class CD extends Item { ... }
Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. } we define common fields in superclass
Subclasses public class CD extends Item { private String artist; private int numberOfTracks; // constructors and methods omitted. } we add subclass fields; inherit superclass fields (note extends keyword) subclass objects will have all fields. public class Video extends Item { private String director; // constructors and methods omitted. }
Inheritance and constructors public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; /** * Initialise the fields of the item. */ public Item(String theTitle, int time) title = theTitle; playingTime = time; gotIt = false; comment = ""; } // methods omitted Inheritance and constructors how do we initialise the fields? superclass: nothing unusual.
Inheritance and constructors public class CD extends Item { private String artist; private int numberOfTracks; /** * Constructor for objects of class CD */ public CD(String theTitle, String theArtist, int tracks, int time) super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } // methods omitted subclass: must call superclass constructor! Must take values for all fields that we want to initialise.
Superclass constructor call Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.
New Database source code public class Database { private ArrayList items; /** * Construct an empty Database. */ public Database() items = new ArrayList(); } * Add an item to the database. public void addItem(Item theItem) items.add(theItem); ... avoids code duplication in client! note: code duplication in class Database removed as well! only on field, one ArrayList creation, on add method
Subtyping First, we had: Now, we have: public void addCD(CD theCD) public void addVideo(Video theVideo) Now, we have: public void addItem(Item theItem) Subtyping
Object diagram database object will hold two collections: one for CDs, one for videos
Review (so far) Inheritance (so far) helps with: Avoiding code duplication Code reuse Easier maintenance Extendibility
Adding more item types it is now much easier to add new types. common attributes do not need to be rewritten.