• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generics question

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make BaseClass generic:
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic