class Parent { protected void protectedMethod() {
class Parent {
protected void protectedMethod() {
System.out.println("Protected method in Parent");
}
}
class Child extends Parent {
public void childMethod() {
protectedMethod();
}
}
public class Test {
public static void main(String[] args) {
Child child = new Child();
child.childMethod();
}
}
What will happen when the code is executed?
Show answerHide answer
Correct answer: (A) The code will compile and print 'Protected method in Parent'.
Explanation
A protected member is accessible in a subclass, so Child can call protectedMethod() directly. The code compiles and prints "Protected method in Parent".