ProgrammingHardWipro · Cognizant pattern
Which statement is correct?
Which statement is correct?
class Base {
protected void configure() { }
}
class Derived extends Base {
public void configure() { }
}- A. It compiles only if Derived is in the same package as Base
- B. It compiles fine: overriding may widen (but never narrow) the inherited method's access level
- C. It fails to compile because configure() must stay protected in both classes
- 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
- Which statement is correct? java final class Immutable { void f() { } } class Sub extends Immutable { }
- Which statement is correct? java class Base { final void process() { System.out.print("Base.process"); } } class Sub extends Base { void…
- What is printed? java class Outer { private int secret = 42; class Inner { int reveal() { return secret; } } } public class Main { public…
- Which statement about overload resolution with varargs is correct? java class A { void f(int a, int b) { System.out.print("fixed"); } void…
- Which statement is correct? java interface Shape { double area(); } abstract class AbstractShape implements Shape { public abstract double…
- What is printed? java class Stack<T> { private List<T> items = new ArrayList<>(); void push(T item) { items.add(item); } T pop() { return…