| Author |
Anonymous class behavior confusion !
|
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Hello Everyone
I have a query from K & B book chapter 8, it is written over there that "Anonymous interface implementers can implement only one interface. There simply isn't any mechanism to say that your Anonymous inner class is going to implement multiple interface not even they can extebd a class and implement an interface. The inner clas has to choose either to be a subclass of named class and not implement any interfaces at all-or to implement a single interace."
Can any one explain above with an example? What i get from this statement is this:-
But it runs properly
|
See Dreams With Open Eyes And Make It Real
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
It means that Anonymous classes can only be defined for one interface of abstract class
Here Anonymous classes are created at line # 7 and 23.
First is implementing Runnable interface and second is implementing Interf interface.
In both the case you are implementing single interface at a time. You can't implement Runnable and Interf simultaneously as in normal class.
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
|
At line 13 i did that
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Shailesh,
The statement is quite clear. An anonymous class can extend one class OR implement one interface. There is just no way that an anonymous class can derive from more than one class/interface.
In your example, you have created 3 anonymous classes:-
1. Anonymous class at line 7, that implements the interface Runnable
2. Anonymous class at line 13, that implements the interface Interf
3. Anonymous class at line 23, that implements the interface Interf
Each one implements one interface, as should be.
So, what is your confusion about?
- Nidhi
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Hi Nidhi,
I have extended Runnable interface to Interf at line 1 so multiple interfaces are implemented at Line 13 anonymous class. i am confused about that. If you please write a code it will be helpful for me to understand the above statement.
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
Sorry i missed line#13
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Shailesh Phatak wrote:At line 13 i did that
Not quite :-)
At line 13, you did this:
Created an instance variable IF of the anonymous class (that was declared on line 7).
instantiated IF creating a new anonymous class that implements Interf
Interf itself implements Runnable.
You are implementing just one interface. That interface may be implementing multiple interfaces.
Let me try to put it another way, using classes. Consider the following code:
XYZ is extending just one class, Child. The fact that Parent & GrandParent are in the hierarchy of Child does not mean that XYZ extends from multiple classes.
Hope that clears your doubt!
- Nidhi
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
It means if i do
and implement it at line 13 it will not execute. Am i correct??
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Shailesh,
Please UseAMeaningfulSubjectLine instead of something like "Confused!" when you post a question.
Please edit your post to add a meaningful subject line by clicking the button.
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
No, It will execute.
The point here is that you can not extend or implement more then one class, interface while defining an Anonymous class.
when you will create Anonymous class for Interf you will provide definitions of required methods and it will work fine because Interf is extending Runnable and AnotherInter and when you will implement it as a Anonymous class it will be a single Interface, not two seperate interface.
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Please give me a code I am sorry since i am not getting it
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
As you know, java doesn't support multiple inheritance of classes, now if I have these classes
Now here would you say that Sub extends both Middle and Base?? Sub extends from only Middle. Sub inherits behavior of both Base and Middle, but it directly only extends from Middle. In the same way at line 13 in your program, the anonymous inner class is only implementing the Interf interface. Runnable is a super-interface of Interf, but the anonymous inner class doesn't directly implement Runnable. So the anonymous inner class actually implements only one interface...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
|
Now what will make the above code to not get compiled?? I mean what should i change in the above program so that it will break the rules which are said in above statement?? At least give me a "code" so that i should understand it properly.. .
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
Thanks Ankit for clarifying my doubt
|
 |
 |
|
|
subject: Anonymous class behavior confusion !
|
|
|