Interfaces
An interface declares a set of behaviors. A class can chose to advertise that it implements the interface. Any class that makes this claim, must implement all methods declared in the interface.1
- Storing collections (groups) of similar objects is a common programming task.
- A text document is a collection of paragraphs. Each paragraph is a collection of words. Each word is a collection of characters.
- The computer's file system is a collection of files.
- A browser's bookmark list contains a collection of URLs.
- etc...
- You have already seen one mechanism for storing collections of objects: the array.
- The Java Collections Framework provides a number of classes that can be used to store collections of data.
- In this page we will:
- Briefly review arrays, discussing some disadvantages
- Introduce the concept of an interface in Java
- Introduce one interface and two classes that are part of the Java Collections Framework:
- The
List<E>
interface. - The
ArrayList<E>
class. - The
LinkedList<E>
class.
- The
Interfaces
- The Java language includes a concept known as interfaces.
- It's a fairly simple concept that we'll need in order to better understand collection types that are part of the Java Collections Framework.
- An interface is basically a promise or contract.
- An interface is a list of method signatures (method name, parameters passed to the method, and the type returned by the method, if anything).
- When a class is declared, it may promise to implement the interface.
- The compiler then makes sure that the class fulfills its promise by refusing to compile unless all of the methods declared in the interface are implemented in the class.
- In much the same way as we can create a reference to an object, we can create a reference to an interface.
- The reference acts as the gatekeeper for accessing the object to which the reference points. Consider the following code:
String word = new String("nonsense");
CharSequence charSeq = word;
- The first line creates a reference to a String and makes it point to a newly created String object.
- The second line creates a reference to a CharSequence and makes it point to the same String object as word.
- Since the
String
class implements theCharSequence
, the second line is valid code. - Both word and charSeq refer to the same object, but we can only access methods known to the
CharSequence
interface when using the charSeq reference. - The
CharSequence
consists of four methods: - The following indicates which lines are illegal. All of the legal lines of code function identically regardless of whether the method is called through word or charSeq.
word.toString(); word.endsWith("sense"); word.length(); word.subSequence(1, 3); word.substring(1, 3); charSeq.toString(); charSeq.endsWith("sense"); // ILLEGAL charSeq.length(); charSeq.subSequence(1, 3); charSeq.substring(1, 3); // ILLEGAL
Java Collections Framework
- Because manipulating collections of objects is such a common task, Sun provides the Java Collections Framework.
- The framework contains a number of interfaces and classes that provide various ways of storing and interacting with collections of data.
- In data structures you will explore the Java Collections Framework in much more detail.
- In this course we will just touch on a few items.
1
Actually, the class could be an abstract class and declare a method abstract, and then not bother to implement it. Alternatively, the class could provide a really bad implementation, but we'll leave these comments as a footnote.