Reputation: 417
I have a parent class :
public class Partida {
private ArrayList<Jugador> jugadores;
private Tablero tablero;
private ArrayList<PartidaListener> observadores;
public Partida(Tablero tablero, ArrayList<Jugador> jugadores) {
observadores = new ArrayList<>();
this.tablero = tablero;
this.jugadores = jugadores;
}
That is inherited by the following class:
public class PartidaC4 extends Partida {
private ArrayList<JugadorC4> jugadores;
private TableroC4 tablero;
private ArrayList<PartidaListenerC4> observadores;
public PartidaC4(TableroC4 tablero, ArrayList<JugadorC4> jugadores) {
observadores = new ArrayList<>();
this.tablero = tablero;
this.jugadores = jugadores;
}
When writing this, I get the error
Error:(39, 73) error: constructor Partida in class Partida cannot be applied to given types;
required: Tablero,ArrayList found: no arguments
reason: actual and formal argument lists differ in length
I think it's very basic but I can't find a solution. I tried to use both solutions provided by Android Studio (using super()
) but I'm not happy with this solution, because I need to use different objects types than the ones from the parent class.
What should I do?
Upvotes: 0
Views: 9372
Reputation: 737
You can define any arguments you need for your constructor, but it is necessary to call one constructor of the super class as the first line of your own constructor. This can be done using super() or super(arguments).
public class PartidaC4 extends Partida {
public PartidaC4() {
super(tablero,jugadores);
//do whatever you want to do in your constructor here
}
public PartidaC4(TableroC4 tablero, ArrayList<JugadorC4> jugadores) {
super(tablero, jugadores);
//do whatever you want to do in your constructor here
}
}
Upvotes: 2
Reputation: 36401
PartidaC4
class is a subclass of Partida
so when instanciating a PartidaC4
you need to specify how its Partida
part should be initialized. As you didn't say anything about this Java tried to initialize the Partida
part using a construtor with no arguments, but there is no in Partida
class. So the error.
We don't really know what you need but it looks like you want to initialize this way:
public Partida(Tablero tablero, ArrayList<Jugador> jugadores) {
super(tablero, jugadores): // initialize the "super" part with appropriate arguments.
observadores = new ArrayList<>();
this.tablero = tablero;
this.jugadores = jugadores;
}
As already mentioned, you should be aware that there is redundancy in your definitions: a PartidaC4
has tablero
, jugadores
and observadores
defined in it plus tablero
, jugadores
and observadores
inherited from the super class. I doubt it is what you want...
Upvotes: 2
Reputation: 93559
Either your base Partida class needs a no argument constructor, or your PartidaC4 class needs to call a constructor of it in its constructor using super(). Most likely you want the second, since you haven't already defined a no args constructor.
Also, your code smells a bit. You shouldn't be constructing the same variables in both the base and the parent class. One of the two should be defining these, not both.
Upvotes: 0