Interfaces

Chapter 2 briefly defined two types of inheritance: implementation and interface. Interface inheritance and how it is treated in Java is quite unique among object-oriented languages. This isn't to say that other languages don't support interfaces. C++ doesn't formally distinguish the construct, but an interface can be implemented by extension of a class in which nothing but pure virtual functions are defined. In Visual Basic, interface inheritance is accomplished through the implements keyword. Java has a little in common with Visual Basic in that regard because it, too, uses the keyword implements. However, this is where the similarity ends.

The designers of the Java language wisely granted interfaces their own unique definition. The declaration looks much like that of a class, except that the keyword class is replaced with interface. The important point about the interface in this chapter is that UML offers its own unique diagrammatic elements. Chapter 2 defined an interface, IPrice, that provided a common pricing contract for the Order class, anticipating that other classes (a future Service class, for example) might abide by this contract as well. UML offers two options to the modeler.

Figure 5-14 diagrams the Order class, which realizes—that is, provides the concrete implementation for—the interface IPrice. The realizes relationship is a combination of the inheritance notation (triangle with solid line) and the dependency relationship (dashed line with an arrowhead). Notice that the Customer class depends on the interface IPrice. The attractive features of this view of interface inheritance are that the operations of the interface are clear, and thus so is the responsibility of the Order class to abide by the interface.

Figure 5-14. UML notation for interface inheritance


Figure 5-15, on the other hand, denotes the alternative modeling convention for interface inheritance. This view may be more familiar to some of you, particularly if you have been exposed to the Microsoft technology architectures (of course there is more than one). Some people refer to this as the lollipop view of interface inheritance.

Figure 5-15. UML and interface inheritance: Alternate form


Although this view is cleaner and abbreviated, it lacks the operations that make up the interface. Because a class in Java may implement many different interfaces, it is possible, then, to have a class that has several lollipop icons extending from it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.56.29