OOConcepts Interfaces
OOConcepts Interfaces
Atul Gupta
Interface
Defining an Interface
public interface GroupedInterface extends Interface1,
Interface2, Interface3
{
// constant declarations
An Example
public class Employee implements Comparable {
int EmpID;
String Ename;
double Sal;
static int i;
public Employee() {
EmpID = i++;
Ename = "dont know";
Sal = 0.0;
}
Rewriting Interfaces
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
}
Interface Methods
Collection<E> The root interface for collections. Provides such methods as add,
remove, size, and toArray.
Set<E> A collection in which no duplicate elements can be present, and whose
elements are not necessarily stored in any particular order (extends Collection<E>).
SortedSet<E> A set whose elements are sorted (extends Set<E>).
List<E> A collection whose elements stay in a particular order unless the list is
modified (extends Collection<E>). This is a general use of the word "list" and does not
necessarily mean "linked list," although that is one possible implementation.
Queue<E> A collection with an implied ordering in its elements (extends
Collection<E>). Every queue has a head element that is the target of specific operations
like deque and enqueue.
Iterator<E> An interface for objects that return elements from a collection one at a
time. This is the type of object returned by the method Iterable.iterator.
ListIterator<E> An iterator for List objects that adds useful List-related methods. This
is the type of object returned by List.listIterator.
Iterable<E> An object that provides an Iterator and so can be used in an enhanced for
statement.
Map Interfaces
Map<K,V> A mapping from keys to at most one value each. (Map does not
extend Collection, although the concepts meaningful to both maps and
collections are represented by methods of the same names, and maps can be
viewed as collections.)
SortedMap<K,V> A map whose keys are sorted (extends Map<K,V>).
HashSet<E> A Set implemented as a hashtable. A good, generalpurpose implementation for which searching, adding, and removing are
mostly insensitive to the size of the contents.
TReeSet<E> A SortedSet implemented as a balanced binary tree.
Slower to search or modify than a HashSet, but keeps the elements
sorted.
ArrayList<E> A List implemented using a resizable array. It is
expensive to add or delete an element near the beginning if the list is
large, but relatively cheap to create and fast for random access.
LinkedList<E> A doubly linked List and Queue implementation.
Modification is cheap at any size, but random access is slow.
HashMap<K,V> A hashtable implementation of Map. A very
generally useful collection with relatively cheap lookup and insertion
times.
TreeMap<K,V> An implementation of SortedMap as a balanced
binary tree to keep its elements ordered by key. Useful for ordered data
sets that require moderately quick lookup by key.
Interface Summary