Lecture 15 Inheritance.

Slides:



Advertisements
Similar presentations
FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.
Advertisements

OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
Inheritance in collections Week Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution.
CS1054: Lecture 18 - Inheritance. The DoME example "Database of Multimedia Entertainment" stores details about CDs and videos –CD: title, artist, # tracks,
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Improving structure with inheritance (Chapters 8 and 9)
Improving structure with inheritance
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Grouping Objects 3 Iterators, collections and the while loop.
Inheritance Chapter 8.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
CPSC150 Abstract Classes and Interfaces Chapter 10.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Programming Fundamentals 2: Inheritance/ F II Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures,
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Improving structure with inheritance 3.0. The media project stores details about CDs and DVDs –CD: title, artist, number of tracks, playing time, got-it,
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
9 Improving structure with inheritance
Sixth Lecture ArrayList Abstract Class and Interface
More about inheritance
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Lecture 19 - Inheritance (Contd).
Java Programming Language
More about inheritance
ITunes Lab Copyright © 2012 Pearson Education, Inc.
COS 260 DAY 18 Tony Gauvin.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Objects First with Java
Object-Oriented Programming
Java Programming with BlueJ Objectives
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Improving structure with inheritance
Presentation transcript:

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.