This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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:-
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.
The statement is quite clear. An anonymous class can extend one classOR 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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Joined: Aug 27, 2009
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.
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.
Joined: Aug 27, 2009
Please give me a code I am sorry since i am not getting it
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...
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.. .