aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling interface ?" Watch "Calling interface ?" New topic
Author

Calling interface ?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, What is happening at Line#7..How can we call interface with new..output is "test"


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Did you compile and run your example?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Yes Bear. output is "test" as given
Bear Bibeault wrote:Did you compile and run your example?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

OK, what do you conclude from this? What do you think is the purpose of lines 7 through 10?
Madhu Akkala
Greenhorn

Joined: Oct 26, 2010
Posts: 1
Bear Bibeault wrote:OK, what do you conclude from this? What do you think is the purpose of lines 7 through 10?


This is the only exception to interfaces. This is classic over riding of Interface method by creating an anonymous inner class.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Madhu Akkala wrote:
This is the only exception to interfaces. This is classic over riding of Interface method by creating an anonymous inner class.


It's technically not overriding an interface -- its overriding the Object class, while implementing the interface. Admittedly, the syntax is a bit weird.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

At line 7 we are not instantiating an interface, but we are instantiating an anonymous class which is implementing
the interface TestA{}. You can see the class has provided the implementation for toString() method. Then you are
instantiating that anonymous class(not the interface).

And as you have put that instance in System.out.println() it will invoke the toString() method on that object, whose defination
will come from its class( i.e/ the anonymous one)

Hope you will fix it now....


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling interface ?