• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing interface with list of generics

 
Fedor Smirnoff
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am experimenting with generics and running into a problem where I cannot set a List<? extends Object1>
I have the following:


My IDE is complaining that:

Name clash: The method setItems(List<OrderItemImp>) of type OrderImp has the same erasure as setItems(List<? extends OrderItem>) of type Order but does not override it

So I am guessing its not possible to set a list in that way? Any ideas on how implement this solution?

Thanks, Fedor
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fedor Smirnoff wrote:
Name clash: The method setItems(List<OrderItemImp>) of type OrderImp has the same erasure as setItems(List<? extends OrderItem>) of type Order but does not override it

So I am guessing its not possible to set a list in that way? Any ideas on how implement this solution?


While what you are pointing out (or your IDE is pointing out) is indeed an error, it isn't really the first issue to worry about. The issue to worry about is that you failed to implement the Order interface.

The Order interface requires you to implement this...



This is a setItems() method that can take a list of a type that is of OrderItem, or an interface that extends the OrderItem interface. This means that I could implement a class, named Henry, that implements OrderItem. And be able to pass an object (that is a List<Henry>) to the method.

Your implementation...



Won't accept a List of Henrys. This means that you didn't implement the setItems() method as required by the interface.


This is also the indirect cause of the name clash. The compiler is assuming the incorrectly implemented method is simply another method. So, your method, will clash with the correctly implemented method version.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic