954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with overriding methods

hi everyone

System.out.println(currentPlayer);

it returns to me like this:Player@7cbde6

i know it was caused by toString() and it is suggested to ues overriding method
but i don't know how to do it, so can somebody teach me how to use overiding method so System.out.println(currentPlayer); will display what i want

thank you very very much

drunkpanda

drunkpanda
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

What's the signature of toString?
All you need to do is provide a method with that exact signature in your own class.

By reading the API docs for java.lang.Object you can find out the signature of toString.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

thank you Jwenting

drunkpanda
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

hellow,
all u hv to do is that override toString() method as one friend also told it, its signature is

public String toString(){
String str;
str = "any thing you want do it here, and place this method in ur class";
return str;
}

khoomy
Newbie Poster
4 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

Hi khoomy thanks

I have actually done something like this which works ok but what happen if i get mutiple String want to display each individually

public String toString() 
     {
        String thePlayer = currentPlayer.getName();    
        return thePlayer;
    }


for example:
System.out.println(toString()); //which display player's name
System.out.println(somthing else);

Drunkpanda

drunkpanda
Newbie Poster
5 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

Where do you get mutiple Strings ? Not clear with your example.

aj.wh.ca
Junior Poster in Training
53 posts since Mar 2005
Reputation Points: 12
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You