Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Anonymous inner class

 
Greenhorn
Posts: 4
Java ME Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic