• 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

Java and Generics

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have some code similar to the one below:

And I am having trouble with the Generics. In the method named "method()" (Line #41), I get warnings if I don't use Generics, and an error if I use wildcard ("?").

Could someone please guide me, on how to use Generics in the right way for my situation, without getting a Warning or an Error ?



Thanks.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering these lines:

The problem here is that, with the way you've parameterized it, i could be of type Interface1<Type1>, Interface1<Type2>, or Interface1<SomeOtherSubtypeOfType>.

Then you get to the second line...and you're trying to put a Type1 in it. The compiler can't allow that - what if it's an Interface<Type2>? Generics only work at compile time, not at runtime, so the compiler will prevent anything it can't be sure about.

There is a mechanism for using wildcard types and still being able to add objects. If you had i declared as type Interface1<? super Type>, then it would be different. That way the compiler could be certain that it would accept a Type (or Type1, Type2 etc.) as an argument, and it would compile.

(Of course, that's not consistent with declaring Interface1<T extends Type> in the first place - so if this was a real situation there's something wrong with the design somewhere).
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Java Newbee X", please check your private messages regarding an important administrative matter.
 
T Mueller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew,

Thanks for the prompt reply.

I was under the impression that the following statement

should allow Interface1<any class that extends Type>. Of-course it should not allow Type1 if it was defined as:
But obviously, I am wrong.

And definitely my design is wrong here. But I don't know how to fix it:

using "? super Type" doesn't seem to help, since I then have error in line 34 & 35.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T Mueller wrote:I was under the impression that the following statement

should allow Interface1<any class that extends Type>


Yes, that's correct.

But that means that i could actually be any of the following:
1. Interface1<Type>
2. Interface1<Type1>
3. Interface1<Type2>
4. etc

The ? is just a wildcard. The actual object that i references is not an Interface1<? extends Type>, it's going to be an Interface1<SomeUnknownClassThatExtendsType>.

In the third case, i.put(new Type1()) is clearly an error. But since the compiler doesn't know which of those cases it is, it can't allow any of them. The key, I think, to understanding generics is to ask yourself "what does the compiler know about this reference?".

I'm afraid without knowing what you're trying to achieve it's difficult for me to show you what the code should actually be.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic