7 Replies - 798 Views - Last Post: 28 February 2010 - 12:07 AM Rate Topic: -----

#1 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

about overrided toString method

Posted 27 February 2010 - 09:58 PM

Hello everyone
well this is not my homework
so please don't think I am asking directly
I did a program of overriding the toString method
I forgot to call the method using the object
but still the desired result was obtained
what I am thinking is that how did the toString method got called without calling?
I tried some another similar method but it does not get called
is it because toString is an overrided method?
please help me
here is the code
class BobTest
{
	public static void main(String [] args)
	{
		Bob b = new Bob("GoBobGo", 19);
		System.out.println(B)/>;
	}
}

class Bob
{
	String nickName;
	int shoeSize;
	public Bob(String nickName, int shoeSize)
	{
		this.nickName = nickName;
		this.shoeSize = shoeSize;	
	}

	public String anotherMethod()
	{
  	 return ("This one simply prints something and the name " + nickName );
	}
	public String toString()
	{
	return ("My name is " + nickName + "and I wear shoes of size " + shoeSize);
	}
	

}

and this is the output

Quote

F:\Java\Concepts\GenericsAndCollections>java BobTest
My name is GoBobGoand I wear shoes of size 19

please help

Is This A Good Question/Topic? 0
  • +

Replies To: about overrided toString method

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: about overrided toString method

Posted 27 February 2010 - 10:07 PM

The System.out.println(), when passed an object, prints out that object's toString() method. Simple eh?
Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: about overrided toString method

Posted 27 February 2010 - 10:29 PM

I blame black magic, but if you want the correct answer, look at DogStoppers post.
Was This Post Helpful? 1
  • +
  • -

#4 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: about overrided toString method

Posted 27 February 2010 - 10:30 PM

well this is not true (I think so)
because see the following code
why did the jvm give something else in the following method?
class Test 
{
	public static void main(String[] args) 
	{
		Object obj = new Object();
		System.out.println(obj);
	}
}


Quote

F:\Java\Concepts\GenericsAndCollections>java Test
java.lang.Object@3e25a5

and why only toString got called in the previous problem?
and not anotherMethod?
please help
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: about overrided toString method

Posted 27 February 2010 - 10:32 PM

The output you got is called HashCode, and is what the Object toString() method returns. So if you don't override toString() in every class you make, they will all use Object's toString() method as all classes extend Object.
Was This Post Helpful? 1
  • +
  • -

#6 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: about overrided toString method

Posted 27 February 2010 - 10:38 PM

@macosxnerd101
sir does that mean that the methods which are overrided versions of built in method need not be called explicitly?
and I have one request sir,
you voted me down for a post which said "wait for 10 minutes, I am trying the problem"
I had done it successfully at the end
what was the problem?
did I commit a mistake?
sir please reply
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10650
  • View blog
  • Posts: 39,559
  • Joined: 27-December 08

Re: about overrided toString method

Posted 27 February 2010 - 10:47 PM

When you go to print an Object, it's toString() method is called implicitly. As for overriding methods in the super class, you do need to call those explicitly from the overriding method in the subclass if you want to include its functionality.

Also, if you want to discuss why I downvoted you in the other thread, PM me and I'll be happy to discuss it in more detail. However, I'm not a big fan of discussing these kinds of things for everyone to see.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: about overrided toString method

Posted 28 February 2010 - 12:07 AM

View Postpdkharkar, on 27 February 2010 - 11:38 PM, said:

@macosxnerd101
sir does that mean that the methods which are overrided versions of built in method need not be called explicitly?
and I have one request sir,
you voted me down for a post which said "wait for 10 minutes, I am trying the problem"
I had done it successfully at the end
what was the problem?
did I commit a mistake?
sir please reply

Which thread ?
Macosxnerd is not the type of guy to "vote down" a thread for unjustified reasons
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1