When we create anonymous inner class for interface, we get one object for the sublcass of that interface .
In interface there is no constructor then how do we get that object. We know that to create Anonymous inner class we should use one super class constructor.
Please let me know whether my understand is correct or not.
I presume you are familiar with the Java Tutorials section about nested classes.
You no longer have an interface, but a class. Since an interface has no constructor, the compiler will give the class a default constructor. Try it by writing an anonymous class and inspecting its bytecode with an instruction like this:-
javap -c Foo$1
Omkant Parashetty wrote:We know that to create Anonymous inner class we should use one super class constructor.
Actually, in the case you describe, you can only use ONE superclass constructor: super().
An anonymous class created from an interface can only possibly be a subclass of Object; even though it implements whatever interface you're creating it for. And that's why, as Campbell says, it will work quite happily without you defining any constructor at all.
Think about what happens with a real class definition: If you write:what you actually get is:and that's why anonymous classes derived from interfaces don't need an explicity defined constructor.
I should probably add that the case of an anonymous class derived from an abstract class is slightly diffierent.
In that case you must call one of its constructors; and if it isn't a "no-args" constructor, you must add your own constructor and super(...) call yourself.
PS: Welcome to JavaRanch!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
What Winston means is that if you have an abstract class like this:-… you have to put the super(…); call in the constructor, then call that constructor when you instantiate the anonymous class like this:-I know you cannot write a new constructor in an anonymous class.
Did you find the default constructor in your anonymous class with javap?
Can you do any of the following?
1: Create an anonymous class from a final class?
2: Redefine or override a final method in an anonymous class?
3: Create a subclass of an anonymous class?
I have never tried but all three look as though they should be impossible.