Computer Science 209 Software Development Inheritance and Composition.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

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.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
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.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
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.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
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.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Computer Science 209 Software Development Packages.
David Stotts Computer Science Department UNC Chapel Hill.
Click to edit Master text styles Stacks Data Structure.
Iterators.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Some Collections: BAGS, SETS, and STACKS
Software Development Java Collections
Software Development Iterators
Stacks.
Software Development Inheritance and Composition
Implementing ArrayList Part 1
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
CSC 1052 Stacks 1/11/2019.
Collections Framework
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
slides created by Alyssa Harding
Stacks.
Presentation transcript:

Computer Science 209 Software Development Inheritance and Composition

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 B A A B = extends = composes

Java Stack Class > Collection > Iterable > List Vector Stack Abstract Collection AbstractList 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! = extends = implements

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

Implement with Inheritance public class Stack extends Vector { 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 the constructor, // are inherited } Vector Stack

Implement with Composition public class ArrayStack extends AbstractCollection { private List list; public ArrayStack(){ list = new ArrayList (); } 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); } AbstractCollection ArrayStack ArrayList Use list instead of this

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

Other Implementations of Stacks > Collection > Iterable Abstract Collection ArrayStack ArrayList LinkedStack LinkedList

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

Using the Stacks TrueStack s1 = new ArrayStack (); TrueStack s2 = new LinkedStack (); // Push a bunch of ints onto s2 for (int i : s2) s1.push(i + ""); TrueStack s3 = new LinkedStack (); s3.addAll(s1); The for loop comes from the Iterable interface The method addAll comes from AbstractCollection

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

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