aspose file tools*
The moose likes Beginning Java and the fly likes Proper polymorphic syntax... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Proper polymorphic syntax..." Watch "Proper polymorphic syntax..." New topic
Author

Proper polymorphic syntax...

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1688
    
  12

I would like to call the overridden toString() method in the subclasses of Student instead of the Student.toString(). I have an ArrayList of <? extends Student>.



I haven't been able to try it because right now I'm fighting with the database that the application ties into.... My gut says it will work, but it looks funny.

I guess really the ultimate question is..... can I use that <? extends whatever> ANYWHERE I would declare what goes into or out of something??

When you do things right, people won't be sure you've done anything at all.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

Actually, you shouldn't need to call toString() directly at all. The println() method already calls toString() for arbitrary classes.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1688
    
  12

Thanks, John.

So I don't need to specify the toString()....

But the <? extends Student> will make sure the subclasses' toString() method is accessed?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Janeice DelVecchio wrote:
I guess really the ultimate question is..... can I use that <? extends whatever> ANYWHERE I would declare what goes into or out of something??


No. Wildcards, bounded or otherwise, are only used for generics. You can't actually declare such a variable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1688
    
  12

Thanks Henry!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Proper polymorphic syntax...