in Java syntax, Class<? extends Something>
Solution 1:
There are a few confusing answers here so I will try and clear this up. You define a generic as such:
public class Foo<T> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
If you want a generic on Foo to always extend a class Bar you would declare it as such:
public class Foo<T extends Bar> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
The ?
is used when you declare a variable.
Foo<? extends Bar>foo = getFoo();
OR
DoSomething(List<? extends Bar> listOfBarObjects) {
//internals
}
Solution 2:
You are almost right.
Basically, Java has no concept of templates (C++ has).
This is called generics.
And this defines a generic class Class<>
with the generics' attribute being any subclass of Something
.
I suggest reading up "What are the differences between “generic” types in C++ and Java?" if you want to get the difference between templates and generics.
Solution 3:
You're right
Definition is that the class has to be subtype of Something
It's the same as Class<T>
, but there is a condition that T
must extends Something
Or implements Something
as Anthony Accioly suggested
It can also be class Something
itself