aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics using <? extends A>,<? super A> Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Generics using <? extends A>,<? super A> Problem" Watch "Generics using <? extends A>,<? super A> Problem" New topic
Author

Generics using <? extends A>,<? super A> Problem

mohitkumar gupta
Ranch Hand

Joined: May 18, 2010
Posts: 516

1. Compiles
---------------------------------------
2. Doesnot Compiles
-------------------------------------

3. Doesnot Compiles

------------------------------------------
4. Complies


-------------------------------
5.


---------------------------------------
Can anyone give me reason for each of the above code ?
whenever extends is used code complies but not when super is used
is there a reason for it ?

--------------------------



this code gives compiler error

q1 can refer to any queue of Number or its supertype
while q2 can refer to any queue of Integer or it subtype

then why the code gives compile time error


OCPJP 6.0 93 %
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 66

Hi Mohit,

The Point is whenever we are asking


you are saying the compiler create an ArrayList<Integer> (of type Interger) and set it to the List<? super Integer>( list which can store Interger and yes as well as its supertype) So as you should know that, the runtime doesnot care about the type safety it uptp the compiler and compiler make a reference who can store that all elements as an Object. Therefore get method would return an Object in this case So Compiles fine.

In other case you setting it to Integer without casting.

-------------------
view plaincopy to clipboardprint?
List<? extends Integer> list = new ArrayList<Integer>();
for (Integer element : list) {
System.out.println(element); //complies
}

I think the opposite reason (ie taking ref of type <? extends Integer>, fetching from that must be an Integer ) arises here

Please Confirm

Thank You



This message was edited 2 times. Last update was at by Sudhakar Sharma

mohitkumar gupta
Ranch Hand

Joined: May 18, 2010
Posts: 516

AS Sudhakar Sharma says:
you are saying the compiler create an ArrayList<Integer> (of type Interger) and set it to the List<? super Integer>( list which can store Interger and yes as well as its supertype) So as you should know that, the runtime doesnot care about the type safety it uptp the compiler and compiler make a reference who can store that all elements as an Object. Therefore get method would return an Object in this case So Compiles fine.

ok then


It doesnot compiles but what about this one,
why does it compile ?


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 2118

Think about what the compiler knows about the objects in the collection.

In your first example, the reference type is List<? super Integer>. It could refer to a List<Integer>, a List<Number> or a List<Object>. list.get(0) could be returning an Object, so assigning it to an Integer reference isn't safe.

In the second example, List<? extends Integer>, list could refer to a List<Integer> or a List<MySubclassOfInteger>*. So list.get(0) is guaranteed to return something that can be safely assigned to an Integer reference.

Note that if you're adding elements it works the other way around:



* At least, it could if Integer wasn't final! But that's not relevant here.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 66

Hi All,

Thats what i wanted to say thank you for clear explanation.

Thanks
 
 
subject: Generics using <? extends A>,<? super A> Problem
 
MyEclipse, The Clear Choice