I have a system of object instances that contain a reference to a definition object. I have a top-level class for each inheritance tree. The instance object has a generic reference to the corresponding definition class.
Using generics in the getter, a subclass of the top-level object can get the right type of definition without casting. However, an abstract subclass that is subclassed again cannot:
class Def { }
abstract class Animal<D extends Def> {
D def;
D getDef() { return def; }
}
class CatDef extends Def { }
class Cat extends Animal<CatDef> { }
abstract class BearDef extends Def { }
abstract class Bear<D extends BearDef> extends Animal<D> { }
class BlackBearDef extends BearDef { }
class BlackBear extends Bear<BlackBearDef> { }
class AnimalDefTest {
public static void main (String... args) {
Cat cat = new Cat();
CatDef catDef = cat.getDef(); // CatDef works fine
Bear bear = new BlackBear();
BearDef bearDef = bear.getDef(); // Error: Expected Def not BearDef? Why???
BearDef bearDef2 = ((Animal<BearDef>)bear).getDef(); // Works
}
}
Why does getDef
require a Bear
to be cast to (Animal<BearDef>
) to get a BearDef
? Bear is conclusively defined as extends Animal<? extends BearDef>
.
[Edit] Even stranger, if I change the Bear class line:
abstract class Bear<D extends BearDef> extends Animal<BearDef> { }
(In which case D is unused and is irrelevant) it still doesn't work. Erase D and the following line resolves the error in the code above (but doesn't help me do what I need to do with subclass definitions):
abstract class Bear extends Animal<BearDef> { }
super is a lower bound, and extends is an upper bound.
There are many advantages of using generics in Java. Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.
Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.
super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .
You're using the raw type Bear
when in fact it should be parameterized with a type of BearDef
. If you wrote
Bear<BlackBearDef> bear = new BlackBear();
or
Bear<?> bear = new BlackBear();
it'd work fine.
Another thing: I'm not sure how you intend to use this, but it seems likely to me that you would be fine just doing this instead:
abstract class Bear extends Animal<BearDef> {}
class BlackBear extends Bear {
// make use of covariant return type to make BlackBear return correct def
@Override
BlackBearDef getDef() { ... }
}
My reason for thinking this is that if you want a Bear
whose getDef()
method returns a BlackBearDef
, that Bear
reference would need to be parameterized as Bear<BlackBearDef>
. In that case (for your example anyway) you effectively know that it's a BlackBear
from the declared type, so you might as well just be referring to it as a BlackBear
. That said, it's obviously not always so simple and your actual situation may not allow this.
This will work too:
Bear<?> bear = new BlackBear();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With