I reading a book about OOP patterns with Java examples.. Class code example from book:
public class Board {
private int width, height;
private List tiles;
...
private void initialize() {
tiles = new ArrayList(width);
for (int i=0; i<width; i++) {
tiles.add(i, new ArrayList(height));
for (int j=0; j<height; j++) {
((ArrayList)tiles.get(i)).add(j, new Tile());
}
}
}
public Tile getTile(int x, int y) {
return (Tile)((ArrayList)tiles.get(x-1)).get(y-1);
}
}
Question:
Why in both described methods used Explicit type conversion? In first case to ArrayList in line ((ArrayList)tiles.get(i)).add(j, new Tile());
, in second to Tile in line return (Tile)((ArrayList)tiles.get(x-1)).get(y-1);
. I mean, this is script author opinion or necessary in Java case?