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

                If you find yourself writing a dummy method that will be overridden by methods defined in all instan- tiable descendant classes, stop and reconsider. There’s a better way. Use an abstract class to
An abstract declaration doesn’t contain enough information to define the method. It just specifies its outside-world interface and says that definition(s) will exist somewhere else. Where? In all instantiable descendant classes! Using an abstract method avoids the inelegant dummy method definition, and it’s a better way to implement polymorphism.
The abstract modifier is well named. Something is abstract if it is general in nature, not detailed in nature. An abstract method declaration is general in nature. It doesn’t provide method details. It just serves notice that the method exists and that it must be fleshed out by “real” method definitions in all instantiable descendant classes. Have we followed this rule for our program? In other words, do we have
13.8 abstract Methods and Classes 531
             tell the compiler what you’re trying to do ahead of time. In the abstract class, declare those
methods that are inappropriate for the reference variable’s class but will be defined by de-
scendant classes that instantiate objects. To declare a method, just write the method head-
ing with the additional modifier abstract, and terminate this modified method heading with a semicolon. For example, note the abstract modifier in the Employee2 class heading in Figure 13.13.
An abstract class outlines future work.
   /******************************************
*
Employee2.java
Dean & Dean
This abstract class describes employees.
*
*
*
Apago PDF Enhancer
  ******************************************/
    public abstract class Employee2
    {
 }
// end class Employee2
public abstract double getPay();
private String name;
//***************************************
public Employee2(String name)
 {
}
this.name = name;
//***************************************
public void printPay(int date)
{
System.out.printf("%2d %10s: %8.2f\n",
date, name, getPay());
} // end printPay
If there’s an abstract method, the class is abstract, too.
 abstract method declaration replaces dummy method definition.
Figure 13.13
Employee2 class, using the abstract modifier to replace a dummy method definition with simpler method declaration



























































   563   564   565   566   567