Chapter:Interfaces - Program against an interface
Meta information about this chapter
Expand using link to the right to see the full content.
Background about this chapter topics...
Make the student familiar with stuff brought up in this chapter...
After this chapter, the student shall understand:
- and the reset
Instructions to the teacher
TODO! Students often ask about X and have problems with Y...
Interfaces: Program against an interface
A principle often recommended is to "program against an interface". In order to give an example of what that means, we'll start by telling you that
java.util.List is an interface. This interface defines what we can do with different kinds of lists, such as
ArrayList is to be considered "a List"
ArrayList implements the
This means, that we can declare a list of
String references as being of type
List<String>, rather than being specific about what exact type of list we are using:
List<String> myStrings = new ArrayList<>();
From this point, we can only treat the list referenced by
myStrings as a general
List<String> as defined in the interface
List. We limit ourselves to the methods defined in the interface List. Isn't this a drawback? No, it's a feature! Since we limit ourselves to only use the methods declared in the
List interface, it is perfectly safe for us to change our mind at a later stage, and initialize
myStrings to some other type of concrete list (such as
LinkedList). Since both
LinkedList implement the
List interface, they share at least the methods declared in the
List interface! So everything we can do with the
myStrings reference, we can do regardless of the actual list is an
ArrayList or a
In a real program, the intialization would typically come from some method, rather than an explicit call to the constructor of e.g.
ArrayList. Let's pretend that we get our list object from a method called
createList(). As long as the method
createList() returns an object of type
List<String>, we don't have to worry about what particular type of list we get. We'll just treat the list as a list as defined in the
List<String> myStrings = createList(); // We can now only use the instance methods declared in the List interface!
createList() method will create and return a reference to some real type of list, perhaps an
ArrayList<String>. But the method will return this object as being of type
List<String>, which will hide from us what the actual type is. This is actually a good thing, because we don't have to worry about the details! We'll just treat the object returned to us a some kind of
List<String>. Why is it a good thing? Because the authors of the
createList() method might change their mind for some reason, and return instead an object of class
LinkedList<String> but still return it as of type
List<String>. But for us, this doesn't matter, which is the good thing! We know that we get an object of the more general type
List<String>, so our code works with any such object. So the authors of the
createList() method can change the actual object as much they like (as long as they stick to the contract of returning something of the general type
Some of the methods of the java.util.List interface include:
boolean add(E e)- adds an element of the list type to the list
E get(int index)- gets the element at index (as the type of the list elements)
boolean isEmpty()- returns true if the list is empty, otherwise false
int size()- returns the number of elements in the list
These (and the rest of the methods declared in the
List interface) are methods we can call on every object whose class implements the
List interface. As long as we stick to these methods, we're safe if we or someone else change their mind and chooses an alternative actual class for our list.
Chapters on the topic of Interfaces
You can check your progress on this chapter using this list:
- Chapter:Interfaces_-_Program_against_an_interface « you are here!
Lecture slides and Videos
- Java - Interfaces - Programming (Full playlist) | Java - Interfaces - Programming 1/2 | 2/2 | Programming against an interface.pdf
- Design Principles from Design Patterns - A Conversation with Erich Gamma, Part III
- Thomas Uhrig - Programming to an interface (blog post)
- Oracle tutorial - What is an interface?
- Oracle tutorial - Interfaces in Java
- Java online API docs - Comparable interface
- Java online API docs - Comparator interface
- Oracle tutorial on arrays (bonus reading!)
Where to go next
The next page has exercises (if any) for this chapter: Interfaces_-_Program_against_an_interface_-_Exercises