Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


Can inner classes be declared private or protected?

  1. Yes

  2. No

  3. Only private

  4. Only protected

The correct answer is: Yes

Inner classes can be declared with any access modifier, including private and protected. However, it is common practice to declare inner classes as private so that they cannot be accessed or instantiated by other classes. Protected inner classes can be accessed by subclasses, but it is not commonly used. Therefore, options B, C, and D are incorrect as they imply that inner classes cannot have certain access modifiers. The correct answer is option A, as it acknowledges that inner classes can be declared with any access modifier.