Java Reference
In-Depth Information
Display 7.5
The Derived Class
SalariedEmployee
(part 2 of 2)
39
/**
40
Returns the pay for the month.
41
*/
42
public
double
getPay()
43
{
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);
We will show you a better way to
define
equals
later in this
chapter.
69
}
70
}
Parent and Child Classes
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.
parent class
child class
ancestor class
descendent
class