Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in interface

 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output is : test

Can anyone explain me the what is happening in these lines

 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivang sarawagi wrote:Can anyone explain me the what is happening in these lines

Welcome to anonymous classes.

When you have problems like this, it's usually good to work from the inside out. In your particular case, the "inside" is:and what that does its to create an instance of an 'anonymous' (ie, unnamed) class that implements TestA. In order to do that, it has to implement its toString() method, which in this case returns "test".

Moving out, you have System.out.println(), and if you look at its documentation, you'll see that it calls String.valueOf(), which in turn calls the object's toString() method.

And voilĂ : your program prints out "test".

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic