Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Explaining interfaces to beginning programmers?

I searched on stackoverflow and here for a good example to teach java interfaces in a beginners class.

I found the following post: http://stackoverflow.com/questions/416331/java-interfaces

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.

Any pointers or experience in teaching java interfaces?

share|improve this question

marked as duplicate by jmquigley, Bernard, Mark Trapp, Michael K, Robert Harvey Mar 28 '12 at 19:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
thanks for the link, unfortunately I missed this questions with my google "site:stackoverflow.com". –  mrsteve Mar 28 '12 at 17:43
add comment

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
    
People taught me interface that way, and I didn't get why it was useful at the time. I'm not sure it is a great idea. –  deadalnix Mar 28 '12 at 19:09
    
@deadalnix This analogy is the one that made it 'click' in my head. –  CFL_Jeff Mar 28 '12 at 19:25
add comment

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.

share|improve this answer
add comment

It is useful at first to think of classes as an is-a relationship and interfaces as features not necessarily inherited. Example class Sheep extends Animal implements HasLegs

share|improve this answer
add comment

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.