1Z0-819 · Question #132
Given: package p1; public class Person { protected Person() { } //line 1 } and package a; import b.Person; public class Main { //line 2 public static void main(String[] args) { Person person = new…
The correct answer is B. In Line 1, change the access modifier to public:public Person() { C. In Line 2, add extend Person to the Main class:public class Main extends Person { and change Line 3 to create a new Main object:Person person = new Main(). Option B works because making the constructor public removes all access restrictions - any class in any package can call new Person() directly, which is exactly what Main needs. Option C works because protected access grants visibility to subclasses, even across packages. When…
Question
Options
- AIn Line 1, change the access modifier to private:private Person() {
- BIn Line 1, change the access modifier to public:public Person() {
- CIn Line 2, add extend Person to the Main class:public class Main extends Person { and change Line 3 to create a new Main object:Person person = new Main();
- DIn Line 2, change the access modifier to public:public class Main {
- EIn Line 1, remove the access modifier:Person() {
How the community answered
(40 responses)- A8% (3)
- B80% (32)
- D3% (1)
- E10% (4)
Explanation
Option B works because making the constructor public removes all access restrictions - any class in any package can call new Person() directly, which is exactly what Main needs.
Option C works because protected access grants visibility to subclasses, even across packages. When Main extends Person, calling new Main() implicitly invokes Person()'s constructor via super(). Since Main IS-A Person (inheritance), assigning it to Person person is valid - the "new Person" requirement is satisfied through polymorphism.
Why the distractors fail:
- A (private): Makes access more restrictive - only callable from within
Personitself. The opposite of helpful. - D (public class Main):
Mainis alreadypublic- this changes nothing. The problem is the constructor's access modifier, not the class's. - E (package-private/default): Removing the modifier gives package-private access, which is stricter than
protectedfor cross-package use - only classes in the same package (p1) could call it, excludingMainin packagea.
Memory tip: Visualize access as four expanding rings - private → default → protected → public. To cross a package boundary without inheritance, you must reach the outermost ring (public). To cross it with inheritance, protected is the minimum - that's the key insight behind why B and C both work.
Topics
Community Discussion
No community discussion yet for this question.