• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics  RSS feed

 
Padmanabh Sahasrabudhe
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the program I am trying while trying an example from java.net explorations. I am not able to compile the code. Is the code not correct? Aren't message(Collection<? extends Serializable> animals) and message(Collection<Animal> animals) two different methods? The error I get is:

InheritanceTester.java:15: name clash: message(java.util.Collection<? extends ja
va.io.Serializable> and message(java.util.Collection<Animal> have the same era
sure
private static void message(Collection<? extends Serializable> animals) {
^
InheritanceTester.java:19: name clash: message(java.util.Collection<Animal> and
message(java.util.Collection<? extends java.io.Serializable> have the same era
sure
private static void message(Collection<Animal> animals) {
^
2 errors


 
Nevin kumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
As far as my understanding is concerned,compiler looks only at the base type rather than parameterizesd type and in our case as the base type is same that is Collection for both the methods so the compiler is complaining as duplicate methods.
And one more thing I want to mention about parameterized type is we specify parameterized type as a type safety,so that the corresponding collection holds only that type of objects.so in this case base type is same and parameterized type is different but the compiler is concerned only about base type so it complains.In order to resolve this change the base type.
 
Padmanabh Sahasrabudhe
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Naveen. So Collection<TypeT> as a parameter T and Collection<?> as a parameter may be different from a programmer point of view but are same to the compiler? Am I right? makes sense. thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!