Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Designing with interfaces

One programmer's struggle to understand the interface

  • Print
  • Feedback
One of the fundamental activities of any software system design is defining the interfaces between the components of the system. Because Java's interface construct allows you to define an abstract interface without specifying any implementation, a major activity of any Java program design is "figuring out what the interfaces are." This article looks at the motivation behind the Java interface and gives guidelines on how to make the most of this important part of Java.

Deciphering the interface

Almost two years ago, I wrote a chapter on the Java interface and asked a few friends who know C++ to review it. In this chapter, which is now part of my Java course reader Inner Java (see Resources), I presented interfaces primarily as a special kind of multiple inheritance: multiple inheritance of interface (the object-oriented concept) without multiple inheritance of implementation. One reviewer told me that, although she understood the mechanics of the Java interface after reading my chapter, she didn't really "get the point" of them. Exactly how, she asked me, were Java's interfaces an improvement over C++'s multiple inheritance mechanism? At the time I wasn't able to answer her question to her satisfaction, primarily because in those days I hadn't quite gotten the point of interfaces myself.

Although I had to work with Java for quite a while before I felt I was able to explain the significance of the interface, I noticed one difference right away between Java's interface and C++'s multiple inheritance. Prior to the advent of Java, I spent five years programming in C++, and in all that time I had never once used multiple inheritance. Multiple inheritance wasn't against my religion exactly, I just never encountered a C++ design situation where I felt it made sense. When I started working with Java, what first jumped out at me about interfaces was how often they were useful to me. In contrast to multiple inheritance in C++, which in five years I never used, I was using Java's interfaces all the time.

So given how often I found interfaces useful when I began working with Java, I knew something was going on. But what, exactly? Could Java's interface be solving an inherent problem in traditional multiple inheritance? Was multiple inheritance of interface somehow intrinsically better than plain, old multiple inheritance?

Interfaces and the 'diamond problem'

One justification of interfaces that I had heard early on was that they solved the "diamond problem" of traditional multiple inheritance. The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common superclass. For example, in Michael Crichton's novel Jurassic Park, scientists combine dinosaur DNA with DNA from modern frogs to get an animal that resembled a dinosaur but in some ways acted like a frog. At the end of the novel, the heros of the story stumble on dinosaur eggs. The dinosaurs, which were all created female to prevent fraternization in the wild, were reproducing. Chrichton attributed this miracle of love to the snippets of frog DNA the scientists had used to fill in missing pieces of the dinosaur DNA. In frog populations dominated by one sex, Chrichton says, some frogs of the dominant sex may spontaneously change their sex. (Although this seems like a good thing for the survival of the frog species, it must be terribly confusing for the individual frogs involved.) The dinosaurs in Jurassic Park had inadvertently inherited this spontaneous sex-change behavior from their frog ancestry, with tragic consequences.

  • Print
  • Feedback

Resources
  • Bill Venners' next book is Flexible Java http://www.artima.com/flexiblejava/index.html
  • Bill Venners just got back from his European bike trip. Read about it at
    http://www.artima.com/bv/travel/bike98/index.html
  • The discussion forum devoted to the material presented in this article http://www.artima.com/flexiblejava/fjf/ interfaces /index.html
  • Links to all previous design techniques articles http://www.artima.com/designtechniques/index.html
  • Recommended books on Java design http://www.artima.com/designtechniques/booklist.html
  • A transcript of an e-mail debate between Bill Venners, Mark Johnson (JavaWorld's JavaBeans columnist), and Mark Balbe on whether or not all objects should be made into beans http://www.artima.com/flexiblejava/comments/beandebate.html
  • Object orientation FAQ http://www.cyberdyne-object-sys.com/oofaq/
  • 7237 Links on Object Orientation http://www.rhein-neckar.de/~cetus/software.html
  • The Object-Oriented Page http://www.well.com/user/ritchie/oo.html
  • Collection of information on OO approach http://arkhp1.kek.jp:80/managers/computing/activities/OO_CollectInfor/OO_CollectInfo.html
  • Design Patterns Home Page http://hillside.net/patterns/patterns.html
  • A Comparison of OOA and OOD Methods http://www.iconcomp.com/papers/comp/comp_1.html
  • Object-Oriented Analysis and Design MethodsA Comparative Review http://wwwis.cs.utwente.nl:8080/dmrg/OODOC/oodoc/oo.html
  • Patterns discussion FAQ http://gee.cs.oswego.edu/dl/pd-FAQ/pd-FAQ.html
  • Patterns in Java AWT http://mordor.cs.hut.fi/tik-76.278/group6/awtpat.html
  • Software Technology's Design Patterns Page http://www.sw-technologies.com/dpattern/
  • Previous Design Techniques articles http://www.javaworld.com/topicalindex/jw-ti-techniques.html