user1211993
user1211993

Reputation:

Protected access to class-constructor

I have
class User
that contains protected constructor and

class Account

that has access to this constructor, but Account doesn't extends User.

How can it be??

Upvotes: 2

Views: 494

Answers (3)

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

protected access is looser than package-access. Basically, any class in the same package can use protected scoped constructs.

Specifically, see JLS 6.6.2.2 Qualified Access to a protected Constructor

... A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) only from within the package in which it is defined.

Upvotes: 0

calebds
calebds

Reputation: 26238

If your classes are in the same package, this is expected behavior:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Upvotes: 1

Ilya
Ilya

Reputation: 29721

I think, that User and Account are in the same package. Protected access is alsoo Package access

Upvotes: 6

Related Questions