I’ve got a set of Java classes I’m using as a toolkit that I can plug into many projects. I do not want to change the code in this toolkit, so that any time I update it or fix a bug, I can just drop it into any of my projects that are using it, without needing to worry about local changes. Thus, if any local projects need to override a method in the toolkit, I just make a local version of the toolkit object, like such:
FILE: toolkit/Dog()
public class Dog(){
public void pet(){
print("scratch ruff");
}
}
FILE: local/Dog()
public class Dog extends toolkit/Dog {
public void pet(){
print("rub ears");
}
}
And in the local objects, I refer to the local Dog object instead of the toolkit Dog object.
This so far has worked great, but I just ran into an issue. Another class in the toolkit uses Dog.
FILE: toolkit/DogHandler
public void careForPack( List<Dog> arg_allTheDogs ){
for( Dog fido : arg_allTheDogs ){
fido.pet();
}
}
The problem that arises is that the system doesn’t like that these are not the same Dog object. I don’t want to locally override DogHandler, because I’ll just end up overriding my entire toolkit, which rather defeats the purpose.
Is there a way for the DogHandler to recognize Dog‘s child, also called Dog, as being valid?
You should make your toolkit method accept a
Listcontaining objects of a type that extendstoolkit.Dog. You can use? extends Tfor this: