jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes E super ClassName - doesn't compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "E super ClassName - doesn Watch "E super ClassName - doesn New topic
Author

E super ClassName - doesn't compile

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Based on an explanation from K&B Master Exam:

<E super CharSequence> makes no sense - super could be used in conjunction with a wildcard but not a type variable like E

I'm wondering, why?

Why doesn't the following work (it doesn't compile, but the compilation error doesn't explain anything):


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

@Nidhi

See code below

Case 1



Case 2



Case 3



In Case 3 you get will get error like following

Stub.java:5: <E>add(java.util.List<E>) in Stub cannot be applied to (java.util.List<java.lang.Object>)
add(l);
^
1 error


Case 1 works but Case 3 does not work because in Case 1 E should be of type CharSequence and String implements CharSequence so condition satisfies with compile time type checking
and in Case 3 list is holding objects of type Object which do not implements CharSequence.

Following is quote from GenericsFAQ
Refer this

A wildcard can have only one bound, either a lower or an upper bound. A list of wildcard bounds is not permitted.
A type parameter, in constrast, can have several bounds, but there is no such thing as a lower bound for a type parameter like E super CharSequence


So you cannot use <E super CharSequence> but you can use <E extends CharSequence> syntax.

Case 4



SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Hi Ninad,

Thanks for taking the time to answer my question. The link that you gave, has a great answer for this exact question.

Nidhi
 
Consider Paul's rocket mass heater.
 
subject: E super ClassName - doesn't compile