Java Programming: Creating a Subclass

By Barry Burd

When you write an object-oriented program in Java, you start by thinking about the data. You’re writing about accounts. So what’s an account? You’re writing code to handle button clicks. So what’s a button? You’re writing a program to send payroll checks to employees. What’s an employee?

In this example, an employee is someone with a name and a job title. Sure, employees have other characteristics, but for now stick to the basics. The code in this listing defines what it means to be an employee.

import static java.lang.System.out;
public class Employee {
    private String name;
    private String jobTitle;
    public void setName(String nameIn) {
        name = nameIn;
    }
    public String getName() {
        return name;
    }
    public void setJobTitle(String jobTitleIn) {
        jobTitle = jobTitleIn;
    }
    public String getJobTitle() {
        return jobTitle;
    }
    public void cutCheck(double amountPaid) {
        out.printf("Pay to the order of %s ", name);
        out.printf("(%s) ***$", jobTitle);
        out.printf("%,.2fn", amountPaid);
    }
}

In the listing, an Employee class is defined. You can use what you define here and extend the definition to create new, more specialized classes. So in this next listing, you define a new class — a FullTimeEmployee class.

public class FullTimeEmployee extends Employee {
    private double weeklySalary;
    private double benefitDeduction;
    public void setWeeklySalary(double weeklySalaryIn) {
        weeklySalary = weeklySalaryIn;
    }
    public double getWeeklySalary() {
        return weeklySalary;
    }
    public void setBenefitDeduction(double benefitDedIn) {
        benefitDeduction = benefitDedIn;
    }
    public double getBenefitDeduction() {
        return benefitDeduction;
    }
    public double findPaymentAmount() {
        return weeklySalary - benefitDeduction;
    }
}

Looking at the listing, you can see that each instance of the FullTimeEmployee class has two fields: weeklySalary and benefitDeduction. But are those the only fields that each FullTimeEmployee instance has? No, they’re not.

The first line says that the FullTimeEmployee class extends the existing Employee class. This means that in addition to having a weeklySalary and a benefitDeduction, each FullTimeEmployee instance also has two other fields: name and jobTitle. These two fields come from the definition of the Employee class, which you can find in the first listing.

In the second listing, the magic word is extends. When one class extends an existing class, the extending class automatically inherits functionality that’s defined in the existing class. So, the FullTimeEmployee class inherits the name and jobTitle fields.

The FullTimeEmployee class also inherits all the methods that are declared in the Employee class — setName, getName, setJobTitle, getJobTitle, and cutCheck. The FullTimeEmployee class is a subclass of the Employee class.

That means the Employee class is the superclass of the FullTimeEmployee class. You can also talk in terms of blood relatives. The FullTimeEmployee class is the child of the Employee class, and the Employee class is the parent of the FullTimeEmployee class.

It’s almost (but not quite) as if the FullTimeEmployee class was defined by the code in this listing. This code is fake, but informative.

import static java.lang.System.out;
public class FullTimeEmployee {
    private String name;
    private String jobTitle;
    private double weeklySalary;
    private double benefitDeduction;
    public void setName(String nameIn) {
        name = nameIn;
    }
    public String getName() {
        return name;
    }
    public void setJobTitle(String jobTitleIn) {
        jobTitle = jobTitleIn;
    }
    public String getJobTitle() {
        return jobTitle;
    }
    public void setWeeklySalary(double weeklySalaryIn) {
        weeklySalary = weeklySalaryIn;
    }
    public double getWeeklySalary() {
        return weeklySalary;
    }
    public void setBenefitDeduction(double benefitDedIn) {
        benefitDeduction = benefitDedIn;
    }
    public double getBenefitDeduction() {
        return benefitDeduction;
    }
    public double findPaymentAmount() {
        return weeklySalary - benefitDeduction;
    }
    public void cutCheck(double amountPaid) {
        out.printf("Pay to the order of %s ", name);
        out.printf("(%s) ***$", jobTitle);
        out.printf("%,.2fn", amountPaid);
    }
}

Why is this listing fake code? Well, the main difference between this listing and the inheritance situation in the previous listings is this: A child class can’t directly reference the private fields of its parent class. To do anything with the parent class’s private fields, the child class has to call the parent class’s accessor methods.

Back in the second listing, calling setName(“Rufus”) would be legal, but the code name=”Rufus” wouldn’t be. If you believe everything you read in the last listing, you’d think that code in the FullTimeEmployee class can do name=”Rufus”. Well, it can’t.

You don’t need the Employee.java file on your hard drive to write code that extends the Employee class. All you need is the file Employee.class.