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 Examlab generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Examlab generics question" Watch "Examlab generics question" New topic

Examlab generics question

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

1 and 2 this both should not work because K can be anything here....

can anyone please explain??? because there is no explanation is given in examlab regarding this too...

Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

you need to look at it differently. K cannot be anything when the method is called. In fact K will be resolved based upon the method call itself. Lets take a simple example
Here too M can be anything, but since we can't instantiate class Abc with anything except for Number or its sub-types, we know that M will actually resolve to Number or its sub-type.

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

Ankit....!!! I code what you said ..but it is not working ....

D:\java\scjp\SCJPEXAM\Collections\GenericsX>javac type parameter T is not within its bound
public <T> void doSomething(Gen1<T> x)
1 error

this error comes when i try to compile this code...
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

Hmm you are right it doesn't work, I forgot that M/T can be resolved without relying on the parameters passed to the method. Like if there is a method like this
Now T can resolve depending upon the object passed to the method and also I can explicitly specify the type of T
So in this case even though I passed null to the method, I explicitly told the compiler during the method call that T is String. This is why the example I gave you doesn't work as T in that case can be explicitly set to a value which is out of bounds for class Gen1. I'm not going to lie to you, but I'm also confused why those 2 statements compile fine (maybe its due to sleep deprivation), its weird behavior...
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3930

ankur trapasiya wrote:because there is no explanation is given in examlab regarding this too...

Ankur, can you let me know the question number of this?

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
Ankur, I am having a difficult time understanding Generics as well. I will give my best shot at explaining this:

1 should never compile anyway because ABS<? super Number> means ABS<Object> is valid, which is untrue. So even if K is Number or its subtype, this method is not applicable.
Similarly Line 3 will never compile because K can be anything and it has to be K extends Number. Hence it is not allowed.

But, one conflict between 1,2 & 3 is, what happens when you say

Now method on Line 1 for SubClassABS can be interpreted as

Line 2 can be

Line 3 can be

However your a in a.useMe(a)(Line 4) matches ABS<? super Number>(Line 1) as well as ABS<? extends Number>(Line 2) as well as ABS<Number>(Line 3)
So all 3 methods are applicable. The compiler will not know which one to choose at runtime. Now if you comment out 1 and 3, 2 will compile without any issue. 2 compiles because there is a chance that <K> defined in the method is Number.

Experts please comment.

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

Too much confused in generic methods ...
Thanks all for reply....

@Devaka : question number is 57 from Diagnostic exam...
Adrabi Abderrahim

Joined: Jan 23, 2011
Posts: 8

very nice explanation,

I try my version ,
In other word is easy to check it, you can in first just remove all generics and compare methods signatures, euh how?

first you've :

after removing generics:

now what, you see? can be compiled? no, some signature!
another example:
Rageni Kalra

Joined: Jan 29, 2014
Posts: 10

Did anyone finally found a good explanation for this question? I am preparing for ocjp6 and I cannot understand the difference as well.

thanks in advance.
I agree. Here's the link:
subject: Examlab generics question
It's not a secret anymore!