The Official Java-Tutorial and the references feature examples that are quite
good but I would need a very simple example to start with for my class. I don't think the offical tutorial is the best resource for absolute beginners. The students are easily confused, and the most effective example would help.

4 Answers
4

I like to use the "promise" or "contract" analogy. Tell them that an interface is like a contract in that any concrete class that implements it MUST implement each of the methods as they are listed. In other words, by implementing an interface, a class is promising to implement all of its listed method signatures.

In the art of unit testing book, they reference interfaces as a method of abstraction really. The example involved being able to log messages without worrying if it was an EmailLogger implementing ILogger or FileBasedLogger or TextMessagingLogger. Write some code that logs using an ILogger. Maybe make them feel the pain by writing code that does not use an interface.

Basically what is confusing about interfaces to beginners is understanding why they are needed over plain old classes.

Interfaces are the basis of proper composition and shine in relationship with many design patterns (e.g. the Command pattern). As such, they are fundamental to sound OO design. Teach interfaces as the rule and class inheritance as the exception. Class inheritance (extends) is mostly unnecessary and widely misunderstood and leads to rigidity while promising flexibility. Interface implementation, on the other hand, leads to a more functional design and stimulates creativity.

A proper way to teach interfaces is to create a bit of undo functionality, with an interface 'Command', having two methods 'do' and 'undo' and a command stack.

Interfaces should be:

Coherent (the methods should have an obvious relationship, such as do() and undo(), fork() and join()),

Obvious to implement (the pre- and post-conditions of each method should be easy to understand, even though the implementation of the interface might be complex). This guarantees encapsulation and reuse,

Well named (generally the name should be an adjective, although exceptions can be made, such as the Command above).

Finally, interfaces generally define the façade of a more complex system, in order to insulate implementation changes in a future release from the users of this system.