File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Murach's Java Servlets and JSP this week in the Servlets 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: 1659
    
  11

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: 1659
    
  11

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: 18509
    
  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: 1659
    
  11

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