File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Leonardo Crespo
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Prasanta Chinara
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prasanta Chinara
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic