wood burning stoves 2.0*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

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

Joined: May 25, 2001
Posts: 388
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

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: http://aspose.com/file-tools
 
subject: Can't Get This toString Method Overridden. Why?
 
Similar Threads
Resolving relative URLs in HttURLConnection response message
Cannot find symbol error
i need some other logic
Best way to handle huge write operation
How to right -adjusted a field