Chapter:Interfaces - Program against an interface

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Background about this chapter topics...

Purpose

Make the student familiar with stuff brought up in this chapter...

Goal

After this chapter, the student shall understand:

  • this
  • that
  • those
  • them
  • and the reset

Instructions to the teacher

Common problems

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

Information
On the right-hand side, you don't need to repeat the generic type: new ArrayList<>(); That is called the "diamond operator", since it looks like a diamond.
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.

Chapters on the topic of Interfaces

You can check your progress on this chapter using this list:

Lecture slides and Videos

Links

Further reading

Where to go next

The next page has exercises (if any) for this chapter: Interfaces_-_Program_against_an_interface_-_Exercises

« PreviousBook TOCNext »