• Post Reply Bookmark Topic Watch Topic
  • New Topic

argument Vector of type derived_class not acceptable as Vector of type parent_class  RSS feed

 
harold wang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was a little surprised to find out that I could not pass an argument of type Vector<derived_class> to method whose signature is void myMethod(Vector<parent_class>) where derived_class extends parent_class.

Since derived_class is technically also an instance of parent_class, I thought this would be ok. Perhaps it just requires a cast, but I couldn't figure out how the syntax. For example, calling 'myMethod((Vector<parent_class>) myArg);' where myArg is type Vector<derived_class> didn't work. I had to create a whole new copy of the vector, casting each element to parent_class to get it to work, but this is really ugly. Any suggestions?

Thanks, HW
[ November 17, 2008: Message edited by: harold wang ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a complicated field, this generics. You will have to go through the Java� Tutorials; look particularly at "bounded types" and "subtypes."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!