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.


How do you invoke a protected method from a subclass?

  1. Directly, if in the same package

  2. Using super keyword

  3. By creating an object of the superclass

  4. It is not accessible from a subclass

The correct answer is: Directly, if in the same package

Directly invoking a protected method from a subclass is possible if they are in the same package. If the subclass is not in the same package as the superclass, then the method cannot be directly invoked. B and C are incorrect because they do not refer to the protection level of the method or the package. D is incorrect because a protected method can be accessed from a subclass, in this case, as long as they are in the same package.