Two Laptop Bag
The moose likes Beginning Java and the fly likes Can't Get This toString Method Overridden. Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic

Can't Get This toString Method Overridden. Why?

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
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

Joined: May 08, 2001
Posts: 1932
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.
[This message has been edited by jason adam (edited December 19, 2001).]
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
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.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Can't Get This toString Method Overridden. Why?
It's not a secret anymore!