Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
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?
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.