Lecture 15 Inheritance The Do ME example Database
Lecture 15 - Inheritance
The Do. ME example "Database of Multimedia Entertainment" n stores details about CDs and videos n n n CD: title, artist, # tracks, playing time, gotit, comment Video: title, director, playing time, got-it, comment allows (later) to search for information or print lists
Do. ME objects
Do. ME classes
Do. ME object model
Class diagram
CD source code public class CD { private String title; private String artist; private String comment; CD(String the. Title, String the. Artist) { title = the. Title; artist = the. Artist; comment = " "; } void set. Comment(String new. Comment) {. . . } String get. Comment() {. . . } void print() {. . . }
Video source code public class Video { private String title; private String director; private String comment; Video(String the. Title, String the. Direct) { title = the. Title; director = the. Direct; comment = " "; } void set. Comment(String new. Comment) {. . . } String get. Comment() {. . . } void print() {. . . }
class Database { private Array. List cds; private Array. List videos; . . . Database source code public void list() { for(Iterator iter = cds. iterator(); iter. has. Next(); ) { CD cd = (CD)iter. next(); cd. print(); System. out. println(); // empty line between items } for(Iterator iter = videos. iterator(); iter. has. Next(); ) { Video video = (Video)iter. next(); video. print(); System. out. println(); // empty line between items } } }
Critique of Do. ME n code duplication n n 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?
Using inheritance ** ‘IS-A’ relationship
Using inheritance n n n 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 public class Item {. . . } public class CD extends Item {. . . } no change here public class Video extends Item {. . . }
Superclass public class Item { private String title; private int playing. Time; private boolean got. It; private String comment; // constructors and methods omitted. }
Subclasses public class CD extends Item { private String artist; private int number. Of. Tracks; // constructors and methods omitted. } public class Video extends Item { private String director; // constructors and methods omitted. }
public class Item { private String title; private int playing. Time; private boolean got. It; private String comment; Inheritance and constructors /** * Initialise the fields of the item. */ public Item(String the. Title, int time) { title = the. Title; playing. Time = time; got. It = false; comment = ""; } // methods omitted }
Inheritance and public class CD extends Item { constructors private String artist; private int number. Of. Tracks; /** * Constructor for objects of class CD */ public CD(String the. Title, String the. Artist, int tracks, int time) { super(the. Title, time); artist = the. Artist; number. Of. Tracks = tracks; } // methods omitted }
Superclass constructor call n n Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) n n works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.
public class Database { private Array. List items; /** * Construct an empty Database. */ public Database() { items = new Array. List(); } /** * Add an item to the database. */ public void add. Item(Item the. Item) { items. add(the. Item); }. . . } New Database source code avoids code duplication in client!
Subtyping First, we had: public void add. CD(CD the. CD) public void add. Video(Video the. Video) Now, we have: public void add. Item(Item the. Item)
Object diagram
Review (so far) Inheritance (so far) helps with: n Avoiding code duplication n Code reuse n Easier maintenance n Extendibility
Adding more item types
- Slides: 23