A Java constructor that has a higher level of access than its class

The Java specification allows a class with default access to have public access to the constructor, what is its purpose since it cannot be referenced outside its package?

+3
source share
3 answers

I wanted to make this a comment, but since code tags are not allowed in the comments ....

Regarding your comment on CristopheDs answer:

package bob;

class MySuperHiddenClass {

  public MySuperHiddenClass() {
        System.out.println("bob");
  }
}

and

package bob;
public class MyPublicClass extends MySuperHiddenClass {

}

No constructor has been declared in MyPublicClass, but you can still call the new MyPublicClass from any package.

+4

If you ask why you might have constructors public: this is because you can, for example, call them explicitly (or implicitly) when extending the base class.

+2

, . . ( ) , , , .

/ , ?

: class visibility - , . ( ) - , , .

0

All Articles