• 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 Class

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know Anonymous innerclasses can extend a class or implement an interface.
Can any one help me what is the syntax to define such an anonymous inner class.
Thanks in advance,
Bala
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
� public A(int i) { } // deliberately put a constructor to show that Anonymous classes CAN use constructors
� public void m1() { S.o.p("a.m1"); }
}
interface I { void i1(); }
class Outer
{
� public A getAnonymousClassForA()
� {
� return new A(10){public void m1() { S.o.p("anonymousA.m1"); } }
� }
� public I getAnonymousClassForI()
� {
� return new I(){public void i1() { S.o.p("anonymousI.m1"); } }
� }
}

HTH,
Paul.
------------------
Get Certified, Guaranteed!
http://www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited September 30, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would like to clarify this....
anonymous class cannot define constructors as it doesnt have a name...So either
1)the super class must provide a constructor
2) an instance initializer can be used to achieve the same effect...
Please clear the doubts...!?how can u say that it can define constructors???
faiza
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
I had the same question as to how an anonymous inner class would extend a class (in addition to the outer class) or implement an interface. I didn't understand how your example answers this question. Could you please explain a few lines about how your example works?
Thanks in advance.
-Raj
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For :
public A getAnonymousClassForA()
{
return new A(10){public void m1() { S.o.p("anonymousA.m1"); } }
}
The anonymous class ( of the object) that is returned by this method extends class A implicitly. This is the way you can have anonymous class extend any other class.
I did not say that anony. class can "define" constructors. It can "use" the constructors defined by the class it extends.
I do not understand what do you mean by "in addition to outer class". If you are under the impression that anony class always extends outer class then it is wrong.
For:
public I getAnonymousClassForI()
{
return new I(){public void i1() { S.o.p("anonymousI.m1"); } }
}
In this case the anomymous class ( of the object) returned by the method extends Object but implements the interface I.

You question was, "I know ....want to know the syntax...". So I gave just the syntax. Sorry for lack of expl. Does this clear up now?
HTH,
Paul.


------------------
Get Certified, Guaranteed!
http://www.enthuware.com/jqplus
 
Raj Palanivel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, Thanks for the good explanation. It it not true that the inner class implicitely extends the outer class also?
 
reply
    Bookmark Topic Watch Topic
  • New Topic