• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with generics  RSS feed

 
Faton alia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody.
I'm stuck with this problem which I think is due to my too limited knowledge of generics. In line 9 in class Test i get an compilation error : Type mismatch: cannot convert from Object to Integer.
Does anybody know if there is a solution with generics?






 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listener's parameter is a raw type, and doesn't know any generic types anymore. Likewise for the declaration of your listener variable.

With two little changes I could get it to compile:
The first change makes the parameter generic, based on the existing generic type "I". The second declares the listener to use "Integer" for "I". That means that the parameter now has Integer for its generic type, and innerClass.someMethod() returns an Integer.
 
Faton alia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob, it's indeed what I stuck with.
In the example below, the interface is not generic anymore.



In the main method below, I use two instance of OuterClass, o1 parameterized with <Integer>, and o2 with <String>.
My annonymous inner class myListener tries to use the generic type of the outer class (E).
The code as it is below does not compile (Integer i = innerClass.someMethod() - Type mismatch: cannot convert from Object to Integer).



I would like to express that myListener is for o1, and should use E = Integer, without repeating it (without repeating <Integer>, I already say it when declaring o1). Is that possible?

Many thanks!
Faton.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!