简体   繁体   中英

Java Generics: Foo<T>, Foo, Foobar<T extends Foo<T>> and Foobar<T extends Foo>

In Java Generics, given a generic class/interface Foo<T> , What's the difference between declaring a new generic class: Foobar<T extends Foo<T>> or simply Foobar<T extends Foo> , also why can I instantiate a generic class Foo<T> without instantiating the type parameter T ?, ie why can i write the following: Foo var = new Foo(); , does this mean that the class is instantiated with an object, through which i can only use the non-generic method? please forgive me if the question is not so clear, the example i was working on is the following: MyClass<T extends Comparable<T>>

class Foo<T> {}

is your class.

Foo yourVariable = new Foo();

equals Foo<Object> yourFoo = new Foo<Object>();

class Foobar<T> extends Foo {}

equals class Foobar<T> extends Foo<Object> {}

the answer to your question

class YourClass<T extends Comparable<T>> {}

means YourClass's type T is able to compare itself to objects of T (its class), whereas

class YourClass<T extends Comparable> {}

's type T is able to compare itself to objects of class Object , which is not what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM