package inheritance; public class TestInheritance7{ protected int p = 100; } package inheritance1; import inheritance.TestInheritance7; public class TestInheritance_7 extends TestInheritance7{ void printP() { TestInheritance7 T7 = new TestInheritance7(); System.out.println("p = "+T7.p); } public static void main(String[] args) { TestInheritance_7 T_7 = new TestInheritance_7(); T_7.printP(); } }
-
p = 100
-
Compiler error
-
p = 0
-
Exception
B
Correct answer
Explanation
The code fails to compile because 'p' is protected and TestInheritance_7 is in a different package. While protected members are accessible in subclasses, the access is through a parent class reference (TestInheritance7 T7 = new TestInheritance7(); T7.p). Protected members in different packages can only be accessed through inheritance or subclass references, not parent class instances. Option A is wrong because compilation fails. Option C is wrong for the same reason.