Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Software Development Inheritance and Composition.

Similar presentations


Presentation on theme: "Computer Science 209 Software Development Inheritance and Composition."— Presentation transcript:

1 Computer Science 209 Software Development Inheritance and Composition

2 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

3 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

4 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

5 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

6 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

7 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

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

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

10 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

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

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


Download ppt "Computer Science 209 Software Development Inheritance and Composition."

Similar presentations


Ads by Google