File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java 1.5 Generics, i need some help please!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java 1.5 Generics, i need some help please!!" Watch "Java 1.5 Generics, i need some help please!!" New topic
Author

Java 1.5 Generics, i need some help please!!

Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
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


SCJP, SCJD, SCWCD, SCBCD (JEE)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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

Joined: Feb 19, 2004
Posts: 53
Thanks a lot, i didn`t even see that!!

Derick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 1.5 Generics, i need some help please!!
 
Similar Threads
Generics
Generics
Question with generics
generics problem
Heterogeneous map and generics