This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

K&B Chapter 8: Inner Classes - Self Test

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

on page 481 of the K&B book there is question #3:

...

3. Which constructs an anonymous inner class instance?

A. Runnable r = new Runnable() { };
B. Runnable r = new Runnable(public void run() { });
C. Runnable r = new Runnable { public void run(){}};
D. Runnable r = new Runnable() {public void run{}};
E. System.out.println(new Runnable() {public void run() { }});
F. System.out.println(new Runnable(public void run() {}));

The answer to this question is:

3. E is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.
A is incorrect because it doesn’t override the run() method, so it violates the rules of interface implementation. B, C, and D use incorrect syntax.

Shouldn't it be C
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


does indeed use incorrect syntax.
If it were it would be correct.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeroen, I was somewhat blind
 
reply
    Bookmark Topic Watch Topic
  • New Topic