• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 1.5 Generics, i need some help please!!

 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I`m struggling with some generics in my classes, mabe someone here can help me out.

Why can you only overight a method that implements an interface like this :


in the following way :



My problem is here :
public T get(GenericHibernateReadDAO genericHibernateReadDAO)

As soon as you change it to
public T get(GenericHibernateReadDAO<T> genericHibernateReadDAO)

it sezz that you are not overiding the method??

why is that???
Shouldn`t it actually be <T> in the method??

Thanks
Derick
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derick,
This occurs because MyClass<T> is not a subclass of MyClass. Adding the <T> changes the type of the object, which means you are no longer overriding the method. Sun describes these relationships some more.
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help...please can you just explain it a bit more

In the interface i`m specifying that the method should take in
GenericReadCommandInterface<T>

So why cant i specify that in the implementing class`s method??

i Understand that if you specify GenericReadCommandInterface<Object>
you cant use GenericReadCommandInterface<String>

but seeing that i`m passing the object into the class as <T> i cant
see why it is`nt possible to specify <T> in the method.

At this point the Generic type is <T> right through my class so it isn`t
changing the type at all.

Sorry mabe i`m just missing i vital part.

Thanks
Derick
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derick,
Oops! I answered the wrong question. My explanation in the above post was for why GenericReadCommandInterface<Object> and GenericReadCommandInterface<String> aren't subclasses.

Now back to your real question.

Note the difference between the two code snippets here. The second one is the one you want. This says you are implementing an interface that deals with generics. You accidently typed the first one, which would mean that you want to get rid of the type parameter. Therefore in the get method, there is no type parameter.
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, i didn`t even see that!!

Derick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic