• 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 Inner Classes

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following question about the instantiation of anonymous inner classes.
given the following code :

The output is:
Subclass: s = "Hello"
x.s = "Hello"

I understand the following:
- An anonymous inner class declaration effectively creates a subclass of the declared type. This is apparent given the output of the program - the overridden aMethod() is called.
- An anonymous inner class declaration cannot have a constructor.
- Constructors cannot be inherited, and therefore cannot be overridden.
What I'd like to know is how initialisation occurs when a parameter is passed to the anonymous inner class. (In the above code the parameter is the String "Hello").
Since the anonymous inner class has no constructor, and no explicit call is made to the superclass via super(), how then is the String member variable (s) correctly initialised with the given "Hello" parameter ? Does the anonymous class simply "use" the constructor of its parent ?
[ May 03, 2003: Message edited by: Rory French ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An anonymous class (no need to say "inner", BTW, as by definition an anonymous class is always an inner class) does have a constructor, which is provided only by the compiler and never by the programmer.
An anonymous class always implicitly extends the class or implements the interface after the new keyword, which explains why it cannot have the extends or implements keywords. If the anonymous class extends another class, then the class instance creation expression can include parameters (as in this case) which will be passed to a constructor of the superclass. The compiler must therefore be providing this code in Anon's constructor:
super("Hello");
If the anonymous class implements an interface, then the instance creation expression can not include any parameters.
[ May 03, 2003: Message edited by: Roger Chung-Wee ]
 
Rory French
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha ! Thanks Roger.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic