Java access levels classes

Java Access Modifiers

Java provides four access modifiers to set access levels for classes, variables, methods and constructors i.e. public, private, protected and default. These access level modifiers determine whether other classes can use a particular field or invoke a particular method.

Let’s quickly compare these access modifiers in nutshell.

  • public – accessible everywhere
  • protected – accessible in the same package and subclasses outside the package
  • default – accessible only in the same package
  • private – accessible only in the same class

The access specifiers can be ordered based on their strictness as below. The public is the least restrictive, and the private is the most restrictive.

public > protected > package-private (or default) > private

The public members are accessible from everywhere. A public class, method, constructor, or interface could be accessed from any other class in the application. However, if the public class we are trying to access is in a different package, then we must import the class before using it.

public class Data < private String format; public String getFormat() < return this.format; >public void setFormat(String format) < this.format = format; >>

In the above example, getFormat() and setFormat() methods are public so they can be accessed from any class.

Please note that the fields in an interface are implicitly public static final and the methods in an interface are, by default, public .

The protected members are accessible by the classes of the same package and the subclasses outside the package.

Читайте также:  Javascript xmlhttprequest open send

In Data class, the method displayMessage() is declared protected, so it can be accessed by all the classes present in the same package where HelloWorld.java is present, as well as child classes present in other packages as well.

If we try to access the displayMessage() in another package without extending the Data class, we get the following compilation error:

'displayMessage()' has protected access in 'com.howtodoinjava.core.basic.accessModifiers.package1.Data'

Protected access modifier

When we inherit the Data class, then we can access the displayMessage() outside the current package.

public class Main extends Data < public static void main(String[] args) < Main main = new Main(); main.displayMessage(); >>

The default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. The default members are accessible only by the classes in the same package.

Let us remove the protected access from the displayMessage() in the Data class. It changes the access to default.

Now when we try to access the displayMessage() in the child class outside the package, we will start getting the compilation error:

'displayMessage()' is not public in 'com.howtodoinjava.core.basic.accessModifiers.package1.Data'. Cannot be accessed from outside package

default access modifier

The private access modifier is the most restrictive access level. The topmost classes and interfaces cannot be private. The private members are accessible within the same class only. The private methods, variables, and constructors can only be accessed within the declared class itself.

We are modifying the previous example again by changing the default access to private access for the displayMessage() method.

Now the method is private so no other class can access it directly.

2. Levels of Access Control

There are two levels of access control.

  • Class level access – allows modifiers to be public, or package-private (default).
  • Method level access – allows modifiers to be public, private, protected, or package-private (default).

Local variables and formal parameters cannot take access specifiers. Since they are inherently inaccessible to the outside according to scoping rules, they are effectively private.

Both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.

Access levels affect us in two ways.

  • First, when you use classes that come from another source, access levels determine which members of those classes your own classes can use.
  • Second, when you write a class, you must decide what access level every member variable and every method in your class should have.

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

Источник

Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level— public , or package-private (no explicit modifier).
  • At the member level— public , private , protected , or package-private (no explicit modifier).

A class may be declared with the modifier public , in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class. 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.

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

The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Let’s look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

Visibility

Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

  • Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.

Источник

Controlling Access to Members of a Class

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level— public , or package-private (no explicit modifier).
  • At the member level— public , private , protected , or package-private (no explicit modifier).

A class may be declared with the modifier public , in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class. 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.

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

The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Let’s look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

Visibility

Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

  • Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.

Источник

Оцените статью