64

Class<? extends Something>

Here's my interpretation, it's class template but the class ? means the name of the class is undetermined and it extends the Something class.

if there's something wrong with my interpretation, let me know.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
lilzz
  • 5,243
  • 14
  • 59
  • 87
  • 3
    the ? means the `type` of the class is undetermined, but you are guaranteed that is extends `Something` – Hunter McMillen Dec 07 '11 at 21:31
  • 1
    yep. refer to http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html – Daniel Moses Dec 07 '11 at 21:32
  • possible duplicate of [What's the purpose behind wildcards and how are they different from generics?](http://stackoverflow.com/questions/2922811/whats-the-purpose-behind-wildcards-and-how-are-they-different-from-generics) – Joachim Sauer Oct 09 '13 at 12:55

5 Answers5

53

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
}
Daniel Moses
  • 5,872
  • 26
  • 39
  • Based on your explanation a question comes to mind. What would have been the difference between declaring the variable that way and using the Bar class directly? As far as I understand, any object from a class inheriting from Bar would have had the same behavior. Am I missing something? Thanks in advance! – Erizo Apr 10 '19 at 12:01
  • Answering my own question: https://stackoverflow.com/questions/2922811/whats-the-purpose-behind-wildcards-and-how-are-they-different-from-generics – Erizo Apr 10 '19 at 12:07
  • Wait - I think the original poster was specifically asking about the Class type. – rrrrrrrrrrrrrrrr Nov 16 '19 at 15:29
37

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.

Community
  • 1
  • 1
Krizz
  • 11,362
  • 1
  • 30
  • 43
10

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

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
8

You're correct.

In Java generics, the ? operator means "any class". The extends keyword may be used to qualify that to "any class which extends/implements Something (or is Something).

Thus you have "the Class of some class, but that class must be or extend/implement Something".

Mac
  • 14,615
  • 9
  • 62
  • 80
4

You're correct.

However usually you will want to name the class that extends Something and write e.g. <E extends Something>. If you use ? you can't do anything with the given type later.

Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114