Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Method Call

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody please explain this one in detail please.


I don't understand the syntax in bold. How does it know to call the toString(). If you have more than one method will "new SubClass();" call them all. Can someone pls explain how this is working.

(Marilyn added code tags)
[This message has been edited by Marilyn deQueiroz (edited November 04, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The System.out.println() method is overloaded to accept Objects as well as Strings (and various primitives) and can therefore deal with this new SubClass() object. See this API page for more details.

What did you see when you ran this program?

new SubClass() will create an instance of SubClass. It does not call any methods.
[This message has been edited by Marilyn deQueiroz (edited November 04, 2001).]
 
Ranch Hand
Posts: 151
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println( new SubClass() );
in the above statement,new SubClass()statement is creating an object of the class SubClass.when an object is passed in print or println staements as an argument,toString() method of that class is called implicitly.
Here System.out.println( new SubClass() );
is equivalent to System.out.println( new SubClass().toString() );
which calls the toString() of SubClass which in turn calls toString() method of Super through super.toString() call.
The output is "43"
HTH
rajashree.
 
Pervez Amin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Rajashree,
I undersand now!!
 
This tiny ad is suggesting that maybe she should go play in traffic.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic