• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Is there any difference between these two generic methods?

 
Ranch Hand
Posts: 209
13
VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me that dogsBark1 and dogsBark2 are the same in that they both only accept as their parameter a List<Dog> or List<Poodle>.


Output:

However dogsBark2 has the added advantage that I might, within its body,  make some use of T.

So, are these two different syntax styles in fact expressing exactly the same thing? If so, why would one ever choose to use the List<? extends Dog> style when the other alternative offers the possibility of using T in some way?
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, of the two methods, only the second method is generic method. The first method is just a regular method that takes a generic. It is a subtle difference, but it is different...


Anyway, a bounded wildcard, is a restriction on a wildcard. And a wildcard is an unspecified generic... but using it doesn't make the method a generic method. And yes, you can force the two features to behave similarly... but doing so can get really complicated.

For example, how would you do this with generic methods? ...



Henry
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Hayward wrote:
However dogsBark2 has the added advantage that I might, within its body,  make some use of T.



On the other hand, dogsBark1 has the added advantage that the method can take a wildcard -- while dogsBark2 won't compile as it needs to be able to determine T. This allows you to write more flexible code, to support logic where the generic type is not known.

Note: in thinking about this response some more, it kinda makes my previous response moot. The complexity argument isn't as strong as simply pointing out what can't be done.

Henry
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, food for thought.

Henry Wong wrote:
For example, how would you do this with generic methods? ...


I thought that would be easy:


Although my attempt at a generic version of the method, dogsBark2, does compile, attempting to call it with arguments that are not exclusively all List<Dog> or all List<Poodle> won't compile.

Reading through the generics chapter in k&b, it may be that I understand it sufficiently to scrape through the exam. However, I currently have the feeling that the more I delve into the detail of this topic the more potential for confusion I come across

 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Hayward wrote:
Although my attempt at a generic version of the method, dogsBark2, does compile, attempting to call it with arguments that are exclusively all List<Dog> or all List<Poodle> won't compile.



The reason it doesn't compile is because the compiler can't find a T that satisfies the calling parameters.  As you know, Poodle IS-A Dog, so, a Poodle instance can be used in place of a Dog instance. However, you are not passing Poodle and Dog instances; you are passing List<Poodle> and List<Dog> instances... and there isn't a IS-A relationship between List<Poodle> and List<Dog>.

Technically, you can handle the two parameter case via two generics ... like so ...



... but this adds complexity, and it only handles the two parameter case. You will need to have overloaded methods for the other cases. And since, I used a variable arity method, you will theoretically need an infinite number of overloaded methods with an infinite number of generics ...  

Henry

PS... I gave you a cow for your investigations. Good Job.
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks for your help Henry.

btw I've corrected a typo I made:

'Although my attempt at a generic version of the method, dogsBark2, does compile, attempting to call it with arguments that are NOT exclusively all List<Dog> or all List<Poodle> won't compile. '

although you obviously replied to what it was I meant to say.
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
you are passing List<Poodle> and List<Dog> instances... and there isn't a IS-A relationship between List<Poodle> and List<Dog>.



If that is the case then why does dogsBark2(poodles, poodles); work ?

List<Poodel>, List<Poodle> is not List<Dog>, List<Dog>

or am I missing something ??

Any explanation will be really helpful.

TIA
 
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which version, Henry or Richard's?
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:In which version, Henry or Richard's?



Well, we can answer for both...

For Richard's version, T resolves to Poodle. For my version, both T and S resolves to Poodle.  And neither of our versions depend on the parameter being of type List<Dog>.

Henry
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic