Possible Duplicate:
Overriding return type in extended interface – Bad idea?
Lets consider the following interface,
public interface Cat{
public Object getCat();//returns any type of object
}
public interface DomesticCatInterface{
public String somethinOnlyRelatedToDomesticCat();
}
public interface WildCatInterface{
public String somethinOnlyRelatedToWildCat();
}
public class DomesticCat implements Cat, DomesticCatInterface{
@Override
public DomesticCat getCat(){
return this;
}
@Override
public String somethinOnlyRelatedToDomesticCat(){
return "something";
}
}
public class WildCat implements Cat, WildCatInterface{
@Override
public WildCat getCat(){
return this;
}
@Override
public String somethinOnlyRelatedToWildCat(){
return "something";
}
}
is this a good practice follow, when I need to get a reference from a class?
Instead of making getCat() return an Object, make it return a Cat. This you should be able to return any Class that implements Cat. In java 5 this is called Co-variant return types