Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous class issues

 
Wilson Mui
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this in the Marcus Green test:
Which of the following statements about this code are true?

I thought this would give an error because of the curly braces in for the anonymous class declaration. I did indeed test the code, and it works, but it also works when there is no curly braces. I have to say this is the first time I've seen an anonymous class written with curly braces, and am not sure what teh rule is, and why it works both ways. Can somebody tell me what is going on?
Damn, can't get this format right.
[ May 08, 2003: Message edited by: Wilson Mui ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The addition of the curly braces means that an anonymous class is being declared that extends Turing, but the anonymous class doesn't add anything new to the Turing type.
m.go(new Turing(){});
The go method accepts a Turning object or any subclass of Turing.
public void go(Turing t){
Since the anonymous subclass of Turning does not add anything new to the Turning type and because the go method accepts a Turning object or any subclass of Turing it really does not matter if the instance created in the first statement is an anonymous class or just and instance of Turning. For that reason, it doesn't make any difference if you remove the curly braces.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is for statement missing a closing ")" bracket?
 
Wilson Mui
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woah that is the first time that I have ever heard of declaring an anonymous class that is extending a class. I know the example in the marcus green exam was somewhat of a trite example that didn't do anything. But I was wondering if anybody has had any experience where this was useful.
But I that is some obscure Java programming...for me at least.
 
aadhi agathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wilson Mui:
But I was wondering if anybody has had any experience where this was useful.

please check out bruceeckel.com for "Thinking in Java" , it has got very good examples on this
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be familiar with the construct of anonymous classes. It isn't because you will have lots of questions about them, rather you will see them used a lot as a space-saving measure in the real exam. (The examiners want to keep the code displayed in the window to a minimum in order to avoid excessive scrolling.) So, be prepared to see anonymous classes being used to do things like implement interfaces, a good example being Runnable in Threading questions.
[ May 09, 2003: Message edited by: Roger Chung-Wee ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic