Java Reference
In-Depth Information
For example, suppose that we wanted to use an array to keep track of the
employees of a company. When creating an array, all the data in the array must
be of the same type. Because there are two types of employees, we could cre-
ate an array for Salary objects and a second array for Hourly objects.
Because Salary and Hourly objects are Employee objects, however, we can
create a single array for Employee objects that can contain both Salary and
Hourly objects. This is an example of a heterogeneous collection because the
elements in the array will not all be the same. They will all be Employee
objects, but some will be Salary objects and some will be Hourly objects. While
Salary and Hourly have a common parent, they are different objects.
Chapter 9 discusses arrays and other data structures used for containing
large amounts of data. In that chapter, I will show you how to create an
array of Employee objects and fill it with different Object types.
The data structures in the Java Collections Framework, such as Vector and
Hashtable, store java.lang.Object types. These collections can be quite
heterogeneous because you can store Employee objects alongside Radio
objects alongside Vehicle objects alongside anything because every object
is of type Object.
Virtual Methods
In this section, I will show you how the behavior of overridden methods in
Java allows you to take advantage of polymorphism when designing your
classes. In Chapter 6, “Understanding Inheritance,” we discussed method
overriding, where a child class can override a method in its parent. An over-
ridden method is essentially hidden in the parent class, and is not invoked
unless the child class uses the super keyword within the overriding method.
For example, suppose that we modify the Salary class in Listing 8.8, and
override the mailCheck() method from the Employee class.
public class Salary extends Employee
{
private double salary; //Annual salary
public Salary(String name, String address, int number, double
salary)
{
Listing 8.8
This Salary class overrides the mailCheck() method of Employee.
Search WWH ::




Custom Search