This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any difference between these two generic methods?  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 170
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 170
11
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 170
11
  • Mark post as helpful
  • send pies
  • 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.
 
R K Singh
Ranch Hand
Posts: 5390
1
Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Stephan van Hulst
Saloon Keeper
Posts: 7501
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which version, Henry or Richard's?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!