AP Computer Science --- Haas --- InheritanceWorker

The following is and an example of inheritance with an abstract class. Run in BlueJ, make sure you understand how it works.



/**
*   A worker gets paid at a current rate.
*   >>> Note: abstract methods can not be instantiated <<<
*/
public abstract class Worker
{
   private String name;
   private double rate;
   
   /**
    *  Construct a Worker object.
    */
   public Worker(String n, double r)  {
      name = n;
      rate = r;
   }

   /***  Get the rate of the pay. ***/
   public double getRate()   {
      return rate;
   }

   /*** Get worker name
    * <<< key word final: can not be overridden >>>
    **/
   public final String getName() {
       return name;
   }
      
   /**
   *  Abstract method to compute the pay.
   *  Abstract methods must be implemented in the subclass.
   */
   public abstract double computePay(int hours);
}



/**
*   A worker who gets paid by the hour.
*/
public class HourlyWorker extends Worker
{
   /**
   *   Construct a HourlyWorker object.
   */
   public HourlyWorker(String n, double r)  {
      super(n, r);
   }
   
  //public String getName() {
  //    return "name = " + super.name();
  //}
    
   /**
   *   Calculate the pay.
   *   Gets time and a half for overtime.
   */
   public double computePay(int hours) {
      double pay = getRate() * hours;
      if (hours > 40)  {
         pay = pay + getRate() * (hours - 40) / 2;
      }
      return pay;
   }
}





/**
*   A worker who gets paid with a salary.
*/
public class SalariedWorker extends Worker
{
   private int sickDays;

   /**
   *  Construct a SalariedWorker object.
   */
   public SalariedWorker(String n, double r)  {
      super(n, r);
      sickDays = 10;
   }

   /**
   *   Calculate the pay.
   *   Is paid for a 40 hour week no matter how many hours worked.
   */
   public double computePay(int hours)   {
      return getRate() * 40;
   }
   
   /**
   *   Take a sick day.
   */
   public void sickDay()   {
     sickDays--;
   }
      
   /**
    * return information about the worker
    */
   public String toString() {
       return "Name: " + getName() + ", Pay: $" + getRate() + " per hour" +
         ", Sick Days Remaining: " + sickDays;
   }
}



/**
*   This class tests class Worker and its subclasses.
*/
public class WorkerTester
{
   public static void main(String[] args)
   {
      // Can't make a new Worker since it is abstract
      // Worker bob = new Worker(); 
      
      Worker s1 = new SalariedWorker("Sally", 10);
      Worker h1 = new HourlyWorker("Howie", 10);
      
      System.out.println(s1); // runs toString method
      System.out.println("Pay for 30 hours: " + s1.computePay(30));
      System.out.println("Pay for 50 hours: " + s1.computePay(50));
      
      // Can't run method sickDay because s1 is a Worker.
      // must cast s1 to SalariedWorker
      // s1.sickDay();
      
      System.out.println("\n");
      System.out.println(h1); // runs toString method
      System.out.println("Pay for 30 hours: " + h1.computePay(30));
      System.out.println("Pay for 50 hours: " + h1.computePay(50));
     
 
   }
}