Computer science 209
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Computer Science 209 PowerPoint PPT Presentation


  • 40 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Computer Science 209

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Computer science 209

Computer Science 209

Software Development

Inheritance and Composition


Two types of class relations

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

= extends

= composes

A

B


Java stack class

Java 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


Use composition

Use Composition

<<Interface>>

Iterable

= extends

<<Interface>>

Collection

= implements

Abstract

Collection

<<Interface>>

List

= composes

ArrayStack

AbstractList

ArrayStack inherits Collection operationsand uses List operations in its implementation

Vector

ArrayList

Stack


Implement with inheritance

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 the constructor,

// are inherited

}

Vector

Stack


Implement with composition

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 composition1

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(){

// We will get to this soon!

}

Last three methods required by AbstractCollection

AbstractCollection

ArrayStack

ArrayList


Other implementations of stacks

Other Implementations of Stacks

<<Interface>>

Iterable

<<Interface>>

Collection

Abstract

Collection

LinkedStack

ArrayStack

LinkedList

ArrayList


Add a single stack interface

Add a Single Stack Interface

<<Interface>>

Iterable

<<Interface>>

Collection

= extends

<<Interface>>

TrueStack

Abstract

Collection

= implements

LinkedStack

ArrayStack

LinkedList

ArrayList


Using the stacks

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 and the method addAll come from AbstractCollection


Defining a stack interface

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

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


  • Login