Download presentation source

Download Report

Transcript Download presentation source

CS100A, 5 Sept. 1998
This lecture continues the discussion of
classes. The important new concept of this
lecture is inheritance. This will require to
introduce constructors and to use the Java
keywords extends, this, and super.
Inheritance is motivated by extending class
Employee of the previous lecture to distinguish between kinds of employees: VIPs (the
CEO and other major officers), salaried employees, and regular employees. Each kind of
employee requires different information in
their record, and there is different processing
for each. Placing the information for all three
kinds of employees in class Employee wastes
space and causes complications in the code;
using classes that are extensions of class
Employee eliminates these problems.
CS100A, 15 Sept. 1998.
Lecture 5
1
CS100A,
15 September 1998
Concepts for this lecture:
• Inheritance: a subclass B of a class A
inherits fields and methods from class
A. A is a super-class of B. In Java,
keyword extends is used to define a
subclass.
• Using the constructor of a super class.
• Access modifier protected
• Overriding methods
• Method toString() for every class
Reading: Holmes --look in index and try
to find appropriate pages to read
CS100A, 15 Sept. 1998.
Lecture 5
2
Method toString
// Yield a String containing the data for
// the person
public String toString()
{return name + " " +
salary + " " +
hireDate;
}
Convention: Every class implements method
toString!
In
<String> + B
(catenation), if B is not a String, method
B.toString() is automatically used to convert
it to a String.
CS100A, 15 Sept. 1998.
Lecture 5
3
Our old class Employee
// An instance of Employee contains a person's name,
// salary, and year hired. It has a constructor and
// methods for raising the salary, printing the data, and
// retrieving the person's name and the year hired.
public class Employee
{public String name;
// The person's name
public double pay;
// The person's yearly salary
public int hireDate;
// The year hired
// Constructor: a person with name n, salary s, and
// year d hired
public Employee(String n, double s, int d)
{name= n;
pay= s;
hireDate= d;
}
CS100A, 15 Sept. 1998.
Lecture 5
4
// Raise the salary by p percent
public void raiseSalary(double p)
{pay= pay * (1 + p/100.0);}
// Yield the year the person was hired
public int hireYear()
{return hireDate;}
// Yield the person's name
public String getName()
{return name;}
// Yield a String containing the data for the person
public String toString()
{return name + " " +
pay + " " +
hireDate;
}
}
(old class Employee)
CS100A, 15 Sept. 1998.
Lecture 5
5
Our task today
Modify class Employees to take into account
three different kinds:
• VIPS (e.g. CEO, president, vice president):
Need a field bonus, since VIPS get (big)
bonuses. Get a yearly salary.
• Salaried: Expected to work overtime
whenever necessary, without extra pay.
• Regular: Time cards! Have an hourly wage
instead of a yearly salary. (Need also to
record the number of hours worked on each
day, but we’ll forego that here and assume
40 hours per week.
CS100A, 15 Sept. 1998.
Lecture 5
6
How to implement?
Add fields:
int employeeKind; // 1 = VIP, 2 = salaried,
// 3 = regular
double bonus;
// bonus for VIP (only)
double hourlyWage; // (for regular
// employees only
// Set bonus to b (if employee is a VIP)
public setBonus(double b) {
if (employeeKind = 1)
bonus= b;
}
A lot of other changes are required.
CS100A, 15 Sept. 1998.
Lecture 5
7
Problems with this approach
• Each employee’s record has a field for a
bonus and for an hourly wage, even though
it may not need it.
• Within the methods of class Employee that
deal with salary and wages and hours, tests
will have to be made to determine which
kind of employee is being processed. This
makes the code longer, less well-structured,
and confusing.
• If a new kind of employee were to be added
that required a different processing for pay,
all the code that deals with pay would have
to be changed.
• Instead, use a different approach:
inheritance.
CS100A, 15 Sept. 1998.
Lecture 5
8
// An instance of VIP contains a VIP's data
public class VIP extends Employee
{private double bonus; // The VIP's bonus
// Constructor: a person with name n, year d hired,
// yearly pay s, and bonus b
public VIP(String n, int d, double s, double b)
{super (n,d);
pay= s; bonus= b;}
// Yield a String containing the data for the person
public String toString()
{return “VIP + " name + " " + pay + " " +
" " + bonus + " " + hireDate;
}
// Change the bonus to p
public void changeBonus(double p)
{bonues= p;}
}
CS100A, 15 Sept. 1998.
Lecture 5
9
An instance of class VIP has every field and
method that an instance of class Employee does,
plus the ones VIP declares.
Employee x;
x= new Employee(“Gries”, 1969);
Employee
x
Name Gries
pay
hireDate 1969
Employee, hireYear,
getName, toString
0
Employee y= new VIP(“Cardie”,1994, 90000,1000);
VIP
y
Name Cardie
pay 90000
hireDate 1994
Employee, hireYear,
getName, toString
bonus 1000
VIP changeBonus
CS100A, 15 Sept. 1998.
Lecture 5
10
Use of super
For an instance of VIP. A constructor of super
class Employee should be called to initialize
the fields declared within Employee. This is as
the first statement in a constructor for VIP:
super (n,d);
Use of protected
If fields of Employee are public, they can be
referenced from anywhere.
If they are private, they can be referenced only
from instances of Employee.
If they are protected, they can be referenced
only in same package --concept discussed
much later!!!
CS100A, 15 Sept. 1998.
Lecture 5
11
Referencing methods
VIP y;
y= new VIP(“Perkins”, 1983, 90000, 1000);
y.getName() refers to method getName of its
superclass, Employee. This is because
getName is not defined in VIP.
y.getBonus() refers to method getBonus of
VIP. This is because getBonus is defined in
VIP.
y.toString() refers to method toString of VIP.
This is because toString is defined in VIP.
Method toString of superclass Employee has
been overridden in class VIP.
CS100A, 15 Sept. 1998.
Lecture 5
12