ProgrammingHardHCL · Tech Mahindra pattern

Which statement is correct about this class?

Which statement is correct about this class?
public class Base {
    public void show() { }
}
class Sub extends Base {
    private void show() { }
}
  1. A. It fails to compile because private methods cannot exist in a subclass
  2. B. It fails to compile: an override cannot reduce visibility from public to private
  3. C. It compiles fine; Sub.show() overrides Base.show() with narrower access
  4. D. It compiles, and Sub.show() simply hides Base.show() without being an override

Correct answer: B. It fails to compile: an override cannot reduce visibility from public to private

Attempting to declare show() with reduced visibility (public to private) where the signature matches an inherited public method is a direct violation of the overriding-visibility rule and is a compile-time error — it is specifically rejected as an invalid override attempt, not silently treated as an unrelated private method.

More OOP with C++ and Java questions

Exams that ask this topic