Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Java syntax, Class<? extends Something>

Tags:

java

generics

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.

like image 420
lilzz Avatar asked Dec 07 '11 21:12

lilzz


People also ask

What does <? Extends mean in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

What is class <? In Java?

A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

What does <? Super t mean in Java?

super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .

What does <? Super E mean?

super E> , it means "something in the super direction" as opposed to something in the extends direction.


1 Answers

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 } 
like image 133
Daniel Moses Avatar answered Oct 02 '22 00:10

Daniel Moses