![]() ![]() When referring to this variable in a Frogosaur object, which copy of the variable - Frog's or Dinosaur's - would be selected? Or, perhaps, would there be only one copy of the variable in a Frogosaur object? The diamond problem would also arise if Animal had declared a public instance variable, which Frogosaur would then have inherited from both Dinosaur and Frog. Will a Frogosaur croak "Ribbit, Ribbit." or sing "Oh, I'm a dinosaur and I'm okay."? The diamond problem rears its ugly head when someone tries to invoke talk() on a Frogosaur object from an Animal reference, as in: Animal animal = new Frogosaur() īecause of the ambiguity caused by the diamond problem, it isn't clear whether the runtime system should invoke Frog's or Dinosaur's implementation of talk(). (This won't compile, of course, because Java Here's what the code might look like if Java supported traditional multiple inheritance: One way the diamond problem can arise in the Jurassic Park hierarchy is if both Dinosaur and Frog, but not Frogosaur, override a method declared in Animal. ![]() In fact, the diamond problem gets its name from the diamond shape of such an inheritance hierarchy. The diamond problem can arise in inheritance hierarchies like the one shown in Figure 1. ![]() This Jurassic Park scenario potentially could be represented by the following inheritance hierarchy: Figure 1. (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. In frog populations dominated by one sex, Chrichton says, some frogs of the dominant sex may spontaneously change their sex. 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. The dinosaurs, which were all created female to prevent fraternization in the wild, were reproducing. At the end of the novel, the heros of the story stumble on dinosaur eggs. 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. The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common superclass. One justification of interfaces that I had heard early on was that they solved the "diamond problem" of traditional multiple inheritance. 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' So given how often I found interfaces useful when I began working with Java, I knew something was going on. In contrast to multiple inheritance in C++, which in five years I never used, I was using Java's interfaces all the time. When I started working with Java, what first jumped out at me about interfaces was how often they were useful to me. Multiple inheritance wasn't against my religion exactly, I just never encountered a C++ design situation where I felt it made sense. 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. ![]() 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.Īlthough 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. 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. 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. Deciphering the interfaceĪlmost two years ago, I wrote a chapter on the Java interface and asked a few friends who know C++ to review it. 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. One of the fundamental activities of any software system design is defining the interfaces between the components of the system. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |