from  Gerry Donaldson   Nov 22 (5 days ago)
 reply-to  AP Computer Science 
 to  AP Computer Science 
 date  Nov 22, 2006 11:09 AM
 subject  Re: [ap-compsci] Interfaces
 mailed-by  lyris.collegeboard.com

On 11/22/06, Peter O'Connor wrote:

>> I always like to try to have a full understanding of a topic before I teach it.

>> Can someone please explain the use of an interface? I can see that you could define a set of
>> constants with the interface, but in my Lewis Loftus Cocking book, they do not seem to give a
>> reason to use an interface.

A little googling will get you formal explanations, but let me give my off the cuff explanation that I might give at student who asked that question. The most formal explanation is from Sun's API at http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html

Let's ensure that we first understand what an interface is and does. Often an appreciation of reasons is intimately tied to knowledge of what something is and how it works.

First, a picky detail, you must know that a method signature is the name of a method and its parameter list. It does not include the return data type usually seen in the heading of a method.

An interface is like a class but the bodies of the methods have been left blank. An interface usually contains at least one method signature. It only contains the method signature and not the body of the method because it wants you or whoever is designing the program to decide precisely how that method should be implemented. When you implement a method, you must supply the source code of the body of the method(s) for which you find method signatures in the interface.

The reason that interfaces are handy is because it is one way to achieve consistancy at an abstract level but allow for varying implentations at the code level. A classic example is the Comparable interface.

There is only one method signature in the Comparable interface. That is the compareTo(Object obj) method. This method will is usually written to return -1 if the host object is less than the parameter object, 0 is the host object is the same as the parameter object, or +1 if the host object is greater than the parameter object. The use of the Comparable interface requires that the program designer determine just how each of the objects are to be compared, but using Comparable will ensure a consistent approach to comparisons of whatever it is that is being compared.

Now this begs the question: How do you compare the two objects.

That depends on what you are comparing. You can compare something as simple as strings or as complicated as two medical records each with 100 fields. It is up to the program designer to decide on the criteria. For purposes of my particular program, where I compare two medical records, I may decide to compare only the blood pressures field of the two objects. That is my design decision. You may instead decide to use the result of a stress test. That is fine. You write your code and I'll write mine, but both of our programs will behave in a consistant fashion.

Now we have a way to always compare two objects at the abstract level.

Another really well known interface is awt's ActionListener. Whenever you add a listener to determine when a GUI event occurs, like the pressing of the Enter key, you must impliment the actionPerformed(ActionEvent event) method. We can't implement actionPerformed ahead of time because we don't know the design of your program. When the "Enter" key is pressed, some programs will change the colour of the background of the screen while others will add the sum of three numbers and print them to the screen. But whenever there is an even, we know that we must perform some action, whatever it is. Java GUI programming is very consistent because of the ActionListener interface. It will force the designer to do something when the event occurs.

In my opinion (remember this is off the cuff) the reason for interfaces is to enforce consistancy in the design of programs. It is one of the advantages of Java over some earlier procedural languages. Whenever I look at a GUI program, I know to immediately look at the actionPerformed() method to see what happens when the event happens. Whenever the Comparable interface is used, I know that the result of a comparison is expressed as the return of -1, 0 or +1.

Gerry Donaldson, Computer Science Teacher
Sir Winston Churchill High School
Calgary, Alberta, Canada.

On the Internet Since March 9, 1996    URL:   http://www.comscigate.com    Last Revised:   November 27, 2006.