• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding toString()

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";

}

}
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like this MapTest class ever hold Tom,Steve & Kelly only ?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags.

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

Regards,
Jon
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic