• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing interface with list of generics

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
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

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic