When I am teaching Java to my students or mentoring a junior person on the job they often ask for some tips to become productive in Java.  I have always believed that a solid foundation in generic object oriented concepts and techniques and an understanding of how these relate to programming in Java.

Going Public

In object oriented systems objects define their behavior through their exposed behavior.  In Java this is through their public methods.   Often people will call the methods that an object exposes its interface.   The most common example I have seen is your television set and the remote control (who uses the buttons on the front of their TV still?).   The remote control defines how you interact with your television.

As a side note, interfaces in Java are often portrayed as a way to circumvent the lack of multiple inheritance in Java that is present in other languages like C++.  While you can implement a form of multiple inheritance and it does solve what C++ critics like to call the diamond problem, this is not where I see interfaces being useful.  I spent a lot of years writing C++ code and do not remember consciously using multiple inheritance anyway.

Contract & Polymorphism

For me interfaces provide an elegant way to implement polymorphism as well as making sure my public interface (this is the interface that means the exposed methods) conform to what is expected.

First to polymorphism.  If you need a refresher here try this.   If you need more help there are thousands of definitions, tutorials and articles on the web.   Interfaces allow me to work with the interface type in Java and not worry about which object I am currently referencing with my interface.   A good example of this is found in this article.  I think in this context interfaces are a nice, simple and lightweight solution verse using base classes etc.   Think of the standard example with an interface  type Shape and implementation classes such as Square, Circle and Triangle.  The interface declares a method Draw and each implementing class must know how to draw itself.   I like this, it makes sense and it is elegant. 

OK, what about forcing a public interface.   This is called Design By Contract.  You can do this by documenting that anybody who wants to implement a class within this hierarchy HAS to implement a method or a set of methods with the signature as specified.  But, who is going to make them.   For a good example of this, think to when you do Swing programming.   You want the system to call an event handler after you have registered a component with the event handling subsystem using the addActionListener(this); method call.    The event handling subsystem will then call a method called actionPerformed(ActionEvent event) when an event occurs.   But how can you be sure this method is implemented, with the write name, in correct case and the right parameters.   By implementing the interface ActionListener.   By declaring your class with the words implements ActionListener all of this will be guaranteed and enforced by the compiler.  So this is a formal way to make sure you deliver what you promise.

So interfaces aren’t about multiple inheritance and were not included to make up for the lack of this being present in Java.  They are for: 1) Elegant way to implement polymorphism;  2) Insure your classes deliver the public interface you promise.

powered by performancing firefox