Java Reference
In-Depth Information
Display 7.3
The Derived Class
HourlyEmployee
(part 3 of 3)
The method
toString
is overridden so it is different in the
derived class
HourlyEmployee
than it is in the base class
Employee
.
84
public
String toString()
85
{
86
return
(getName() + " " + getHireDate().toString()
87
+ "\n$" + wageRate + " per hour for " + hours + " hours");
88
}
89
public
boolean
equals(HourlyEmployee other)
90
{
91
return
(getName().equals(other.getName())
92
&& getHireDate().equals(other.getHireDate())
93
&& wageRate == other.wageRate
94
&& hours == other.hours);
95
}
We will show you a better way to define
equals
later in this chapter.
96
}
Derived Class (Subclass)
You define a
derived class
by starting with another already defined class and adding (or
changing) methods, instance variables, and static variables. The class you start with is called
the
base class
. The derived class inherits all the public methods, all the public and private
instance variables, and all the public and private static variables from the base class and can
add more instance variables, more static variables, and more methods. So, of the things we
have seen thus far, the only members not inherited are private methods. (As discussed a little
later in this chapter, the derived class definition can also change the definition of an inherited
method.) A derived class is also called a
subclass
,
in which case the base class is usually
called a
superclass
.
SYNTAX
public class
Derived_Class_Name
extends
Base_Class_Name
{
Declarations_of_Added_Static_Variables
Declarations_of_Added_Instance_Variables
Definitions_of_Added__And_Overridden_Methods
}
EXAMPLE
See Displays 7.3 and 7.5.