Software Development Inheritance and Composition

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Computer Science 112 Fundamentals of Programming II List Iterators.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Java Review Interface, Casting, Generics, Iterator.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
Computer Science 209 Software Development Iterators.
COMP 103 Linked Stack and Linked Queue.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Computer Science 209 Software Development Java Collections.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Computer Science 209 Software Development Inheritance and Composition.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Computer Science 209 Software Development Refactoring.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Click to edit Master text styles Stacks Data Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Iterators.
Sections 3.4 Formal Specification
Some Collections: BAGS, SETS, and STACKS
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Software Development Java Collections
Software Development Iterators
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Implementing ArrayList Part 1
Java Collections Overview
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
CSC 1052 Stacks 1/11/2019.
Collections Framework
JCF Collection classes and interfaces
Lecture 2: Stacks and Queues
slides created by Alyssa Harding
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Software Development Inheritance and Composition Computer Science 209 Software Development Inheritance and Composition

java.util.stack Class <<Interface>> Iterable <<Interface>> Collection Stack inherits List operations as well as Collection operations Gets Vector to manage its data and provide some of its methods, but at a price! Abstract Collection <<Interface>> List = extends AbstractList = implements Vector Stack

The Price of java.util.Stack Not only can we push, pop, and peek, but we can also get, set, remove, or insert at any position This implementation does not really live up to the spirit of a stack, as would be specified in a much more restricted interface Inheritance is a convenience, but it can be abused

Two Types of Class Relations Inheritance: Class A inherits the attributes and operations of class B, and then adds some of its own; clients can run B’s methods as well as A’s methods Composition: Class A contains an instance variable of class B; clients run only A’s methods, not B’s methods (directly) B A = extends = composes A B

Use Composition = extends = implements = composes <<Interface>> Iterable = extends <<Interface>> Collection = implements Abstract Collection <<Interface>> List = composes ArrayStack AbstractList ArrayStack inherits Collection operations but uses List operations in its implementation Vector ArrayList Stack

Implement with Inheritance public class Stack<E> extends Vector<E>{ public E pop(){ return this.remove(this.size() – 1); } public void push(E newElement){ this.add(newElement); public E peek(){ return this.get(this.size() – 1); // The rest, including a constructor, // are inherited Vector Stack E is a formal type parameter, which is replaced by an actual type when Stack is instantiated

Implement with Composition public class ArrayStack<E> extends AbstractCollection<E>{ private List<E> list; public ArrayStack(){ list = new ArrayList<E>(); } public E pop(E newElement){ list.remove(list.size() - 1); public void push(E newElement){ list.add(newElement); public E peek(){ return list.get(list.size() – 1); Use list instead of this AbstractCollection ArrayStack ArrayList

Implement with Composition public class ArrayStack<E> extends AbstractCollection<E>{ private List<E> list; public ArrayStack(){ list = new ArrayList<E>(); } public int size(){ return list.size(); public boolean add(E newElement){ this.push(newElement) return true; public Iterator<E> iterator(){ return list.iterator(); Last three methods are required by AbstractCollection AbstractCollection ArrayStack ArrayList

Another Implementation of Stacks <<Interface>> Iterable <<Interface>> Collection Abstract Collection LinkedStack ArrayStack LinkedList ArrayList

Add a Single Stack Interface Iterable <<Interface>> Collection = extends <<Interface>> TrueStack Abstract Collection = implements LinkedStack ArrayStack LinkedList ArrayList

Using the Stacks TrueStack<String> s1 = new ArrayStack<String>(); TrueStack<Integer> s2 = new LinkedStack<Integer>(); Supply actual types as arguments for the type parameters

Using the Stacks Wrapping of ints to Integers is automatic TrueStack<String> s1 = new ArrayStack<String>(); TrueStack<Integer> s2 = new LinkedStack<Integer>(); for (int i = 1; i <= 10,; i++) s2.push(i); Wrapping of ints to Integers is automatic

Using the Stacks The for loop comes from the Iterable interface TrueStack<String> s1 = new ArrayStack<String>(); TrueStack<Integer> s2 = new LinkedStack<Integer>(); for (int i = 1; i <= 10,; i++) s2.push(i); for (int i : s2) s1.push(i + ""); The for loop comes from the Iterable interface In what order are a stack’s items visited by the iterator????

Using the Stacks The method addAll comes from AbstractCollection TrueStack<String> s1 = new ArrayStack<String>(); TrueStack<Integer> s2 = new LinkedStack<Integer>(); for (int i = 1; i <= 10; i++) s2.push(i); for (int i : s2) s1.push(i + ""); TrueStack<String> s3 = new LinkedStack<String>(); s3.addAll(s1); The method addAll comes from AbstractCollection

Defining a Stack Interface public interface TrueStack<E> extends Collection<E>{ public E pop(); public void push(E newElement); public E peek(); } <<Interface>> Iterable <<Interface>> Collection <<Interface>> TrueStack

Implementing a Stack Interface public class ArrayStack<E> extends AbstractCollection<E> implements TrueStack<E>{ // As before } <<Interface>> Iterable <<Interface>> Collection <<Interface>> TrueStack Abstract Collection ArrayStack

Interface Polymorphism When two classes implement the same interface, the implemented methods are polymorphic, and we have interface polymorphism <<Interface>> Iterable <<Interface>> Collection <<Interface>> TrueStack LinkedStack ArrayStack

Structural Polymorphism When two classes inherit implementations of the same methods, the implemented methods are polymorphic, and we have structural polymorphism <<Interface>> Iterable <<Interface>> Collection <<Interface>> TrueStack Abstract Collection LinkedStack ArrayStack

For Friday Abstract Classes