You are on page 1of 10

Software Engineering

Lecture 4(b):
Polymorphic abstraction: an overview
2013

Outline
What is polymorphic abstraction?
Polymorphic procedure
Polymorphic data abstraction
Benefits
Design issues
Example

2013

FIT330 Software Engineering

What is polymorphic abstraction?


Polymorphism means to support multiple types
Polymorphic abstraction generalises an
abstraction to supports multiple types
Applies to all three abstractions:

2013

procedure: polymorphic w.r.t argument type


collection-based data abstraction (e.g. set):
polymorphic w.r.t the element type
iterator: polymorphic w.r.t underlying collection
type

FIT330 Software Engineering

Polymorphic procedure
/**
* @effects
*
<pre>
*
if v is null
*
throws NullPointerException
*
else if o is in v
*
returns an index where o is stored
*
(uses equals to compare o with elements of v)
*
else
*
throws NotFoundException</pre>
*/

public static int search(Vector v, Object o)


throws NotFoundException, NullPointerException

2013

FIT330 Software Engineering

Polymorphic data abstraction


Collections that store Object elements
Example: ch8.Set

2013

generalise IntSet to store abitrary objects

state in @overview how null object is handled

have same behaviours as IntSet

FIT330 Software Engineering

Set
public class Set {
private Vector els;
...
public boolean insert(Object x) throws
NullPointerException
...
}

2013

FIT330 Software Engineering

Benefits
Code re-use through type hierarchy:

extends the use of an abstraction

Ease maintenance:

2013

avoid interface update

FIT330 Software Engineering

Design issues
Can use a supertype more specific than
Object

e.g. Person, Comparable

equals of the element type must behave


correctly

2013

FIT330 Software Engineering

Example: create a set


Set s = new Set();
// store objects to collection
Object o = new String("abc");
s.insert(o);
o = new Integer(3);
s.insert(o);
// print elements
Iterator g = s.elements();
while (g.hasNext()) {
System.out.println(g.next());
}
2013

FIT330 Software Engineering

Retrieve elements with casting


// retrieve objects from set
//
(casting is required)
String s = (String) s.get(0);
Integer i = (Integer) s.get(1);

2013

FIT330 Software Engineering

10

You might also like