Professional Documents
Culture Documents
11 InheritanceExamples PDF
11 InheritanceExamples PDF
Behavior. Our program should read a sequence of Input file name String varying args[0]
employees from an input file, ( managers, secretaries, Employee Employee varying employee[i]
pay and print a paycheck for each employee showing the Secretaries Secretary varying --
Programmers Programmer varying --
employee’s name, ID, and pay.
Consultants Consultant varying --
Pay double varying employee[i].pay( )
Paycheck Paycheck varying paycheck
Employee’s name String varying employee[i].name( )
1 2
OOD Analysis
Kind of employee Common Attributes
Managers, programmers salary Object
Create a SalariedEmployee class
Make Manager and Programmer subclasses.
Payroll Paycheck
Employee
Generator
Secretaries, consultants hourly wage, hours
Create an HourlyEmployee class
Salaried Hourly
Make Secretary and Consultant subclasses. Employee Employee
Salaried employee, hourly employees name, ID number, Manager Programmer Consultant Secretary
pay, etc.
Create an Employee class
Make SalariedEmployee and HourlyEmployee
subclasses.
3 4
1
Operation Responsibility of
Object
5 6
2
class HourlyEmployee extends Employee {
public final static double OVERTIME_THRESHOLD = 40;
public final static double OVERTIME_FACTOR = 1.5;
But only subclasses SalariedEmployee and
HourlyEmployee will know how to compute their pay, //--- Constructors ---
...
and so we leave it to them to supply a definition for this //--- Accessor methods ---
method: ...
//--- Pay for hourly employee ---
public double pay() {
class SalariedEmployee extends Employee { if (myHours <= OVERTIME_THRESHOLD)
return myHours * myHourlyRate;
//--- Constructors --- else
... return OVERTIME_THRESHOLD * myHourlyRate +
//--- Accessor methods --- (myHours - OVERTIME_THRESHOLD) *
public double pay() { return salary(); } myHourlyRate * OVERTIME_FACTOR;
public double salary() { return mySalary; } }
... ...
//--- String converter (for output) --- //--- String converter (for output) ---
... ...
//--- File input --- //--- File input --- }
... ... pay() is defined differently
//--- Attribute variables --- //--- Attribute variables --- in SalariedEmployee and
private double mySalary; private double myHourlyRate;
} HourlyEmployee.
private double myHours; Polymorphism selects the
}
correct version.
9 10
The Manager class is defined as a subclass of Similarly, the Programmer class is defined as a subclass of
SalariedEmployee. Note that although pay() was an SalariedEmployee:
abstract method in the root class Employee, it was defined
in SalariedEmployee, and it is this definition that is class Programmer extends SalariedEmployee
{
inherited by Manager. //--- Constructors ---
...
class Manager extends SalariedEmployee //--- Accessor method ---
{ public String project() { return myProject; }
//--- Constructors --- ...
... //--- File input ---
//--- Accessor method --- ...
public String division() { return myDivision; }
... //--- Attribute variable ---
//--- String converter (for output) --- private String myProject;
... }
//--- File input ---
//--- Attribute variable --- And Secretary and Consultant classes are defined as
private String myDivision; subclasses of HourlyEmployee and inherit it's methods,
}
including the pay() method.
11 12
3
//--- Accessor methods ---
public String name() { return myName; }
public double amount() { return myAmount; }
The Paycheck class is designed to model paychecks: public int id() { return myID; }
4
employees.txt java PayrollGenerator employees.txt
Java has a class named Class that provides various 7 Grumpy $950.00
operations for manipulating classes. Two useful ones are Manager 4
Grumpy
forName() and newInstance(). If String str is 4 Bashful $380.00
the name of a class, 950.00
Java
1
(Employee)Class.forName(className).newInstance() Consultant
Doc Programmer
creates an object of type className. 2 Sleepy
6
15.90
17 20 900.00 18
Java Threads
19 20
5
/** SnowOwl.java provides a subclass of Owl that models a
* snow owl. It provides a constructor and a definition
* of getCall().
*/
21