Kevin Kevin - 24 days ago 8
Java Question

Undefined Constructor

I have the following code:

public class Cockpit extends Seat {

private Seat seat;

public Cockpit(String location) {
super("Cockpit");
}
. . .


But Eclipse is giving me an error saying "The constructor Cockpit() is undefined". I'm not sure where to go from here?

This is what I have for Seat:

public class Seat {

private String location;

public Seat(String location) {
this.location = location;
}

public String getLocation() {
return location;
}

public String toString() {
return location;
}
. . .

Answer

You're not showing the code causing the error, but I suspect it looks something like this:

Cockpit cockpit = new Cockpit();

The problem that your Cockpit constructor is expecting a String argument, but you're not passing one in. Since the location argument of Seat is hardcoded in the Cockpit constructor, you probably want to remove the parameter altogether:

public Cockpit() {
    super("Cockpit");
}

Alternatively, you can pass the location parameter into super() and specify its value when you call the constructor:

public Cockpit(String location) {
    super(location);
}
// ...
Cockpit cockpit = new Cockpit("Cockpit");