wood burning stoves*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Examlab generics question" Watch "Examlab generics question" New topic
Author

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...


OCPJP(83%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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 GenericTest2.java
GenericTest2.java:4: 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
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3179
    
  49

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

Grr...
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.
Seema.

OCPJP 6
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
Greenhorn

Joined: Jan 23, 2011
Posts: 8

hello,
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
Greenhorn

Joined: Jan 29, 2014
Posts: 10
Hi,

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Examlab generics question