T
Taras_96
Hi everyone,
I'm wondering if there's a way of achieving the following using Java
generics.
* animal class contains a set of objects of type 'animal'
* dog class extends animal, and contains a set of objects of type
'dog'
* cat class extends animal, and contains a set of objects of type
'cat'
* ....
ie: the various derived classes have sets of objects of types equal to
their own derived types. Since this architecture (ie: the set) is
common to all derived classes, it should have this logic in the Base
class. One way of achieving this is:
public class Animal <T extends Animal>
{
Set<T> theSet;
}
public class Dog extends Animal<Dog>
{
}
However, I don't like this solution because it exposes implementation
through the generic parameter. What ideally I would like is for
something similar to the following, but defined at compile time:
public class Animal
{
Set<the most specific type of object pointed to by the this pointer>
theSet;
}
Thanks
Taras
I'm wondering if there's a way of achieving the following using Java
generics.
* animal class contains a set of objects of type 'animal'
* dog class extends animal, and contains a set of objects of type
'dog'
* cat class extends animal, and contains a set of objects of type
'cat'
* ....
ie: the various derived classes have sets of objects of types equal to
their own derived types. Since this architecture (ie: the set) is
common to all derived classes, it should have this logic in the Base
class. One way of achieving this is:
public class Animal <T extends Animal>
{
Set<T> theSet;
}
public class Dog extends Animal<Dog>
{
}
However, I don't like this solution because it exposes implementation
through the generic parameter. What ideally I would like is for
something similar to the following, but defined at compile time:
public class Animal
{
Set<the most specific type of object pointed to by the this pointer>
theSet;
}
Thanks
Taras