File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics not working, compiler still thinks its an Object... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics not working, compiler still thinks its an Object..." Watch "Generics not working, compiler still thinks its an Object..." New topic
Author

Generics not working, compiler still thinks its an Object...

Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
I got 2 classes, Reptile and Snake. Reptile has the walk method and Snake overrides it.

I'm having a compile error with this code

for(Reptile o : myList){
o.walk(); //////// THIS IS WHERE THE PROBLEM IS

This is the compile error msg:
--------------
java:34: cannot find symbol
symbol : method walk()
location: class java.lang.Object
o.walk();
--------------

Why the compile still thinks o is an object, even when it says explicitly "Reptile o : mylist" ?

Thank you. Leonardo.




Leonardo Crespo
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
Could you please post the complete code.

The definition:
public <Reptile, S extends Reptile> void listContent(List<Reptile> myList, List<S> myOtherList)

looks like its intended to be a generic method - how do you mention concrete classes? Again, I am guessing Reptile is a concrete class.

Please post the complete code that can be compiled.
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
The below code works! I am not sure if thats what you intended. I think, the error was in the definition of the generic method.



Hope that helps.
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
Here is the full code.


REPTILE CLASS




SNAKE CLASS



GENERICS CLASS (tester class)

[ August 02, 2005: Message edited by: Leonardo Crespo ]
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31
Prasanta, i see your point. Yes, your code worked just fine, thanks for answering.

I'm still wondering why my original code is not compiling... I posted the complete version, please let me know if you have any ideas.

Best Regards.
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
Well,

public <Reptile, S extends Reptile> void listContent(List<Reptile> myList, List<S> myOtherList)

Here the Reptile has nothing to do with your Reptile class!! The above declaration is just similar to:

public <R, S extends R> void listContent(List<R> myList, List<S> myOtherList)

So, here R and S are just 2 type parameters. Now considering erasure (or even without that), these 2 parameters could be anything say Integer, String or say ClassXYZ, ClassABC or whatever based on the where we are making the generic function call.

The walk() methos is only available to Reptile and its subclasses - by specifying:

public <R extends Reptile, S extends R> void listContent(List<R> myList, List<S> myOtherList)

we just say that.

----------------
"Towards 1.5 exam on Aug 4th"
Leonardo Crespo
Ranch Hand

Joined: Jul 03, 2005
Posts: 31


Here the Reptile has nothing to do with your Reptile class!! The above declaration is just similar to:

public <R, S extends R> void listContent(List<R> myList, List<S> myOtherList)



Ooouch, i got it now. Thanks alot Prasanta!
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by Prasanta Chinara:

public <Reptile, S extends Reptile> void listContent(List<Reptile> myList, List<S> myOtherList)

Here the Reptile has nothing to do with your Reptile class!! The above declaration is just similar to:

public <R, S extends R> void listContent(List<R> myList, List<S> myOtherList)


Could you further explain why <Reptile, S extends Reptile> is the same as <R, S extends R>? We have an upper bound, Reptile, at here, why it's not recognized?

Thanks


SCJA, SCJP5.0, SCBCD, SCWCD
Prasanta Chinara
Ranch Hand

Joined: Jul 20, 2005
Posts: 30
An upper or lower bound in a generic method is declared as:

S super LowerBoundClass
or
S extends UpperBoundClass
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics not working, compiler still thinks its an Object...