Javanook Home

Inheritance

Examples >> Core | Inheritance


Objectives

listing 1

// Employee.java

import java.util.Vector;

/*
* abstract class, so it cannot be instantiated.
* implements Comparable, so Collections.sort(list) can use it
*/
public abstract class Employee implements Salary, Comparable {

// protected to make it accessible only to derived classes
protected int accountNumber;
protected String firstName;
protected String lastName;
protected double salary;
protected String type;
protected Vector randomNumbers = new Vector();

// recursive method to generate random number
protected int getRandomNumber() {
int R = (int)(Math.random() * 999);

// wrap into Integer object, so Vector can store it
Integer rI = new Integer(R);

// check to see if number is greater than 99
// also, it is not already there in the Vector
if(R >= 100 && !randomNumbers.contains(rI)) {
// meets the condtion, break from recursion
randomNumbers.add(rI);
return R;
} else {

// recurse until the condition is met
return getRandomNumber();
}
}

// method to add increment to salary
public void updateSalary(int percent) {
salary += (salary * percent)/100;
}

// set account number
public void setAccountNumber(int number) {
accountNumber = number;
}

// set first name
public void setFirstName(String fname) {
firstName = fname;
}

// set last name
public void setLastName(String lname) {
lastName = lname;
}

//set last name
public void setSalary(double sal) {
salary = sal;
}

// set emp type
public void setType(String type) {
this.type = type;
}

// get account number
public int getAccountNumber() {
return accountNumber;
}

// get first name
public String getFirstName() {
return firstName;
}

// get last name
public String getLastName() {
return lastName;
}

// get salary
public double getSalary() {
return salary;
}

public String getType() {
return type;
}

// needed to sort
// Used by the Collections.sort(list) method
public int compareTo(Employee emp) {
if(lastName.equals(emp.lastName)) {
return firstName.compareTo(emp.firstName);
}
return lastName.compareTo(emp.lastName);
}

public void display() {
System.out.println(accountNumber+"..."+firstName+" "+lastName+"..."+type+"..."+salary);
}
}

The Employee class has a random number generator. It generates a 3 digit account number such that no duplicate account numbers are possible. The random numbers so generated are in the range 100 to 999.

The class has a sort method which sorts the employees by last name. If two or more employees have the same last name then they are sorted by first name. To accomplish the sort operation, compareTo method is used. The java runtime ensure that this method is called for comparison whenever two Employee objects are compared. This is possible because the Employee class implements the Comparable interface which has just one method - compareTo.

A note on the Comparable interface

The Employee class implements the Comparable interface so that a natural ordering of the objects occurs. The compareTo() method is implemented so that it is consistent with the equals() method.

The Employee objects are stored in a data access class called EmployeeDAO, which uses an ArrayList to store Employee objects. The method sort() in the EmployeeDAO class merely calls the Collections.sort() method to sort the Employee objects. 

The sort implementation in lists and arrays (Collections.sort()) uses the compareTo() method of the class that implements the Comparable interface. In our case it is the Employee class.

We will now take a look at the EmployeeDAO class that stores the Employee objects in an ArrayList. It also has the sort method that automatically sorts the Employee objects (see note above).

listing 2

// EmployeeDAO.java

import java.util.*;

public class EmployeeDAO {

java.util.ArrayList list;

public EmployeeDAO() {
// initialize ArrayList object
list = new ArrayList();
}

// add employee to the list
public void add(Employee emp) {
list.add(emp);
}

public boolean contains(Object o) {
return list.contains(o);
}

public int indexOf(Object o) {
return list.indexOf(o);
}

public Employee get(int index) {
Object o = list.get(index);
return (Employee)o;
}

public int size() {
return list.size();
}

public void sort() {
Collections.sort(list);
}
}

The example also shows the use of a java interface. Interface Salary has just one method updateSalary(). The Employee class has three derived classes

Each of the above classes extends the Employee class and implements the Comparable interface. In the compareTo() method, we merely call the super class method. It is possible to provide a custom implementation in each class.

The entire source code is available for download. Click here to download inheritance.zip. There is a driver program EmployeeDriver.java to run the example. Compile all the classes and run java EmployeeDriver from the command line. The program runs interactively with the user with a simple menu display on the console. Five Employee objects are hard coded, though the user may add any number of employees to the  program.