Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Chapter:Interfaces - Program against an interface

From Juneday education
Revision as of 10:17, 20 June 2017 by Henrik Sandklef (Talk | contribs) (Created page with "==Program against an interface== ===Description=== A principle often recommended is to "program against an interface". In order to give an example of what that means, we'll st...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Program against an interface

Description

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. Since ArrayList is to be considered "a List" ArrayList implements the List interface.

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 ArrayList and 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 LinkedList.

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 interface:

List<String> myStrings = createList();
// We can now only use the instance methods declared in the List interface!

Now, the 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 List<String>.

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.

Videos

This section of the chapter is just for reading. We include it in order to be more complete.


Links

External links

Books this chapter is a part of

Programming with Java book

Book TOC | previous chapter | next chapter