Professional Documents
Culture Documents
Object Oriented
Programming Techniques
Compositional Techniques
with Classes and Interfaces
Aggregation vs. Inheritance
Professor Ioan Salomie
Spring 2010
Objective
A
comparative approach of
aggregation and inheritance as the
main techniques of software code
reuse
Introduction
Consider
the implementation of a
class Stack using a class Vector
Note. Well not consider the Stack
class implemented by Java library
Stack
behavior
push
pop
top
isEmpty
isFull
Vector
class Vector {
// see if collection is empty
public boolean isEmpty() { }
// return size of collection
public int size() { }
// add element to the end of collection
public void addElement (Object value) { }
// return last element in the collection
public Object lastElement() { }
// remove element at given index
public Object removeElementAt (int index) { }
// other class resources
}
Aggregation technique
Stack uses Vector
A
Aggregation technique
Stack uses Vector
class Stack {
// structure
private Vector stk;
// constructor
public Stack() { stk = new Vector();}
// behavior
public Object push(Object o) {
Object retObject;
if(isFull()) retObject = null;
else {
stk. addElement(o);
retObject = o;
}
return retObject;
}
6
Aggregation technique
Stack uses Vector
public Object pop() {
Object retObject;
if(stk.isEmpty()) retObject = null;
else {
retObject = stk.lastElement();
stk.removeElementAt(stk.size()-1);
}
return retObject;
}
public Object top()
Object retObject;
if(stk.isEmpty()) retObject = null;
else retObject = stk.lastElement();
return retObject;
}
public boolean isFull() { return false;}
public boolean isEmpty() { return stk.isEmpty();}
} // end class
7
Aggregation technique
Stack uses Vector
Stack
Inheritance technique
Stack inherits from Vector
The
The
Inheritance technique
Stack inherits from Vector
class Stack extends Vector {
public Object push(Object elem){
Object retObject;
if(isFull()) retObject = null;
else { addElement(elem); retObject = elem; }
return retObject;
}
public Object pop() {
Object retObject;
if(isEmpty()) retObject = null;
else {
retObject = elementAt(size()-1);
removeElementAt(size() 1);
}
return retObject;
}
public Object top() {
Object retObject;
if(isEmpty()) retObject = null;
else retObject = lastElement();
return retObject;
}
public boolean isFull() { return false;}
}
10
Inheritance technique
Stack inherits from Vector
Stack
constructor
Method isEmpty()
11
Simple technique
All the involved elements are clearly
highlighted
Clearly shows all available operations
for the abstraction Stack
Longer code, clear operations
A user (programmer) looks only at the
class supplied code in order use stack
facilities
Compositions are very simple to be
changed by using other structural
components.
Examples
12
Semantic differences
Less overhead in execution, than the
composition
13
(cont.)
Error prone
Examples
14
Inheritance
Dynamic composition
The link between the new abstraction
and the older abstraction is
created/changed at run time
15