I have these interfaces:
public interface IShipOwner {}
public interface ICitizen {}
public interface IPlayer extends ICitizen, IShipOwner {}
public interface IAIPlayer exends IPlayer {}
Further I have these two methods in the same class:
public boolean isHumanPlayer(ICitizen citizen) {
if (citizen instanceof IPlayer) {
if (citizen instanceof IAIPlayer) {
return false;
} else {
return true;
}
}
return false;
}
public boolean isHumanPlayer(IShipOwner shipOwner) {
if (shipOwner instanceof IPlayer) {
if (shipOwner instanceof IAIPlayer) {
return false;
} else {
return true;
}
}
return false;
}
When calling isHumanPlayer
with an object of type IPlayer
I have to cast it either to type ICitizen
or IShipOwner
to make it clear which method should be called.
What is the best way from a client perspective for calling these methods? If possible I do want to avoid the need to cast the parameter while at the same time retaining the method overloading.
- Make citizen implement
IShipOwner
even if not everyIShipOwner
is aICitizen
and vice versa. - Use different method names.
- Something else I am not thinking of.