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() { }
}- A. It fails to compile because private methods cannot exist in a subclass
- B. It fails to compile: an override cannot reduce visibility from public to private
- C. It compiles fine; Sub.show() overrides Base.show() with narrower access
- 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
- Which statement is correct? java class Base { protected void configure() { } } class Derived extends Base { public void configure() { } }
- 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…