I've got the following (simplified) class definitions in my Java application:
public class Branch {
int branch_id;
String branch_name;
public Branch(int id, String name)
{
branch_id = id;
branch_name = name;
}
}
and:
public class BranchWithFleet extends Branch {
public Map<VanType, Integer> fleet;
public BranchWithFleet(int id, String name)
{
super(id, name);
fleet = new HashMap<VanType, Integer>();
}
I've written a routine to find "bad routes" whose arguments are based on the parent class (Branch) as it doesn't need any of the extra Fleet information, and has this general structure:
public void findBadRoutes(Map<Integer, Branch> branches, Integer rId)
{
if (!(branches.containsKey(rId)))
{
// bad
}
}
My question is as follows: if I am of the correct understanding that every BranchWithFleet is also a Branch, then why doesn't the following work?
Map<Integer, BranchWithFleet> branches = new HashMap<Integer, BranchWithFleet>();
Integer rId = 3;
findBadRoutes(branches, rId);
It fails with the following error in Eclipse's code-checking lint:
The method findBadRoutes(Map<Integer,Branch>, Integer) in the type [Type] is not applicable for the arguments (Map<Integer,BranchWithFleet>, Integer)
Surely a Map involving BranchWithFleet should be recognised as being one also identifiable with Branch?
EDIT: Foo(List<? extends Branch>) for the win: linked question