• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous class behavior confusion !

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 13 i did that
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i missed line#13
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means if i do


and implement it at line 13 it will not execute. Am i correct??
 
Sheriff
Posts: 7137
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give me a code I am sorry since i am not getting it
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Ankit for clarifying my doubt
 
reply
    Bookmark Topic Watch Topic
  • New Topic