G.10.2. Concrete Subclass SalariedEmployee

Class SalariedEmployee (Fig. G.17) extends class Employee (line 4) and overrides abstract method earnings (lines 33–37), which makes SalariedEmployee a concrete class. The class includes a constructor (lines 9–14) that takes a first name, a last name, a social security number and a weekly salary as arguments; a set method to assign a new nonnegative value to instance variable weeklySalary (lines 17–24); a get method to return weeklySalary’s value (lines 27–30); a method earnings (lines 33–37) to calculate a SalariedEmployee’s earnings; and a method toString (lines 40–45), which returns a String including the employee’s type, namely, "salaried employee: " followed by employee-specific information produced by superclass Employee’s toString method and Salaried-Employee’s getWeeklySalary method. Class SalariedEmployee’s constructor passes the first name, last name and social security number to the Employee constructor (line 12) to initialize the private instance variables not inherited from the superclass. Method earnings overrides Employee’s abstract method earnings to provide a concrete implementation that returns the SalariedEmployee’s weekly salary. If we do not implement earnings, class SalariedEmployee must be declared abstract—otherwise, class SalariedEmployee will not compile. Of course, we want SalariedEmployee to be a concrete class in this example.

Method toString (lines 40–45) overrides Employee method toString. If class SalariedEmployee did not override toString, SalariedEmployee would have inherited the Employee version of toString. In that case, SalariedEmployee’s toString method would simply return the employee’s full name and social security number, which does not adequately represent a SalariedEmployee. To produce a complete String representation of a SalariedEmployee, the subclass’s toString method returns "salaried employee: " followed by the superclass Employee-specific information (i.e., first name, last name and social security number) obtained by invoking the superclass’s toString method (line 44)—this is a nice example of code reuse. The String representation of a SalariedEmployee also contains the employee’s weekly salary obtained by invoking the class’s getWeeklySalary method.


 1      // Fig. G.17: SalariedEmployee.java
 2      // SalariedEmployee concrete class extends abstract class Employee.
 3
 4      public class SalariedEmployee extends Employee
 5      {
 6         private double weeklySalary;
 7
 8         // four-argument constructor
 9         public SalariedEmployee( String first, String last, String ssn,
10            double salary )
11         {
12            super( first, last, ssn ); // pass to Employee constructor
13            setWeeklySalary( salary ); // validate and store salary
14         } // end four-argument SalariedEmployee constructor
15
16         // set salary
17         public void setWeeklySalary( double salary )
18         {
19            if ( salary >= 0.0 )
20               baseSalary = salary;
21            else
22               throw new IllegalArgumentException(
23                  "Weekly salary must be >= 0.0" );
24         } // end method setWeeklySalary
25
26         // return salary
27         public double getWeeklySalary()
28         {
29            return weeklySalary;
30         } // end method getWeeklySalary
31
32         // calculate earnings; override abstract method earnings in Employee
33         @Override                                                           
34         public double earnings()                                            
35         {                                                                   
36            return getWeeklySalary();                                        
37         } // end method earnings                                            
38
39         // return String representation of SalariedEmployee object   
40         @Override                                                    
41         public String toString()                                     
42         {                                                            
43            return String.format( "salaried employee: %s %s: $%,.2f",
44               super.toString(), "weekly salary", getWeeklySalary() );
45         } // end method toString
46      } // end class SalariedEmployee


Fig. G.17 | SalariedEmployee concrete class extends abstract class Employee.

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

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