Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

A perspective on interfaces

How to solve problems with Java's interfaces

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

For example, recently I had a Service interface for which start() and stop() were the logical names to use. I changed the names to startService() and stopService() to allow services that inherit from the class Thread to implement this interface. The Thread class has two methods named: start() and stop(). These methods control the state of the thread. Any interface that will use the method names start and stop can never be implemented by a subclass of the Thread class. Java has no scoping operator like C++ (the ::) to tell the compiler exactly what should be done in the case of overlapping method names from different interfaces or the superclass.

Lack of default implementation

The final problem is the lack of default implementation. When a class extends another class, this other class can provide default implementations for all the methods. Sometimes a subclass can be created simply by extending a superclass and using the entire implementation of the superclass. Some interfaces are quite extensive, and implementing all methods can be a lot of work. This is especially irritating when only one or two methods are useful to implement in a certain situation. The interface concept forces the user of the interface to implement every method in the definition. For example, the Swing ButtonModel interface has 21 (!) methods defined. Quite often, an implementation wants to implement only a part of this interface, and it should be possible to do this.

Solving the problems

How can these problems be solved? Any operation in this area should be implemented with care. Fortunately, Sun is extremely careful in making any language changes to prevent the kind of disaster that hit the C++ standardization process. Doing the mental exercise to formulate a solution to the sketched problems, however, can't do any harm.

So, what's one solution to many of the problems discussed above? Just add to the language a new keyword: "like," which you then use in place of "extends" and "interface":

public class C like A {
}


You should interpret the code above as follows: C should implement the same interface as A. If C does not implement a required method, however, the implementation is copied from A. The keyword "like" basically means: Treat a class as an interface definition and copy the code of that class when no implementation is supplied (if not abstract).

Using this keyword will solve the problem of duplication of classes and interfaces, as well as the issues of default implementation and extension discussed above. It seems feasible to implement this change in the current language version without requiring changes in the current virtual machines.

Conclusion

These are just first thoughts on the matter of interfaces. In my opinion, continued effort should be made to provide solutions to these problems -- of course, always with the caveat that changing the Java language should be done with extreme caution.

Even with the problems listed above, believe me when I say I am still in love with interfaces!

About the author

Peter Kriens is an experienced software consultant mainly working in the telecommunications area. Currently, he works for Ericsson Telecom in Mlndal on network management and in Stockholm for Ericsson Software Research. He is one of the organizers of the OOPSLA DesignFest and a long time user of object-oriented technologies. He has been working with Java intensely for the last 18 months.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources