0

I was reading some code for learn something about manage data in Android. When i read a litle of the code...

I find this declaration.

public abstract class DBObject<T extends DBObject<?>> implements Cloneable{} 

I understadn that this is and Abstract class with T that is a Generic Object but i don't understand why y have to put a wildcart after Extend the same class.

Here is all the code https://github.com/tasks/tasks/blob/master/src/main/java/com/todoroo/andlib/sql/DBObject.java

1 Answers1

0

It looks like this is used so that it can be parameterised with its subclasses. It would be used like so:

class Foo extends DBObject<Foo> { ... }

It appears this is done solely so that the as() method will return the correct type.

The reason for the wildcard is because without it you would have to somehow specify a recursive generic definition, DBObject<T extends DBObject<T extends DBObject<...>>> - which is impossible.

Edit: Although see comments below; the wildcard is not actually necessary.

Logan Pickup
  • 2,294
  • 2
  • 22
  • 29
  • I don't think you'd have to. Enum is declared in a similar way and doesn't have to be recursive: http://stackoverflow.com/questions/3061759/why-in-java-enum-is-declared-as-enume-extends-enume – Marcin Koziński May 23 '16 at 22:11
  • @MarcinKoziński Yes, you're right - you don't even need the `>` wildcard. Some IDEs may complain without it, and that might be why it is in the code sample. – Logan Pickup May 23 '16 at 23:04