Java Programming for Android Developers For Dummies
Book image
Explore Book Buy On Amazon
You can inherit a lot from your parents. Maybe not the ability to code your Android app with Java, but that’s another discussion. One thing you can't inherit is their experiences of having been born. Yes, you can see pictures that your grandparents took. But that's not the same as having been there.

At this point, you may feel like quibbling. What would it mean to “inherit” your parents' birth experiences? Well, you can stop right there. This is not an effort to form a perfect metaphor. It only helps to introduce an important fact about Java programming — the fact that classes don't inherit constructors from their parent classes.

Check out this code: What Is an Employee?

package com.allyourcode.company;

public class Employee {

public String name;

public String jobTitle;

public Employee() {

}

public Employee(String name, String jobTitle) {

this.name = name;

this.jobTitle = jobTitle;

}

<strong> public String getPayString() {</strong>

<strong> return name + ", Pay not known\n";</strong>

<strong> }</strong>

}

Now examine this code: Full-Time Employees Have Salaries

package com.allyourcode.company;

import java.text.NumberFormat;

import java.util.Locale;

public class FullTimeEmployee extends Employee {

<strong> public double salary;</strong>

static NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);

public FullTimeEmployee() {

}

public FullTimeEmployee(String name, String jobTitle, double salary) {

this.name = name;

this.jobTitle = jobTitle;

this.salary = salary;

}

<strong> public double pay() {</strong>

<strong> return salary;</strong>

<strong> }</strong>

<strong> </strong>

<strong> @Override</strong>

<strong> public String getPayString() {</strong>

<strong> return name + ", " + currency.format(pay()) + "\n";</strong>

<strong> }</strong>

}

Look at the constructors.

The FullTimeEmployee class extends the Employee class.

Both classes have parameterless constructors.

Both classes have constructors that initialize all of their fields.

In fact, a FullTimeEmployee constructor initializes three fields. Only one of these fields — the salary field — is declared in the FullTimeEmployee class's code. The FullTimeEmployee class inherits the other two fields — name and jobTitle — from the Employee class. This isn't a matter of FullTimeEmployee overriding its parent class's constructors. There are no constructors to override. Like any other subclass, the FullTimeEmployee class doesn't inherit its parent class's constructors.

Is there any way to avoid the loathsome redundancy of all the constructor declarations? There is. Java's super keyword can refer to a parent class's constructor:

public FullTimeEmployee(String name, String jobTitle, double salary) {

super(name, jobTitle);

this.salary = salary;

}

In this code, the FullTimeEmployee constructor calls its parent class's constructor. The call to super has two parameters and, as luck would have it, the parent Employee class has a two-parameter constructor:

public Employee(String name, String jobTitle) {

this.name = name;

this.jobTitle = jobTitle;

}

The super call sends two parameters to the parent class's constructor, and the parent class's constructor uses those two parameters to give name and jobTitle their initial values. Finally, the FullTimeEmployee class assigns a value to its own salary field. Everything works very nicely.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: