Week 8 & 9 - Polymorphism
Week 8 & 9 - Polymorphism
Polymorphism
John Lewis
William Loftus
Late Binding
Polymorphism via Inheritance
Polymorphism via Interfaces
Sorting
Searching
obj.doIt();
Late Binding
Polymorphism via Inheritance
Polymorphism via Interfaces
Sorting
Searching
Holiday
Holiday day;
day = new Christmas();
Christma
s
StaffMembe
r
Volunteer Employee
Executive Hourly
• See Firm.java
• See Staff.java
• See StaffMember.java
• See Volunteer.java
• See Employee.java
• See Executive.java
• See Hourly.java
//-----------------------------------------------------------------
// Constructor: Sets up this staff member using the specified
// information.
//-----------------------------------------------------------------
public StaffMember (String eName, String eAddress, String ePhone)
{
name = eName;
address = eAddress;
phone = ePhone;
}
continue
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
public String toString()
{
String result = "Name: " + name + "\n";
return result;
}
//-----------------------------------------------------------------
// Derived classes must define the pay method for each type of
// employee.
//-----------------------------------------------------------------
public abstract double pay();
}
//-----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
//-----------------------------------------------------------------
public double pay()
{
return 0.0;
}
}
//-----------------------------------------------------------------
// Constructor: Sets up this employee with the specified
// information.
//-----------------------------------------------------------------
public Employee (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
continue
//-----------------------------------------------------------------
// Returns information about an employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
return result;
}
//-----------------------------------------------------------------
// Returns the pay rate for this employee.
//-----------------------------------------------------------------
public double pay()
{
return payRate;
}
}
//-----------------------------------------------------------------
// Constructor: Sets up this executive with the specified
// information.
//-----------------------------------------------------------------
public Executive (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
continue
//-----------------------------------------------------------------
// Awards the specified bonus to this executive.
//-----------------------------------------------------------------
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
//-----------------------------------------------------------------
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
//-----------------------------------------------------------------
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//-----------------------------------------------------------------
// Constructor: Sets up this hourly employee using the specified
// information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
continue
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
continue
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
return result;
}
}
//-----------------------------------------------------------------
// Constructor: Sets up the list of staff members.
//-----------------------------------------------------------------
public Staff ()
{
staffList = new StaffMember[6];
continue
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40);
}
continue
//-----------------------------------------------------------------
// Pays all staff members.
//-----------------------------------------------------------------
public void payday ()
{
double amount;
if (amount == 0.0)
System.out.println ("Thanks!");
else
System.out.println ("Paid: " + amount);
System.out.println ("-----------------------------------");
}
}
}
personnel.payday();
}
}
Late Binding
Polymorphism via Inheritance
Polymorphism via Interfaces
Sorting
Searching
//-----------------------------------------------------------------
// Constructor: Sets up this contact with the specified data.
//-----------------------------------------------------------------
public Contact (String first, String last, String telephone)
{
firstName = first;
lastName = last;
phone = telephone;
}
continue
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public String toString ()
{
return lastName + ", " + firstName + "\t" + phone;
}
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public boolean equals (Object other)
{
return (lastName.equals(((Contact)other).getLastName()) &&
firstName.equals(((Contact)other).getFirstName()));
}
continue
//-----------------------------------------------------------------
// Uses both last and first names to determine ordering.
//-----------------------------------------------------------------
public int compareTo (Object other)
{
int result;
if (lastName.equals(otherLast))
result = firstName.compareTo(otherFirst);
else
result = lastName.compareTo(otherLast);
return result;
}
continue
//-----------------------------------------------------------------
// First name accessor.
//-----------------------------------------------------------------
public String getFirstName ()
{
return firstName;
}
//-----------------------------------------------------------------
// Last name accessor.
//-----------------------------------------------------------------
public String getLastName ()
{
return lastName;
}
}
Sorting.selectionSort(friends);
Sorting.selectionSort(friends);
list[position] = key;
}
}
Late Binding
Polymorphism via Inheritance
Polymorphism via Interfaces
Sorting
Searching
if (found)
return list[index];
else
return null;
}
continue
Copyright © 2012 Pearson Education, Inc.
continue
if (found)
return list[mid];
else
return null;
}
continue
Sorting.selectionSort(friends);
Sorting.selectionSort(friends);