Forums Register Login
difference between java acces specifiers?
I need breif difference in private, public, protected, default.
The access modifiers in Java are 'public, private, protected, or package-private (no modifier)'

In Java there are two levels of access control.

1. The class level or top level (public or package-private access)
2. The memeber level (public, private, protected, or package-private)

The class level:

When a class is declared 'public', it is visible across packages. But when it is defined package-private or default (that is without any modifier), the class is visible only to other classes within the same package.

public class JavaAdvice -> visible across packages.
class JavaAdvice -> visible only within the package.

At the class level only public or default access are allowed


The memeber level:

The following table shows the access to members permitted by each modifier.

Access Levels:

Modifier Class Package Subclass World
-------- ----- ------- ------- ------
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

In Summary,
A class can be defined public or default (no modifier) and class members are allowed to have one of 'public, private, protected, or package-private (no modifier)'. Public members are visible across packages. Any private member of a class is not visible outside of the class. Any protected members are available to the class and its subclass. Default members are visible in the class and within the same package.

Hope this helps!

Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.

This thread has been viewed 1447 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 15, 2018 08:33:35.