I started studying Java-generics. And I have some misunderstanding of the generics syntax and its meaning. I beg to treat with understanding if my question seems too trivial.
You can write:
public class MyClass<SomeClass> {}
and you can write:
public class MyClass<C extends SomeClass> {}
and you can write also:
public class MyClass<? extends SomeClass> {}
What is the difference between these cases ?
Are my guesses right or not? And especially what is the difference between the using of the second and third cases?
Thanks in advance for explanation!
Well the difference is you cant use ?
in generic class declaration
public class MyClass<? extends SomeClass> {} // this isn't valid
The above declaration leads to compiler error.
A generic class is defined with the following format:
class name { /* ... */ } The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.
public class MyClass<C extends SomeClass> {}
In this declaration C
is a type-argument which could be of type SomeClass or any of its SubClass's.
Example :
Class SomeOtherClass extends SomeClass {
}
MyClass clazz = new MyClass<SomeOtherClass>();
MyClass clazz = new MyClass<SomeClass>();
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.