aspose file tools*
The moose likes Java in General and the fly likes Implementing interface with list of generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implementing interface with list of generics" Watch "Implementing interface with list of generics" New topic
Author

Implementing interface with list of generics

Fedor Smirnoff
Ranch Hand

Joined: Apr 21, 2003
Posts: 45
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
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing interface with list of generics