Java Reference
In-Depth Information
Display 7.3
The Derived Class HourlyEmployee (part 3 of 3)
82 }
83 }
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);
We will show you a better way to
define equals in the subsection
“The Right Way to Define equals.”
95 }
96 }
Derived Class (Subclass)
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 it
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
in the subsection “Overriding a Method Definition,” 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.
 
 
Search WWH ::




Custom Search