Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding toString()

 
harsha balluru
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like this MapTest class ever hold Tom,Steve & Kelly only ?
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.

What's the question?
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works on with those variables that uses with arraylist...

Regards,
Jon
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic