| Author |
Proper polymorphic syntax...
|
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1611
|
|
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: 1611
|
|
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: 16695
|
|
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: 1611
|
|
|
Thanks Henry!
|
 |
 |
|
|
subject: Proper polymorphic syntax...
|
|
|