08interfaces PDF
08interfaces PDF
ABSTRACT CLASSES
= new ArrayQueue(15);
= new CircularQueue(22);
15);
15);
Questions
Why would it be pointless to:
Allow members of an interface to be private?
Create an instance of an interface?
Would interfaces be useful if variables in Java didnt need to have their types
declared?
The problem
We want to provide a sorting service, but we need our subscribers to ensure
that they give us only objects that have a particular property: they must be
comparable.
An interface can help express this.
The comparable idea is so useful that it is in the Java API.
Interface Comparable
Comparable is part of the Java API. It contains only one method:
public interface Comparable<T> {
/**
* Compares this object with o for order. Returns
* a negative integer, zero, or a positive integer
* as this object is less than, equal to, or
* greater than o.
*/
int compareTo(T o);
}
10
11
A Design Pattern
Suppose we want to go through the elements contained in an object, doing
something with each one.
Your first thought: go through by index.
// LinkedLists have elements, like Vector or an array.
LinkedList list = new LinkedList();
list.add(...);
...
for (int i = 0; i != list.size(); ++i) {
... do something with list.get(i) ...
}
For many data structures (LinkedList is one of them), getting the element
at a particular index isnt very fast, but moving from one element to the next
is fast.
12
13
Design Patterns
An iterator is an example of a design pattern.
A design pattern is a solution to a common software design problem. It is
a general, flexible template that can be applied in many different situations,
and so it supports software reuse.
The classic reference on this topic is Design Patterns, by Gamma, Helm,
Johnson and Vlissides.
Iterators are such a good idea that the Java API already has an interface for
them.
14
16
17
18
21
Eg 2: removeLess
To call removeLess, Iterator had to be implemented for the first argument.
We wanted to remove smaller things from a Vector. Vectors arent themselves iterators the class Vector doesnt implement Iterator. But it
has a method that will give you an Iterator over its elements.
The class Vector does implement Iterable it promises to provide the
method iterator().
23
24
25
26
Abstract Classes
An abstract class is halfway between a class and an interface.
Its declared like a class, but with the keyword abstract:
public abstract class Stack { . . . }
But like an interface, it can have unimplemented public instance methods.
These methods are also marked with the keyword abstract:
public abstract void push(Object o);
Instantiating
You cant instantiate an abstract class.
If a descendant does not fill in all the missing method bodies, it must be
declared abstract too.
If a descendant fills in all method bodies, it does not need to be abstract.
27
A Tradeoff
We have given a simple rule: Always use an interface to represent an ADT.
But another option is to use an abstract class. Its not obvious which choice
is better.
Reason to prefer an interface:
A class implementing the interface can still extend something else.
Reason to prefer an abstract class:
Can provide code for an operation defined in terms of other ones. A
subclass can still override this code if it wants.
Revised rule: Use an interface unless you want to provide default method
implementation(s).
Question: Why might a subclass override head()?
28