This is a group of classes that I made for context to the question
Aquarium class:
package test;
import java.util.ArrayList;
public class Aquarium {
// the aquarium is composed of fish tanks
ArrayList<Object> aquarium;
public Aquarium(){
aquarium = new ArrayList<Object>();
}
public <T> void addFishToTank(int index, T fish){
FishTank<T> tank = (FishTank<T>) aquarium.get(index);
tank.addFish(fish);
}
public ArrayList<Object> getAquarium(){
return aquarium;
}
public String toString(){
String holder = "";
int i = 0;
for (Object tank : aquarium){
holder += "Tank " + i + ":\n";
holder += tank.toString();
i ++;
}
return holder;
}
public static void main(String[] args){
Aquarium seaWorld = new Aquarium();
seaWorld.getAquarium().add(new FishTank<Salmon>());
seaWorld.addFishToTank(0, new Salmon());
seaWorld.addFishToTank(0, new Grouper());
System.out.println(seaWorld.toString());
}
}
FishTank class:
package test;
import java.util.ArrayList;
public class FishTank<T>{
// inside each fish tank there is a group of fish
ArrayList<T> fishGroup = new ArrayList<T>();
public void addFish(T element){
fishGroup.add(element);
}
public String toString(){
String holder = "";
for (T fish: fishGroup){
holder += fish.toString() + "\n";
}
return holder;
}
}
Salmon class:
package test;
public class Salmon {
public String toString(){
return "This is a salmon";
}
}
Grouper class:
package test;
public class Grouper {
public String toString(){
return "This is a grouper";
}
}
All the code compiles fine and the output of Aquarium ends up being
Tank 0:
This is a salmon
This is a grouper
How can this be possible? When I added the tank to the aquarium
seaWorld.getAquarium().add(new FishTank<Salmon>());
it was specified that it was a FishTank that can only hold Salmon objects. First, I added a Salmon object
seaWorld.addFishToTank(0, new Salmon());
and that would be fine since the fish tank at index 0 of the aquarium is a fish tank that holds Salmon objects. But when I add a Grouper object to the Salmon tank at index 0
seaWorld.addFishToTank(0, new Grouper());
it still adds the Grouper to the tank. Shouldn't an exception be thrown during run time? Then, how can I enforce so that only Salmon objects be added to the Salmon tank?