I have a class Ship
public class Ship {
private String name;
private boolean loaded;
private int size;
private boolean bIsDefeated;
private int gunpower;
public Ship(int size, int gunpower, String name) {
this.size = size;
this.gunpower = gunpower;
this.name= name;
loaded = true;
bIsDefeated = false;
}
}
and Submarine
class Submarine extends Ship {
private final String NAME = "U-Boot";
private final int SIZE = 2;
private final int GUNPOWER = 1;
public Submarine(){
super(SIZE,GUNPOWER,NAME); //Here it gets underlined
}
}
Can anyone tell me why that is not possible?
Looks like you're trying to make a constructor with a different name than the class. Try a
static factory methodwhere
UBOAT_SIZEandUBOAT_GUNPOWDERareprivate static final intvariables in your classand
Ship's constructor is wrongShould be
EDIT
Okay you've changed your question now...
SIZE,GUNPOWDER, andNAMEall need to beprivate static final ...variables because you don't have an instance of Submarine at the time of the constructor -- so they must bestatic