Inheritance in collections Week 16 3.0. Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
OOP (Java): Inheritance/ OOP Objectives – –to introduce inheritance, superclasses, subclasses, polymorphic data structures, and wrapper.
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#
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
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.
Inheritance Chapter 8.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Grouping Objects 1 Introduction to Collections.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Programming Fundamentals 2: Inheritance/ F II Objectives – –the use of super, overriding, method polymorphism (dynamic binding), protected.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CS2110: SW Development Methods Inheritance in OO and in Java Part 1: Introduction Readings: A few pages in Ch. 2 of MSD text introduce this Section 3.3.
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,
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Objects First With Java A Practical Introduction Using BlueJ Casting Week
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.
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.
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.
JAVA CLASSES, METHODS AND VARIABLES.  Defined Noun: A set or category of things having some property or attribute in common and differentiated from others.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
IT 210 Week 8 Individual Object-Oriented Design To purchase this material link Week-8-Individual-Object-Oriented-Design.
9 Improving structure with inheritance
Sections Inheritance and Abstract Classes
More about inheritance
Problem Solve Suppose a librarian needs to keep track of all the books in a library. How would we write a program to solve this problem?
Chapter 19 Generics Dr. Clincy - Lecture.
Lecture 19 - Inheritance (Contd).
More about inheritance
ITunes Lab Copyright © 2012 Pearson Education, Inc.
COS 260 DAY 18 Tony Gauvin.
Building Java Programs
Building Java Programs
Building Java Programs
Objects First with Java
Comp1202: Inheritance I Super and Sub-classes.
F II 8. More on Inheritance Objectives
Building Java Programs
Chapter 10: Void Functions
Improving structure with inheritance
Lecture 15 Inheritance.
Presentation transcript:

Inheritance in collections Week

Main concepts to be covered Inheritance in ArrayList objects Subtyping Substitution

The DoME example "Database of Multimedia Entertainment" stores details about CDs and DVDs –CD: title, artist, # tracks, playing time, got- it, comment –DVD: title, director, playing time, got-it, comment allows details of items to be printed

DoME objects

DoME classes top half shows fields bottom half shows methods

DoME object model

Class diagram

public class Database{ private ArrayList cds ; private ArrayList dvds ; public Database() { cds = new ArrayList (); dvds = new ArrayList (); } public void addCD(CD theCD) { cds.add(theCD); } public void addDVD(DVD theDVD) { dvds.add(theDVD); } Database source code

public void list() { for(CD cd : cds) { cd.print(); System.out.println(); } for(DVD dvd : dvds) { dvd.print(); System.out.println(); } Database source code

Critique of DoME code duplication in CD and DVD classes also code duplication in Database class –makes maintenance difficult/more work –introduces danger of bugs through incorrect maintenance

Using inheritance

private ArrayList items; public Database() { items = new ArrayList (); } Old New Database source code private ArrayList cds; private ArrayList dvds; public Database() { cds = new ArrayList (); dvds = new ArrayList (); }

public void addItem(Item theItem) { items.add(theItem); } Old New Database source code public void addCD(CD theCD) { cds.add(theCD); } public void addDVD(DVD theDVD) { dvds.add(theDVD); }

public void list() { for(Item item : items) { item.print(); System.out.println(); } Old New Database source code public void list() { for(CD cd : cds) { cd.print(); System.out.println(); } for(DVD dvd : dvds) { dvd.print(); System.out.println(); }

Subtyping First, we had: public void addCD(CD theCD) public void add DVD ( DVD the DVD ) Now, we have: public void addItem(Item theItem) We call this method with either a CD object or a DVD object as a parameter

Subclasses and subtyping Classes define types. Subclasses define subtypes. Objects of subclasses can be used where objects of supertypes are required. (This is called substitution.)

Subtyping and assignment Vehicle v1 = new Vehicle();Vehicle v2 = new Car();Vehicle v3 = new Bicycle(); subclass objects may be assigned to superclass variables

Subtyping and parameter passing public class Database { public void addItem(Item theItem) {... } DVD dvd = new DVD(...); CD cd = new CD(...); database.addItem(dvd); database.addItem(cd); subclass objects may be passed to superclass parameters

Object diagram

Class diagram

Review Inheritance can be used in collections Variables can hold subtype objects Subtypes can be used wherever supertype objects are expected (substitution)