• 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

Generics and use with Vector

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a simple question...

this is a simple class implements a simple interface


this is a simple interface


this is the main

give me this error


who tells me because i get this error and how i can solve?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Francesco. Welcome to the Ranch!

You can't assign a Vector<PallinoNome> to a Vector<PincoPallino>. The problem is that if you did, you'd then be able to add instances of other classes that implement PincoPallino into a collection that was originally supposed to be a Vector<PallinoNome>. So you'd lose the type safety that generics are supposed to give you.

The general rule is, even if A is-a B, a GenericClass<A> is-not-a GenericClass<B>.

Your options to get round this are:

- Use either PincoPallino or PallinoNome as your generic type everywhere
- Make your method generic, and use a bounded type parameter
- Use a wildcard type parameter

Which is best depends on what you're trying to achieve, but the second and third options are more complicated. There's much more information about how they work in the Java tutorials at http://docs.oracle.com/javase/tutorial/java/generics/index.html

By the way, Vector is considered a legacy class nowadays. I'd strongly recommend using ArrayList instead.
 
Francesco Sisca
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i use this solution.

thanks a lot for suggest
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic