Page 569 - Introduction to Programming with Java: A Problem Solving Approach
P. 569

                method is not static. Finally, note that the keyword final does not appear in our syntax template. It’s understood that all constants are final, and since no methods are defined, it’s understood that any method declaration is not final.
Using an Interface to Store Universal Constants
In addition to telling the world that your class defines a certain minimum set of methods, implementing an interface also gives your class free access to all the named constants which that interface defines. Putting common named constants into an interface and then giving multiple classes access to those named constants by having them implement that interface is a handy way to provide easy access to a large set of common physical constants and/or empirical factors or constants. You avoid duplicate definitions of those constants, and you don’t have to use a class-name dot prefix to access those constants. In principle, you could use an inheritance hierarchy to provide direct access to common constants, but that would be bad practice, because it would waste your one inheritance opportunity on nothing more than a bunch of constants. If you use an interface to do this, you’re still free to use inheritance and/or additional interfaces for other purposes.
Using Interfaces to Implement Additional Polymorphisms
Now suppose you have already created an inheritance hierarchy, and you are already using it to implement some particular polymorphism, as we did in our Payroll program. Then suppose you want to add another polymorphism that doesn’t fit the structure of the original inheritance hierarchy. For example, you might want a method to be polymorphic among only some of the classes in the original hierarchy, and/or you might want a polymorphism to include classes that are outside that hierarchy. A Java class cannot participate in more than one inheritanAcep—ait cganoextenPd oDnlFy oneEotnherhclassn. Tchues,ryou cannot use ab-
stract classes to support polymorphisms that span more than one inheritance hierarchy. But as the previous accounting system example suggests, you can span more than one inher- itance hierarchy with a Java interface. And one of the principle reasons to use Java interfaces is to implement multiple polymorphisms.
To illustrate this, we’ll enhance the previous Payroll program by adding two classes
13.9 Interfaces 535
      Make it polymorphic without distorting inheritance.
        4
of commissioned employees. One of those classes gets a “straight” commission. The other class gets a
salary plus a commission. In either case, the commission is based on a common fixed percentage of sales. Figure 13.14 contains the code for an interface that defines this fixed percentage as a named constant and declares a method that must be defined in all classes that implement the interface.
Figure 13.15 shows the code for a Commissioned class, which describes a class of employees who work on a straight commission. The Commissioned class extends Figure 13.13’s Employee2 class. Employee2 is an abstract class, and as such, the Commissioned subclass must define all of Employee2’s abstract methods. The only abstract method in the Employee2 class is the getPay class, so the Comissioned class must define the getPay method, and yes, it does. This increases the total number of polymorphic getPay methods to three. In the commissioned class’s heading, note the clause, implements interface Commission.ThisprovidesdirectaccesstotheCOMMISSION_RATEnamed constant, which the Commissioned class’s getPay method uses to do its job. When it implements the Commission interface, the Commissioned class also takes on an obligation. It must define all the meth- ods declared in that interface. The only method declared in the Commission interface is the addSales method, and yes, the Commissioned class defines this method, too.
 4 See Appendix 7 for a complete UML diagram of the enhanced Payroll program developed in this subsection.





















































































   567   568   569   570   571