I have 2 interfaces Sub
and Obs
as illustrated below:
public interface Sub<O extends Obs<? extends Sub>>{
public void addObs(O);
public void removeObs(O);
}
public interface Obs<S entends Sub<?>>{
public void update(S);
}
Now there are 2 concrete implementations of the above as class Vie
which implements Obs
and class Mod
which implements Sub
as below:
public class Vie<M extends Mod> implements Obs<M>{
public void update(M){
//some code;
}
}
public class Mod implements Sub<Vie<Mod>>{
Vie<Mod>[] obs = new Vie<Mod>[0]; //This is the line of error.
public void addObs(Vie<Mod>){
//some code;
}
public void removeObs(Vie<Mod>){
//some code;
}
}
Now my problem is when I want to initialise the array of Vie
in the class Mod
I get an error "cannot create generics array of Vie<Mod>
". So how can I initialise an array of Vie<Mod>
containing 0 elements.
NOTE I know of a solution:
@SuppressWarnings("unchecked")
Vie<Mod>[] obs = (Vie<Mod>[]) new Vie<?>[0];
I am not looking for this can anybody suggest anything else?
Edit I have already posted the answer that was there in the question which is attached as duplicate to this question. I was not looking for answers like that. I can share this another question like mine. But my point is that is there any other way?.
Now I add a second part to my question:
If no other solution, following my suggested solution, are there any problems in this casting as it would not be checked at the compile time. Is there any scope or any runtime exceptions?