• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java and Generics

 
T Mueller
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • 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).
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4495
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4567
8
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic