nerdexam
Oracle

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…

Java Object-Oriented Approach

Question

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 Person(); //line 3 } } Which two allow to allocate a new Person? (Choose two.)

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)
  • A
    8% (3)
  • B
    80% (32)
  • D
    3% (1)
  • E
    10% (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 Person itself. The opposite of helpful.
  • D (public class Main): Main is already public - 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 protected for cross-package use - only classes in the same package (p1) could call it, excluding Main in package a.

Memory tip: Visualize access as four expanding rings - privatedefaultprotectedpublic. 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

#access modifiers#inheritance#constructors#package visibility

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice