Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Proper polymorphic syntax...

 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20822
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic