Hai ULF, we can not instatiate the interface(do not have constructor)...
so how it is possible to create an object?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35250
7
posted
0
we can not instatiate the interface
Correct. What's happening here is that an object gets instantiated that implements the interface.
You may want to read the Java Language Specification (or your favorite Java book) on the subject of "anonymous inner classes". [ June 02, 2008: Message edited by: Ulf Dittmer ]
i read some url regarding this...but still i could not get the point..
please can you explain object of interface
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35250
7
posted
0
Why don't know you try to explain what you do understand about it, and we'll chime in if you get it wrong. Often one understands things better if one needs to explain them to others, or write them down.
Ulf has told you: you are converting the interface to an inner class, which you haven't given a name to (hence anonymous). You have to implement all the interface's methods, then you have a class, then you create an object from it all in one statement.
Originally posted by seetharaman venkatasamy: 1.do not have name
Technically each class has a name, although for anonymous classes that name is not known at compile time. You can see those classes in the same folder as your other class files - they end with $1, $2 etc.
2.do not have constructor
Every class has a constructor, even anonymous classes. With anonymous classes you can't define your own constructors though; instead, it "inherits" all constructors of its parent class, and then redirects to that parent constructor. It's similar to the following:
Now you may thing, "but interfaces do not have constructors!" Well, you're 100% correct. However, the parent class in that case is Object, and that certainly has a constructor. Another similarity example:
Can anyone provide me the syntax to make an annonymous inner class extend a class or implement an interface.
Thanks & Regards
Sidharth Pallai
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
4
posted
0
Originally posted by Sidharth Pallai: Can anyone provide me the syntax to make an annonymous inner class extend a class or implement an interface.
They already have. Read seetharaman venkatasamy's post dated 2nd June 13:15 and he has provided an example of a named object reated as an instance of an anonymous class which implements the MyInterface interface, with a single method. [ June 24, 2008: Message edited by: Campbell Ritchie ]
Originally posted by Campbell Ritchie: They already have. Read seetharaman venkatasamy's post dated 2nd June 13:15 and he has provided an example of a named object reated as an instance of an anonymous class which implements the MyInterface interface, with a single method.
[ June 24, 2008: Message edited by: Campbell Ritchie ]
Thank you Campbell. But does an annonymous inner class can use implement keyword to implement an interface or extend to inherit a super
Originally posted by Sidharth Pallai: But does an annonymous inner class can use implement keyword to implement an interface or extend to inherit a super
No, you cannot use the 'implements' or 'extends' keywords with an anonymous inner class.
The anonymous inner class is an implementation of the interface that you specify - it can't implement or extend any other interface or class. [ June 24, 2008: Message edited by: Jesper Young ]
1) Reuse. If you need the same anonymous class in two places it's better to make it a named inner class, or perhaps even a method local class. 2) If you need to extend a class AND implement an interface, or implement multiple interfaces, you will need to create a class for it. 3) Just because you don't like anonymous classes. There is nothing wrong with using inner classes or method local classes instead of anonymous classes. You'll only have to think of names for those. [ June 24, 2008: Message edited by: Rob Prime ]
Originally posted by seetharaman venkatasamy:
1.do not have name
Technically each class has a name, although for anonymous classes that name is not known at compile time. You can see those classes in the same folder as your other class files - they end with $1, $2 etc.
2.do not have constructor
Every class has a constructor, even anonymous classes. With anonymous classes you can't define your own constructors though; instead, it "inherits" all constructors of its parent class, and then redirects to that parent constructor. It's similar to the following:
Now you may thing, "but interfaces do not have constructors!"
Well, you're 100% correct. However, the parent class in that case is Object, and that certainly has a constructor. Another similarity example:
Constructor inheritance logic is bizarre here for me ,but as you said that anonymous classes will always have some name so i believe there should be a default constructor of anonymous class that's used to instantiate its object.
Tell the difficulties that i am difficult.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
4
posted
0
An anomymous class has the same constructors as its superclass. If you create it from an interface which has no constructor, the compiler will provide it with a default constructor.