Multiple choice technology programming languages

package overriding; class Super1 { void testSuper(int empId) { System.out.println("I am in Super " + empId); } } public class TestOverrideSub1 extends Super1 { private void testSuper(int empId) { System.out.println("I am in Sub " + empId); } public static void main(String[] args) { Super1 s1 = new TestOverrideSub1(); s1.testSuper(144670); } }

  1. I am in Sub 144670

  2. Compiler Error

  3. I am in Super 144670

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Overriding cannot reduce visibility. The superclass has default (package-private) access, while the subclass tries private. This violates Java's overriding rules, causing a compiler error.

AI explanation

Super1.testSuper has default (package-private) access, and TestOverrideSub1 is in the same package, so the subclass's private void testSuper(int) is treated as an attempt to override it while reducing its visibility from package-private to private. Java disallows overriding methods with a more restrictive access modifier, so this fails to compile with an error like "cannot reduce the visibility of the inherited method." That makes "Compiler Error" correct. The other options assume the code compiles and either dispatches to the subclass version (impossible since private methods aren't overridden/dispatched polymorphically) or the superclass version — but neither runs because compilation fails before any output occurs.