File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802


See code below

Case 1

Case 2

Case 3

In Case 3 you get will get error like following <E>add(java.util.List<E>) in Stub cannot be applied to (java.util.List<java.lang.Object>)
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.

I agree. Here's the link:
subject: E super ClassName - doesn't compile
jQuery in Action, 3rd edition