user707549
user707549

Reputation:

Does Java provide 'friend' access modifier?

I wonder if Java provides 'friend' (as in C++) access modifier? Someone said we can tread 'friend' as default modifier in Java. Is it true?

Upvotes: 5

Views: 9234

Answers (4)

MeBigFatGuy
MeBigFatGuy

Reputation: 28598

As others have said, there is no friend access, but package based access is available.

However OSGI, and the (hopefully) forthcoming Super Packages attempt to extends this concept to classes in a some higher lever grouping of classes.

Upvotes: 3

Chankey Pathak
Chankey Pathak

Reputation: 21676

There is no such keyword (in Java) named as -

“Friendly”

The default access modifier has no keyword, but it is commonly referred to as “friendly.” It means that all the other classes in the current package have access to the friendly member, but to all the classes outside of this package the member appears to be private. Since a compilation unit – a file – can belong only to a single package, all the classes within a single compilation unit are automatically friendly with each other. Thus, friendly elements are also said to have package access . Friendly access allows you to group related classes together in a package so that they can easily interact with each other. When you put classes together in a package (thus granting mutual access to their friendly members; e.g. making them “friends”) you “own” the code in that package. It makes sense that only code that you own should have friendly access to other code that you own. You could say that friendly access gives a meaning or a reason for grouping classes together in a package. In many languages the way you organize your definitions in files can be willy-nilly, but in Java you’re compelled to organize them in a sensible fashion. In addition, you’ll probably want to exclude classes that shouldn’t have access to the classes being defined in the current package.

Upvotes: 5

SLaks
SLaks

Reputation: 888177

The default access modifier in Java allows members to be accessed by any code in the same package.

Upvotes: 8

anon
anon

Reputation:

There isn't a friendly modifier in Java. In Java it is called package private. And it is the default modifier. It allows members of the same package to access it.

Upvotes: 3

Related Questions