ProgrammingModerateProduct-company placement · Java

Which statement about Java anonymous classes is correct?

  1. A. They can declare constructors named after the interface they implement
  2. B. They can implement two interfaces at once
  3. C. They cannot declare constructors, but can pass arguments to the superclass constructor and use instance initializer blocks
  4. D. They cannot access any variables of the enclosing method

Correct answer: C. They cannot declare constructors, but can pass arguments to the superclass constructor and use instance initializer blocks

An anonymous class has no name, so it cannot declare a constructor; new Base(args) { … } forwards args to Base's constructor and an initializer block does any setup. It extends one class or implements one interface, and may read effectively final locals.

More OOP with C++ and Java questions

Exams that ask this topic