Java Reference
In-Depth Information
Dynamic Binding
Arrays can store objects of different types, that is, of the base class and of
any of its subclasses. This creates the possibility of defining an array of ob-
jects of a superclass and then filling the array at runtime with objects of any
class in the inheritance hierarchy. Since the objects are created when the
program runs, it is clear that the binding between the object and the method
takes place at runtime, which is a case of true polymorphism.
The program named ObjectArray.java, listed below, uses the classes
Employee and HourlyEmployee to demonstrate dynamic binding with an
array of objects. During program execution the user selects an object of
either class, which is then inserted in the object array. The binding is dy-
namic since the object is not known until the program runs.
On the Web
The source file for the program ObjectArray.java can be found in the
Chapter17folderat www.crcpress.com .
.
//***********************************************************
// Program: ObjectArray
// Reference: Chapter 17
// Topics:
//
1. Creating and using an array of objects
//
2. Creating an object at runtime
//
3. Demonstrates dynamic binding (runtime polymorphism)
//
// Requires:
// 1. Keyin class in the current directory
//***********************************************************
//******************************************
//******************************************
// Employee class
//******************************************
//******************************************
class Employee
{
//********************************
// attributes section
//********************************
// Instance fields
private String name;
private String address;
private String ssn;
private int dependents;
private int empNum;
// Class attribute (note the static qualifier)
Search WWH ::




Custom Search