2

I saw a code in android that defines the following class:

android.widget.AdapterView<T extends android.widget.Adapter>

and also has the following interface: AdapterView.OnItemClickListener which contains the following method to be implemented:

onItemClick(AdapterView<?> parent, View view, int position, long id)

I can't understand how the first parameter works, if AdapterView was defined with constraint ( extneds...) How can it be a quiestion mark if it must be Adatper or something that derived from it?

Eitanos30
  • 1,331
  • 11
  • 19

1 Answers1

0

Class AdapterView<T extends android.widget.Adapter> requires that the concrete type used to create its instance extends Adapter and thus Adapter methods can be used inside.

Interface declaration (which actually doesn't have to be inside this class) accepts parameter AdapterView<?> where ? is so called wildcard. It means that type is unknown and can be anything.

For class it makes difference because its implementation will actually do something with type that is passed as generic parameter. You can define upper or lower bound on the type, but can't use unknown type in this case.

In case of interface - it is not a generic itself. Also, it's method is not generic, but accepts AdapterView of any type. Because of the wildcard any AdapterView can be passed to onItemLongClick. This is because AdapterView<A> is not a subclass of AdapterView<B>, even if A is a subclass of B. It is explained here (with example):

https://docs.oracle.com/javase/tutorial/java/generics/subtyping.html

You may want to read more about Java generics here: How do generics of generics work?

or/and here:

https://docs.oracle.com/javase/tutorial/java/generics/index.html

mlc
  • 415
  • 3
  • 7
  • thanks a lot, but why are you saying in isn't important in the interface. If the person who implement the interface will use the AdapaterView> method (for example the method getAdapter()), to which type of reference can the return value be assigned?. In what is it different from using getAdapter method inside the AdapterView class? – Eitanos30 May 07 '20 at 21:06
  • I edited my answer. It needed correction and I used a 'thought shortcut'. I hope it is a bit more clear now. – mlc May 07 '20 at 21:30
  • thanks i'm on my way to shift. I will read your answer later and approve it (i hope) after it. thanks!!! – Eitanos30 May 07 '20 at 21:45
  • isn't it more reasonable that instead of AdapaterView> it will be ApaterView extends Adaper> or AdapterView? because the Type inside the brakcets should be an Adapter that related to the ListView, GridView, etc? – Eitanos30 May 08 '20 at 15:54
  • 1
    Hi, sorry, was off the grid for some time. Unbounded wildcards (```>```) are reifiable types in Java (https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.6 - check both 4.7 and 4.6). In this case type parameter of AdapterView will be known at runtime as Adapter (check https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.5.1). To wrap it up - in this case ```?``` and ```? extends Adapter``` will be equivalent. – mlc May 19 '20 at 18:09