Suppose we have two interfaces...
public interface StockBroker{
//Give our client some investment strategies.
public String adviseClient(Client c);
}
public interface Doctor{
//Examine our client and give them some medical advice
public String adviseClient(Client c);
}
And a class implementing both interfaces....
public class JackOfAllTrades implements StockBroker, Doctor{
public String adviseClient(Client c){
}
}
While it may be syntactically correct to implement both interfaces with one method, you may not get the desired behavior. For example, a stock broker and a doctor typically each give their clients vastly different advice.
Someone using an object that implements the interface Doctor
expects the adviseClient()
method to give medical advice. But someone using an object that implements the interface StockBroker
expects the adviseClient()
method to give out investment strategies.
In this case, the object JackOfAllTrades
does not know what type of advice to give out because the adviseClient()
method has no parameters telling it which interface it is supposed to be implementing when adviseClient()
is called.
This is a shortcoming in Java because the person designing the Doctor
interface may have had no way of knowing that someone else would design a StockBroker
interface with the same method signature.
To anyone creating interfaces, its probably good practice to make the method names unique enough that name collisions are rare.