Chapter:Interfaces - Writing 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.


This chapter goes through when and how to write an interface and also how to use it.


This chapter should prepare the student on how to think when wanting to specify a specific behavior for objects of different classes and write this down in an interface. Using this knowledge, the student should know how to write and use his/her own interface.


After this chapter, the student shall:

  • understand when you need to write your own interface
  • be able to write her/his own interface
  • be able to write code that manages objects implementing the interface
  • be able to use (implement) her/his own interface in different classes

Instructions to the teacher

Common problems


Full frontal - Code example up front!

Here's a small code example, just to introduce you to some code related to this topic. You don't have to understand it, it is here just to expose you to some code. You may ignore it, or just enjoy it.

// Classes that implement this interface, must implement the play() method "for real"
// After that, objects of such classes, may be cast to the Playable type!
public interface Playable{
  public void play();

Interfaces: Writing your own interfaces

Sometimes we need to write our own interfaces in order to solve some problem. In this lecture we show you how you can write a simple interface in order to make an abstraction which creates less dependencies between different parts of the code in a media player project.

The idea in this example is that a media player should be able to focus on objects which can be played. How the actual playing of a media file is implemented, shouldn't be the problem of the media player part of the system.

An interface is used to create this abstraction. The interface we create is called Playable and defines only one abstract method play(). All classes which implement this interface must then implement the play() method. In return, objects from classes implementing the Playable interface can now be viewed as playable objects, since they have an instance method which knows how to play them.

public interface Playable{
  public void play();

The media player can now accept a reference to some Playable object and call the play() method on the object. This means that the media player doesn't have to care about the objects' real classes, but rather focus on the fact that they have a play() method!

static void playFile(Playable p){;

In order to create some drama (just kidding), we have decided to design our classes for two types of media files, AudioFile and VideoFile using a common base class File. The File class in this example is pretty simple and just keeps the name of the file. Now, both AudioFile and VideoFile extend File. This means that they are first of all representations of special types of files. But we want them to also be playable and here comes the interface to the rescue.

The classes AudioFile and VideoFile both extend File but also implement Playable. Implementing Playable means that they must provide implementation for the play() method. This means that our design has put the responsibility of playing in the classes, so that the objects know how to play themselves, so to speak.

public class AudioFile extends File implements Playable{

  public static final int MUSIC = 0;
  public static final int SPEECH= 1;

  public int type;

  public AudioFile(String name, int type){

  public int type(){
    return type;

  public void play(){
    System.out.println("Playing the file " + name() +
                       " which is a " + 
                       " file");

The test class we have written creates an array which can hold references to Playable objects (objects of some class which implement Playable). We populate the array with references to a mix of objects, some audio files and some video files. Both AudioFile and VideoFile implement Playable, so this is legal and fine.

Next, the test class loops through the array and calls the media player (a simple method which calls play() on the argument which is of type Playable) which each reference in the array as the argument.

The playFile(Playable) method (which simulates our media player) accepts a reference to a Playable as the single argument, and calls play() on the reference. Our hope is that this shows that the media player is very general and can be called with any type of playable file or object. It doesn't care about the actual class of the object to be played, it simply focuses on the fact that a Playable object knows how to play itself via its play() method.

static void playFile(Playable p){;

In fact, the media player is future proof in the sense that if we create a new class for a new type of playable object, it will still work as long as that object implements the play() method and is of a class implementing the Playable interface.

What if we in the future decide, for instance, that it would be cool to play a stream from a network service? Then we could actually create a class MediaStream which implements Playable. The play() method in that class would create a connection to some service on the internet and stream and play from that connection. Could we send a MediaStream object to the media player? Yes, of course! Since it knows how to play itself (and since the media player doesn't really care how this is done) it will happily accept the MediaStream object and view it as a Playable in general, and call the play() method on it.

The good thing about that, is that we now have a system which can handle totally different types of objects (files and network streams) as long as they share some capabilities. The capability is to have a functional play() method and to be able to be viewed as being of type Playable. The interface Playable fixes that for us, so all we have to do is to write classes which implement that interface and offers a good version of the play() method.

Chapters on the topic of Interfaces

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


Note: This chapter has only one video in English, as well as only one video in Swedish. More may be added later. Until then, be prepared that the playlist only has the one and only video.

English videos

Swedish videos

Source code for this chapter


Bonus PDF - An example with FBFile using composition, strategy, decorator, interfaces and more

Further reading

Where to go next

Next page has exercises for this chapter: Interfaces_-_Writing_an_interface_-_Exercises

« PreviousBook TOCNext »