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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 1613
    
  10

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: 1613
    
  10

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: 18108
    
  39

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: 1613
    
  10

Thanks Henry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper polymorphic syntax...
 
Similar Threads
XSLT : appending node number
SAX parsing question
sending/retrieving data via SQL statements...
Inheritance in Spring
Polymorphics and Generics.... how to do specific things to a subclass