• Post Reply Bookmark Topic Watch Topic
  • New Topic

generics and inner classes  RSS feed

 
Jim Lang
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ranchers,

I've been a diehard Java 1.4 programmer for a fair while, but I'm trying to catch up now - and I've got a problem with using generics and inner classes together.

Consider the following simplified example:

The message the compiler spits out is:

I can see that the generic identifier 'E' used by class C masks the generic identifier 'E' used by class A, but I can't see a way round this - even if I change the generic identifier 'E' used by class C to some other identifier the compiler still complains.

Can anyone shed some light on how I can get around this problem, but still use inner classes and generics

Many thanks,
Jim

(edited because I was mixing up the term varargs with the term generics, so fixed it)
[ March 23, 2007: Message edited by: Jim Lang ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the type parameter you use for class A, and the type parameter you use for class C are different even though you've used the same symbol.

Try using another symbol for the parameter of C, and you can see where the problem is.
 
Jim Lang
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help - I hadn't tried just changing the one identifier attached to class C. However, the problem now that I'm trying to get my head around is that when you use a different identifer for the generic identifier in class C, and leave the rest of the class as it was, I can't see how you can then instantiate C while enforcing the type-safety, since the type you now pass to C is never used anywhere within C.

Or is that kind of redundancy ok, since I'm implementing it as an inner class?
[ March 23, 2007: Message edited by: Jim Lang ]
 
Jim Lang
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use this listing as an example it should illustrate what I meant in my last post:

What bugs me is providing '<X>' to class C when it's never even used. Or can I omit it because it's never used (as in above listing)? Is there anythign wrong with this approach from a type-safety point of view (I don't think so, but then I'm only getting to know generics)

Honestly, Java becomes more like C++ with every new release.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason you feel you need to declare a type parameter for C?
 
Jim Lang
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a simplified version of another set of classes - and in the larger set of classes C is a class that implements the Iterator interface. It's ok to have 'class C<X> implements Iterator<E>' and the compiler is also happy for me to omit the '<X>' - which seems to be sufficient from the point of view of satisfying the Iterator type-safety part, and for the class to rely on the generic identifiers from the outer class.

Then there's the purist in me that thinks that I should always be enforcing the type of C in any case. On the other hand, it doesn't matter so much because that's a private inner class that can only be instantiated by the outer class, so as long as I make sure that the outer class doesn't abuse the lack of type checking in the inner class it's all hunky-dory.

Perhaps I'm just being upended by feeling the need to add generics everywhere because it's a new feature (to me at least). The most important thing for me is getting used to when I should use these new features, and when I shouldn't. Perhaps in this case, as you sensibly suggest for class C, I just shouldn't declare the type parameter.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!