Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Inheritance.

Similar presentations


Presentation on theme: "Lecture 15 Inheritance."— Presentation transcript:

1 Lecture 15 Inheritance

2 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

3 DoME objects one object per CD or video; each object stores details for one item.

4 DoME classes add the obvious methods (getters and setters); not complete here - just examples add a print method to print out the details

5 DoME object model database object will hold two collections: one for CDs, one for videos

6 Class diagram class diagram is simple (ArrayList not shown in BlueJ)

7 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

8 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?)

9 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.

10 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)

11 ** ‘IS-A’ relationship
Using inheritance solution: inheritance. make superclass with common attributes, make subclasses ** ‘IS-A’ relationship

12 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

13 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 { ... }

14 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

15 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. }

16 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.

17 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.

18 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.

19 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

20 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

21 Object diagram database object will hold two collections: one for CDs, one for videos

22 Review (so far) Inheritance (so far) helps with:
Avoiding code duplication Code reuse Easier maintenance Extendibility

23 Adding more item types it is now much easier to add new types.
common attributes do not need to be rewritten.


Download ppt "Lecture 15 Inheritance."

Similar presentations


Ads by Google