ProgrammingHardWipro · Cognizant pattern

Which statement is correct?

Which statement is correct?
class Base {
    protected void configure() { }
}
class Derived extends Base {
    public void configure() { }
}
  1. A. It compiles only if Derived is in the same package as Base
  2. B. It compiles fine: overriding may widen (but never narrow) the inherited method's access level
  3. C. It fails to compile because configure() must stay protected in both classes
  4. D. It fails to compile: visibility was reduced

Correct answer: B. It compiles fine: overriding may widen (but never narrow) the inherited method's access level

Java explicitly permits an override to broaden access (protected to public here), since that can never break any caller who already had access at the narrower level — only narrowing access is forbidden.

More OOP with C++ and Java questions

Exams that ask this topic