The Artima Developer Community
Design Techniques | Book List | Print | Email | First Page | Previous | Next
Sponsored Link

Designing with Interfaces
One Programmer's Struggle to Understand the Interface
by Bill Venners
First Published in JavaWorld, November 1998

Page 1 of 6  >>


This article describes the process I went through to understand Java's interface. I talk about multiple inheritance and the diamond problem, polymorphism and dynamic binding, separation of interface and implementation as the spirit of Java, and my ultimate epiphany on how we should think about and use interfaces when we design.

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 Objects and 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?

Page 1 of 6  >>

Design Techniques | Book List | Print | Email | First Page | Previous | Next

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use