File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Class.

 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of this program is :constructor of main class
Anonymous class.

How is the constructor getting invoked??? In this case, the new keyword is used for a different purpose, to create an anonymous class?







[Dave - added code tags]
[ October 12, 2008: Message edited by: David O'Meara ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi,
" Tptest p=new Tptest() {" - This code actually means to create an anonymous subclass of Tptest overriding the methods defined (in your case go.) Just as with a named subclass, the superclass constructor is automatically called.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new keyword is used the same way it always is, to create an object (not a class).
What follows the new is whatever is supposed to be used to create the object, usually a constructor call. In this case you have a constructor call included in the anonymous class because you write new TpTest().

[What you write in the class body is whatever is overridden or implemented from the original class of that name-here TpTest.]

Of course you have a constructor; every class which has been compiled has a constructor; in the case of an anonymous class you can't write a constructor, so it uses the same constructor as its superclass. Yes, the original TpTest class is the superclass, and the anonymous class you have written is a subclass of TpTest, and the go() method is overridden.
You have a constructor in the TpTest superclass; when you create an anonymous class object, that constructor is invoked. Remember you always invoke a superclass constructor when you instantiate any subclass. Any "subclass" here means anything other than java.lang.Object.

It is a little like writing super(); at the beginning of a constructor.
 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome Good to see we are being helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic