• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't Get This toString Method Overridden. Why?

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get the default toString method overridden by my version. However, it's not even getting called by main. Why?
Do all overridding methods have to be public? I can't make it public since the driving class containing main (is in the same file) and must be public. Any help would be deeply appreciated. Here's the code:


[This message has been edited by Cindy Glass (edited December 19, 2001).]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding methods do not have to be public, but they can not be more restrictive than the method they are overriding. In other words, if the method is public, you can't override it making it protected, default, or private.
In regards to your program, your output should look something like Str in ButtonTest = ButtonTest@17d275 This is correct for the program you have written. ButtonTest is not overriding toString(), your Stack class is. Since you never create an object of Stack, that toString() method is not called. Since ButtonTest doesn't override toString(), Object's implementation is called, which simply returns the hashcode of that object.
Jason
[This message has been edited by jason adam (edited December 19, 2001).]
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
are you overriding the method toString() of ButtonTest ? as i see it, you only override it in the class Stack. but you only construct a ButtonTest, no Stack.
you ned either construct a Stack object or override the toString() method in the ButtonTest class.
karl
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know - naming one of YOUR classes "Stack" is not a good idea because there is a class named Stack in the API - and all sorts of stuff could start breaking if you try this in real life.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic