Lecture 8 - Inheritance

Lecture 8 - Inheritance - Definition of Inheritance Lecture...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 8 Inheritance The sharing of attributes and methods among classes. we take a class, and then define other classes based on the first one; the new classes inherit all the attributes and methods of the first one, but also have attributes and methods of their own. Definition of Inheritance Example: CD & Video classes 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() { . .. } ... } incomplete version! [ ] 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() { . .. } ... } incomplete version! [] Critique of CD/Video Classes • code duplication – CD and Video classes very similar (in large part identical) – makes maintenance difficult/more work – introduces danger of bugs through incorrect maintenance • code duplication also in collection class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Definition of Inheritance (cont) – Inheritance defines a relationship among classes where one class shares the structure and/or behaviour of one or more classes – Inheritance defines a hierarchy of abstractions in which a subclass inherits from one or more superclasses • With single inheritance, the subclass inherits from only one superclass • With multiple inheritance, the subclass inherits from more than one superclass (Java does not support this type) – Inheritance is an “is a” or “kind of” relationship Using Inheritance Using inheritance • define one superclass : Item • define subclasses for 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 { ... } public class Video extends Item { .. . } no change here change here Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. } Subclasses public class CD extends Item { private String artist; private int numberOfTracks; // constructors and methods omitted. } public class Video extends Item { private String director; // constructors and methods omitted. }
Background image of page 2
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 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 } Superclass constructor call • Subclass constructors must always contain a 'super' call.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Lecture 8 - Inheritance - Definition of Inheritance Lecture...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online