wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Overriding toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding toString()" Watch "Overriding toString()" New topic
Author

Overriding toString()

harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
I am curious why the following code does not honor my override of toString() method. Any ideas???




//Class to test the override of toString method
import java.util.*;
public class MapTest extends HashMap {

public static void main(String args[]){
HashMap<String, Integer> mps = new HashMap<String, Integer>();
mps.put("Tom",new Integer(26));
mps.put("Steve",new Integer(33));
mps.put("Kelly",new Integer(27));
System.out.println(mps);
}

public String toString(){

return "Tom is 26, Steve is 33, Kelly is 27";

}

}
harsha balluru
Greenhorn

Joined: Feb 06, 2009
Posts: 14
Sorry.... My mistake...I realized that I invoked an object of SuperClass type. But what if I change the statement like this:

import java.util.*;
public class MapTest extends HashMap {

public static void main(String args[]){
HashMap<String, Integer> mps = new MapTest<String, Integer>();
mps.put("Tom",new Integer(26));
mps.put("Steve",new Integer(33));
mps.put("Kelly",new Integer(27));
System.out.println(mps);
}

public String toString(){

return "Tom is 26, Steve is 33, Kelly is 27";

}

}
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

Looks like this MapTest class ever hold Tom,Steve & Kelly only ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Hi,

Actually you need to do something like this..



but i dun see any variables declared.. maybe this might help and using google.com? I did not have any experience using hashmap so can't help much..

Regards,
Jon
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
Did your code compile?
I am having problems compiling the code. It says "Type MapTest doesn't take parameters"!.

Anyway, I guess, to test the override of toString method, you have to call the toString() method actually.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.

What's the question?
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
it works on with those variables that uses with arraylist...

Regards,
Jon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Agree with David Newton; you haven't actually told us what the problem is.

Your class has an overridden toString method which appears to work. You ought to declare it as . . . classMapTest<K, V> . . . but your class seems not to fulfill the test of "a MapTest IS-A HashMap". And as Vijitha Kumara said, the toString method will be wrong whenever one of your friends has a birthday!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding toString()