Java Reference
In-Depth Information
Display 7.5
The Derived Class
SalariedEmployee
(part 2 of 2)
39
/**
40
Returns the pay for the month.
42
public double
getPay()
43 {
41
*/
44
return
salary/12;
45 }
46
/**
47
Precondition: newSalary is nonnegative.
48
*/
49
public void
setSalary(
double
newSalary)
50 {
51
if
(newSalary >= 0)
52 salary = newSalary;
53
else
54 {
55 System.out.println("Fatal Error: Negative salary.");
56 System.exit(0);
57 }
58 }
59
public
String toString()
60 {
61
return
(getName() + " " + getHireDate().toString()
62 + "\n$" + salary + " per year");
63 }
64
public boolean
equals(SalariedEmployee other)
65 {
66
return
(getName().equals(other.getName())
67 && getHireDate().equals(other.getHireDate())
68 && salary == other.salary);
69 }
70 }
We will show you a better way to
define
equals
in the subsection
“The Right Way to Define equals.”
parent class
Parent and Child Classes
child class
A base class is often called the
parent class
. A derived class is then called a
child class
.
This analogy is often carried one step further. A class that is a parent of a parent of a parent
of another class (or some other number of “parent of” iterations) is often called an
ancestor
class
. If class A is an ancestor of class B, then class B is often called a
descendent
of class A.
ancestor class
descendent
class