computer science 209 software development inheritance and composition

12
Computer Science 209 Software Development Inheritance and Composition

Upload: josephine-york

Post on 21-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science 209 Software Development Inheritance and Composition

Computer Science 209

Software Development

Inheritance and Composition

Page 2: 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

Page 3: Computer Science 209 Software Development Inheritance and Composition

Java Stack Class

<<Interface>>Collection

<<Interface>>Iterable

<<Interface>>List

Vector

Stack

AbstractCollection

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

Page 4: Computer Science 209 Software Development Inheritance and Composition

Use Composition

<<Interface>>Collection

<<Interface>>Iterable

<<Interface>>List

Vector

Stack

AbstractCollection

AbstractList

ArrayStack inherits Collection operations and uses List operations in its implementation

ArrayStack

ArrayList

= extends

= implements

= composes

Page 5: Computer Science 209 Software Development Inheritance and Composition

Implement with Inheritancepublic 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 the constructor, // are inherited}

Vector

Stack

Page 6: Computer Science 209 Software Development Inheritance and Composition

Implement with Compositionpublic 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); }

AbstractCollection

ArrayStack

ArrayList

Use list instead of this

Page 7: Computer Science 209 Software Development Inheritance and Composition

Implement with Compositionpublic 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(); }

AbstractCollection

ArrayStack

ArrayList

Last three methods are required by AbstractCollection

Page 8: Computer Science 209 Software Development Inheritance and Composition

Other Implementations of Stacks

<<Interface>>Collection

<<Interface>>Iterable

AbstractCollection

ArrayStack

ArrayList

LinkedStack

LinkedList

Page 9: Computer Science 209 Software Development Inheritance and Composition

Add a Single Stack Interface

<<Interface>>Collection

<<Interface>>Iterable

AbstractCollection

ArrayStack

ArrayList

LinkedStack

LinkedList

<<Interface>>TrueStack

= extends

= implements

Page 10: Computer Science 209 Software Development Inheritance and Composition

Using the Stacks

TrueStack<String> s1 = new ArrayStack<String>();

TrueStack<Integer> s2 = new LinkedStack<Integer>();

// Push a bunch of ints onto s2

for (int i : s2) s1.push(i + "");

TrueStack<String> s3 = new LinkedStack<String>();

s3.addAll(s1);

The for loop comes from the Iterable interface

The method addAll comes from AbstractCollection

Page 11: Computer Science 209 Software Development Inheritance and Composition

Defining a Stack Interface

public interface TrueStack<E> extends Collection<E>{

public E pop();

public void push(E newElement);

public E peek();} <<Interface>>

Collection

<<Interface>>Iterable

<<Interface>>TrueStack

Page 12: Computer Science 209 Software Development Inheritance and Composition

Implementing a Stack Interface

public class ArrayStack<E> extends AbstractCollection<E> implements TrueStack<E>{ // As before}

<<Interface>>Collection

<<Interface>>Iterable

<<Interface>>TrueStack

ArrayStack

AbstractCollection