Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling interface ?

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, What is happening at Line#7..How can we call interface with new..output is "test"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you compile and run your example?
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Bear. output is "test" as given
Bear Bibeault wrote:Did you compile and run your example?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, what do you conclude from this? What do you think is the purpose of lines 7 through 10?
 
Madhu Akkala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....


  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic