• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous inner class  RSS feed

 
Omkant Parashetty
Greenhorn
Posts: 4
Java Java ME Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to java coding....

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.

Thanks in advance
Omkant
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HIH

Winston

PS: Welcome to JavaRanch!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!