Difficulty: Medium
Correct Answer: Boo f = new Bar() { };
Explanation:
Introduction / Context:This question focuses on valid anonymous inner class creation expressions and constructor usage in inheritance hierarchies.
Given Data / Assumptions:
Concept / Approach:An anonymous inner class can extend a concrete class or implement an interface. The syntax is new Type(args) { /* body */ }. The constructor arguments must match an existing constructor. The resulting instance type is an anonymous subclass of the specified type.
Step-by-Step Solution:
Option B constructs an anonymous subclass of Bar using the no-arg Bar() constructor. The variable type Boo is acceptable because Bar extends Boo.Option A is invalid because Boo(24) does not match any constructor (int vs String/no-arg).Option C and D use illegal anonymous class syntax (you cannot write parameter types in the type position nor qualify Boo.Bar as though it were a member class).Verification / Alternative check:Also valid would be Boo f = new Boo() { }; using Boo's no-arg constructor, but that option is not provided exactly as such.
Why Other Options Are Wrong:They either use non-existent constructors or incorrect syntax for anonymous classes.
Common Pitfalls:Supplying wrong constructor arguments for the chosen type, and confusing anonymous subclassing with named nested classes.
Final Answer:Boo f = new Bar() { };
Discussion & Comments