J
Jarmo
I have a 3rd-party api that returns objects of class Dog. I want to add
some features to Dog but I cannot change the Dog class, nor can I change the
interface that returned me a Dog in the first place. One option might be to
write a new BigDog class that contains a Dog member and then implement
superficial getcolor(), setcolor(), getweight(), setweight() methods in
BigDog that simply call the same-named method in the contained Dog member.
But this doesn't seem to be ideal and results in me writing lots of
virtually useless code such as:
class BigDog
{
private Dog dog;
BigDog(Dog _dog)
{
dog = _dog;
}
int getcolor()
{
return dog.getcolor();
}
};
Another option might be for BigDog to extend Dog in which case I would
simply inherit the previously mentioned get/set methods with no extra code.
But in this case I don't see how to downcast, or otherwise convert, my Dog
object to a BigDog, even though all BigDogs are Dogs.
What's the best Java way to do this? Thanks.
some features to Dog but I cannot change the Dog class, nor can I change the
interface that returned me a Dog in the first place. One option might be to
write a new BigDog class that contains a Dog member and then implement
superficial getcolor(), setcolor(), getweight(), setweight() methods in
BigDog that simply call the same-named method in the contained Dog member.
But this doesn't seem to be ideal and results in me writing lots of
virtually useless code such as:
class BigDog
{
private Dog dog;
BigDog(Dog _dog)
{
dog = _dog;
}
int getcolor()
{
return dog.getcolor();
}
};
Another option might be for BigDog to extend Dog in which case I would
simply inherit the previously mentioned get/set methods with no extra code.
But in this case I don't see how to downcast, or otherwise convert, my Dog
object to a BigDog, even though all BigDogs are Dogs.
What's the best Java way to do this? Thanks.