• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generic method invocation

 
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble understanding the compiler error in the generic method invocation on line 17 of the code below. Could someone explain the rules being followed here ?
Thanks,

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In generics, the generic type T must match the method declaration. So for your generic declaration Collection<T>, you can pass:
- Collection<T>
- SubtypeCollection<T>


But...
- Collection<SubtypeT> // won't compile
- SubtypeCollection<SubtypeT> // won't compile


On the other hand for arrays, you can pass an object of a subtype. For your array declaration T[], you can pass:
- T[]
- SubtypeT[]


For the code to compile properly, you have to modify the Collection declaration to accept T or its subtype like this:

 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Faraz,

I am still a bit unclear on how the compiler figures out what the T is in each of the method calls.
I thought that it would just look from left to right and infer T to be the type of the 1st parameter in the method call. However, this seems incorrect as on the 1st call, it infers T to the type Object which was the type of the 2nd parameter.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Faraz,

If i change the second method like



its compiles fine.I think its conflict with your statement,

In generics, the generic type T must match the method declaration. So for your generic declaration Collection<T>, you can pass:
- Collection<T>
- SubtypeCollection<T>

But...
- Collection<SubtypeT> // won't compile
- SubtypeCollection<SubtypeT> // won't compile

On the other hand for arrays, you can pass an object of a subtype. For your array declaration T[], you can pass:
- T[]
- SubtypeT[]

For the code to compile properly, you have to modify the Collection declaration to accept T or its subtype like this:




 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does bring up an interesting "sub-topic"... When there is a generic method, using a type <T>, which takes 2 different parameters of the generic type in question - how is <T> ascertained exactly?

Kind of hard to put into words, but, consider this shortened version of the original post:



As stated this compiles fine. Now, if we examine it, we have to come to the conclusion that for this specific invocation of the method, the generic type T has to be set to Object, and the reason the String[] argument still applies is because it is "widened" up to an Object[]. (If T had been set to String here, the method call would cause a compiler error, since the Collection<Object> wouldn't match a Collection<String>).

So I reiterate my question, is there a specific way that <T> is ascertained? Or is it just so to speak "by the means available"?

I think this question is easily "tripped on" during a real test, since one might be lured into thinking that for this method-call, the <T> of the method would be set to String, simply cus it's the first parameter appearing in the signature, and hence would generate a compiler error. If one doesn't take the time to consider different "variations" of the arguments, I'd say it's pretty easy to miss that this does in fact compile.

// Andreas
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andreas,
You have read my mind ! This is exactly the question I was trying to ask but I could not have put it better than you have. I am also pretty sure that I will not be able to spot this in the real exam unless I know exactly how <T> is ascertained.
Hope we can get a good answer now
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muneeswaran Balasubramanian wrote:Hi Faraz,

If i change the second method like



its compiles fine.I think its conflict with your statement,



Hi Muneeswaran,

The call in your post works because the generic T in this case is of type String.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,
this is my PERSONAL analysis, and I guess it is coherent;
when you have such a situation java chooses the 'MOST-GENERIC' of the
types passed into the method as a substitute, lets take a look at the very
first program in this thread, this time with a little modification:



As for now this is effective...I Hope So.
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect !. Easy to understand, easy to remember.
Thanks Ikpefua.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Andreas,
You are excelently correct, the 'String[]' is "widened" to 'Object[]' during invocation.
Java simply does NOT 'implicitly' allow "widening" in collections, you must tell it to with this syntax:

so if you modify the genericMethod() like this:

The code compiles without problems
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:@Andreas,
You are excelently correct, the 'String[]' is "widened" to 'Object[]' during invocation.
Java simply does NOT 'implicitly' allow "widening" in collections, you must tell it to with this syntax:

so if you modify the genericMethod() like this:

The code compiles without problems



Yes, I am aware of the wildcards and agree with you Jacob, cheers.

Asad wrote:Andreas,
You have read my mind ! This is exactly the question I was trying to ask but I could not have put it better than you have. I am also pretty sure that I will not be able to spot this in the real exam unless I know exactly how <T> is ascertained.
Hope we can get a good answer now



I am afraid we're pretty much "on our own" though, I think we just gotta pay attention and think carefully during the test, since the basic knowledge required to pass this type of question only requires that we are aware of widening method-arguments, and the general workings of generic methods. We just have to make sure we think twice (or three times ;) on these questions.

What's good though is that you brought it to attention, we'll all be better prepared for it now

// Andreas
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know about the real exams, your knowledge will NOT be tested on a serious-brainer-widening-erasure-generics-methods, you are expected to know whats legal and NOT legal about generic type declarations, basic method arguments, return types etc.
The exam creators know that generics itself it too dificult to mix up other with logics. However it is absolutely 'beneficial' to have learnt this today.

Cheers!

Ikpefua.
 
I don't even know how to spell CIA. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic