• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Examlab generics question

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 6600
1253
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too much confused in generic methods ...
Thanks all for reply....

@Devaka : question number is 57 from Diagnostic exam...
 
Greenhorn
Posts: 8
Eclipse IDE Opera Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic