This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt in interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in interface" Watch "doubt in interface" New topic
Author

doubt in interface

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147


Output is : test

Can anyone explain me the what is happening in these lines

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: doubt in interface
 
Similar Threads
Regarding interface
constructor question
Interface accepting instance
toString is overrides Object Class or TestA
Calling interface ?