Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this generic recognize its superclass boundary (Java)?

Tags:

java

generics

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> { }
like image 860
Nicole Avatar asked Nov 10 '10 16:11

Nicole


People also ask

What is the difference between List <? Super T and List <? Extends T?

super is a lower bound, and extends is an upper bound.

Are Java generics capable of preventing runtime error?

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.

How to understand Java generics?

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.

What is Super T means in Java?

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 .


2 Answers

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.

like image 150
ColinD Avatar answered Oct 26 '22 23:10

ColinD


This will work too:

Bear<?> bear = new BlackBear(); 
like image 30
Allen P Avatar answered Oct 26 '22 23:10

Allen P