• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics question

 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my current project, I'm trying to do something like this:



Each ExtendedClass has its own ExtendedType class. Each ExtendedClass so far has its own method getTypes() which returns a Set<ExtendedType> which is then used further. Now what I want to do is refactor this so that getTypes() is in BaseClass. However I don't think there's a way to create a correctly typed set in the BaseClass and return it (or is there?), so I want to pass the set to the setTypes() method instead which should fill it then.

But when compiling, the above code produces the error "setTypes(Set<? super BaseType>) in BaseClass cannot be applied to (Set<ExtendedType>)". I'm not sure what I'm doing wrong here...any help appreciated.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling setTypes(Set<ExtendedType>)

That doesn't work because ExtendedType is not a super-class of BaseType, it's a subclass. It would work if you used ? extends BaseType (but of course you wouldn't be able to add to the set then).
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make BaseClass generic:
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers, going to give the above suggestion a try! Not too experienced with writing such generic classes but I guess it makes sense here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic