{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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. }
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.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern