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

How will it execute?

 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i execute this type of inner class?


How do i call this so that the ans would be "Human drinks water in main class"
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few syntex errors in your code. Can you please refine and post it again?
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant.. I tried a lot to execute it anyhow. But i am sure that there is no syntax error in go() part of InnerAnoInterface Class
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...

 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks MM for your help.. But what kind of magic going on here ?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several things to look at. First, on line 06 below, go() is called
on the anonymous class created by new. Okay so far?
Then looking at the definition of Water, its drinkWater() method immediately calls
drink() on an Interf object that is passed in. The key to the puzzle is to figure out
how this Interf object is created.

Notice below that the parameter passed to drinkWater() continues all the way from
the 'new' on line 02 through line 06. The drinkWater() call ends with ); on line 06.Below again is the type Interf parameter from above. Line 07 starts a rewritten version
that illustrates what is happening.So you can 'new' an interface, creating an anonymous class, if you implement the interface's
methods in this way. Right in the call parameter list, Interf is implemented by a 'no-name'
class and then the 'no-name' class is instantiated as a type Interf object. Pretty cool, ey?

Jim... ...
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim for the elaborative reply
I used the correct word i think in the last post i.e "MAGIC"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic