This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quiz on anonymous classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quiz on anonymous classes" Watch "Quiz on anonymous classes" New topic
Author

Quiz on anonymous classes

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is a quiz based on K&B Chapter 8, Self Test 12, page 485-486
This is a lean version of the example.

Quiz: Replace the anonymous class declarations with named class declarations and instantiate the classes. Try to preserve the class structures and relationships.
For example,
class C extends A {}
A x = new C();
[ August 14, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hint:
Here is an analysis of the class structures and relationships.

[ August 14, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Answer: Part I.

In the first example, the anonymous subclass of A.B is an inner class of A. The reference to the enclosing instance is passed upstairs through the constructor of the anonymous class. The subclass is instantiated in a static context. It does not need the reference itself. That is difficult for me to understand.
[ August 14, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Answer: Part II.
The new classes have the same structure as the old classes.

My way of viewing the heap.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Quiz on anonymous classes
 
Similar Threads
anonymous classes
Inner Classes
Anonymous class implementing Interface
Inner Class
A Nested Class Question