Using generic methods as an alternative to overloading?
Both ideas are bad. Your ObjectA
and ObjectB
classes should implement a common interface that defines getId()
and getName()
methods.
Then you can get away with a single method:
public String getInfo(Interface o){
if(o.getId()!=1) return o.getName();
return "";
}
Your case is a good candidate for Programming to Interface. Create an interface and move common methods.
public String getInfo(InterfaceAB in){
if(in.getId()!=1) return in.getName();
return "";
}