class Person { private String name;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
private void displayName() {
System.out.println("Name: " + name);
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person("John");
p.displayName();
}
}
What will happen when the above code is compiled and executed?
Show answerHide answer
Correct answer: (A) Compilation Error: The method displayName is not visible in Test.
Explanation
displayName() is private in Person, so it cannot be called from class Test. The compiler reports that displayName() has private access in Person.