This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generics and use with Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics and use with Vector" Watch "Generics and use with Vector" New topic
Author

Generics and use with Vector

Francesco Sisca
Greenhorn

Joined: Dec 13, 2012
Posts: 2
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?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

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

Joined: Dec 13, 2012
Posts: 2
i use this solution.

thanks a lot for suggest
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics and use with Vector
 
Similar Threads
singleton?
My first interface
an abstract class implementing an interface
Switch between class implementation
Can I override an interface method in the class ?