Java Reference
In-Depth Information
Display 7.4
Inheritance Demonstration
The methods
getName
and
setName
are
inherited from the base class
Employee
.
1
public class InheritanceDemo
2 {
3
public static void
main(String[] args)
5 HourlyEmployee joe =
new
HourlyEmployee("Joe Worker",
4 {
6
new
Date("January", 1, 2004),
50.50, 160);
7 System.out.println("joe's longer name is " +
joe.getName());
8 System.out.println("Changing joe's name to Josephine.");
9 joe.setName("Josephine");
10 System.out.println("joe's record is as follows:");
11 System.out.println(joe);
12 }
13 }
Sample Dialogue
joe's longer name is Joe Worker
Changing joe's name to Josephine.
joe's record is as follows:
Josephine January 1, 2004
$50.5 per hour for 160 hours
Inherited Members
A derived class automatically has all the instance variables, all the static variables, and
all the public methods of the base class. These members from the base class are said
to be
inherited
. These inherited methods and inherited instance and static variables are,
with one exception, not mentioned in the definition of the derived class, but they are
automatically members of the derived class. The one exception is as follows: As explained
in the subsection “Overriding a Method Definition,” you can give a definition for an
inherited method in the definition of the derived class; this will redefine the meaning of the
method for the derived class.