| Author |
How will it execute?
|
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
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"
|
See Dreams With Open Eyes And Make It Real
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
There are a few syntex errors in your code. Can you please refine and post it again?
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
|
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
Joined: Jan 22, 2007
Posts: 400
|
|
Try this...
|
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Thanks MM for your help.. But what kind of magic going on here ?
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
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... ...
|
BEE MBA PMP SCJP-6
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Thanks Jim for the elaborative reply
I used the correct word i think in the last post i.e "MAGIC"
|
 |
 |
|
|
subject: How will it execute?
|
|
|