computer science 209

15
Computer Science 209 Software Development Abstract Classes

Upload: aristotle-richardson

Post on 02-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Computer Science 209. Software Development Abstract Classes. Implementing a Stack Resource. Iterable. Some behavior is provided by inheritance and some behavior is provided by composition. Collection. TrueStack. Abstract Collection. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Science 209

Computer Science 209

Software Development

Abstract Classes

Page 2: Computer Science 209

Implementing a Stack Resource

<<Interface>>Collection

<<Interface>>Iterable

<<Interface>>TrueStack

AbstractCollection

ArrayStack

ArrayList

LinkedStack

LinkedList

Some behavior is provided by inheritance and some behavior is provided by composition

Page 3: Computer Science 209

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 completed method addAll comes from AbstractCollection

Page 4: Computer Science 209

Defining a Stack Interface

import java.util.Collection;

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

public E pop();

public void push(E newElement);

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

Collection

<<Interface>>Iterable

<<Interface>>TrueStack

A Java interface can be compiled prior to developing any implementing classes

Page 5: Computer Science 209

The Iterable Interfaceimport java.util.Iterator;

public interface Iterable<E>{

public Iterator<E> iterator();}

Java automatically uses an iterator to generate code for a for-each loop

All collections are required to implement the iterator method

<<Interface>>Collection

<<Interface>>Iterable

Page 6: Computer Science 209

The Collection Interfacepublic interface Collection<E> extends Iterable<E>{

public boolean add(E newElement); public boolean addAll(Collection<E> col); public void clear(); public boolean contains(Object o); public boolean containsAll(Collection<E> col); public boolean isEmpty(); public boolean remove(Object o); public boolean removeAll(Collection<E> col); public boolean retainAll(Collection<E> col); public int size();}

Methods in green must be included but need not be supported

<<Interface>>Collection

<<Interface>>Iterable

Page 7: Computer Science 209

Implementing a Stack Interfaceimport java.util.*;

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

<<Interface>>Collection

<<Interface>>Iterable

<<Interface>>TrueStack

ArrayStack

AbstractCollection

Page 8: Computer Science 209

The AbstractCollection Class

• An abstract class serves as a repository of methods and/or data that are implemented in the same manner for any subclasses

• Never instantiated, but always subclassed

• Some methods are implemented, others remain abstract (to be implemented by subclasses)

Page 9: Computer Science 209

The AbstractCollection Classabstract public class AbstractCollection<E> implements Collection<E>{ abstract public boolean add(E newElement); public boolean addAll(Collection<E> col){} public void clear(){} public boolean contains(Object o){} public boolean containsAll(Collection<E> col){} public boolean isEmpty(){} public boolean remove(Object o){} public boolean removeAll(Collection<E> col){} public boolean retainAll(Collection<E> col){} abstract public int size(); abstract public Iterator<E> iterator();}

abstract methods are completed in the subclasses

Page 10: Computer Science 209

The AbstractCollection Classabstract public class AbstractCollection<E> implements Collection<E>{ public boolean contains(Object o){ for (E element : this) if (element.equals(o)) return true; return false; }

public boolean containsAll(Collection<E> col){ for (E element : col) if (! this.contains(element)) return false; return true; } ...}

Leverage the for-each loop and other methods!

Page 11: Computer Science 209

The AbstractCollection Classabstract public class AbstractCollection<E> implements Collection<E>{ public boolean addAll(Collection<E> col){ boolean allAdded = true; for (E element : col) allAdded = allAdded && this.add(element); return allAdded; }

public boolean isEmpty(){ return this.size() == 0; } ...}

Methods that remove elements require the use of an explicit iterator – a topic for another day!

Page 12: Computer Science 209

How Does This Work?abstract public class AbstractCollection<E> implements Collection<E>{ public boolean addAll(Collection<E> col){ for (E element : col) this.add(element); return true; }

Java finds c1’s addAll method in its superclass, AbstractCollection

Java then finds this’s add method in its original class, whatever that is

AbstractCollection

ASubclass

c1.addAll(c2);

Page 13: Computer Science 209

Dynamic Binding of Methods

• The Java compiler can determine that the methods addAll and add are included in a given collection class (or its ancestors)

• However, the implementation of the add method used in addAll cannot be determined until runtime, when the actual class of a collection is available

Page 14: Computer Science 209

Interface or Abstract Class?

• An interface contains only method headers, and specifies a set of abstract operations

• An abstract class contains implemented methods and data for a set of subclasses, as well as abstract methods

Page 15: Computer Science 209

Abstract Methods

• An abstract method must be declared in an abstract class (and only there) when that method is used in other methods implemented in that class, but the implementation of the abstract method is deferred to subclasses

• Examples: add and iterator