Forums Register Login

Anonymous class issues

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Is for statement missing a closing ")" bracket?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 ]
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 969 times.
Similar Threads
Anonymous class as parameter
Annnymour class Q in Marcus exam
A anonymous class question
MG1 #34
Question 34 from greens Mock
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:45:36.