Access Specifiers Permissions and Restrictions

Access Specifiers Permissions and Restrictions

Access specifiers, as the name indicates, specify the access to our code for other classes – whether other classes can access or not and if permitted, to what extent they can access. Java includes access modifiers also which are quiet different from access specifiers.

Java comes with four access specifiers. They are

  1. public
  2. protected
  3. default
  4. private

All the Access specifiers are keywords and thereby should be written in lowercase letters only. Java does not have the confusing combinations of public protected etc. which C++ does. All specifiers are straight to their meaning. To understand the specifiers easily, let us take a small scenario where two packages exist – animal and bird. Let us imagine two classes Lion and Tiger exist in animal package and Parrot and Peacock exist in bird package. It is illustrated in the following figure.

Let us think that there exists a method called void eat() in the Tiger class of animal package. Now the question is who can access this eat() method; Lion of same package or Peacock of different package. The access specifiers, in Java, act on package boundaries.

1. public

Let us assume that the eat() method is public. If a member of a class is public

1. It can be accessed by the classes of the same package. That is, Lion of the same package can access it.
2. It can also be accessed by the classes of other packages also. That is, Peacock of other package also can access.

public member of a class can be accessed by any class of any package as every one has got equal right to access. public gives maximum permissions among all access specifiers with no restrictions. public means, think like a public park; anybody can come in and anybody can go out; no ticket and no restrictions. Do not think always public, we can put restrictions with other access specifiers. Let us go to the other.

2. protected

Let us assume that the eat() method is protected. If a member of a class is protected

1. It can be accessed by the classes of the same package as if public. That is, Lion of the same package can access it.
2. Only the subclasses of other packages can access. That is, Peacock should extend Tiger class and use eat() method, else not permitted.

protected gives a small restriction for the classes of other package only, but not to the classes of the same package.

3. default

Let us assume that the eat() method is default. Default means we do not specify any specifier at all; that is we write simply void eat() without any specifier.

Generally, novices think default is public; it is not correct. Default has its own restrictions.

If a member of a class is default

1. It can be accessed by the classes of the same package as if public. That is, Lion of the same package can access it.
2. It is impossible for the classes of other package to access (even if they extend also). That is, Peacock cannot access.

The specifiers, public, protected and default does not make any difference for the classes of the same package, but makes to the classes of other packages only. We can say that access specifiers act on package boundaries.

The default specifier is known as package-level access specifier as the classes of the same package only are allowed to access.

Note: Actually there exist a default keyword in Java, but used with switch statement. Do not get confused here with the default specifier.

4. private

Let us assume that the eat() method is private in Tiger class.

If a member of a class is private, it can be accessed by the objects of the same class only but not by the classes of the other package or even of the same package. private means, it is purely for the private usage of the same class. Other classes, even of the same package, cannot access by composition. That is, private member should be used within the same class with the same class objects. More information on private is available at Java Private variable accessibility (Private access specifier)

Topics related to access specifiers and modifiers.

1. Access Specifiers & Access Modifiers
2. Access Specifier vs Access Modifier in Java

5 thoughts on “Access Specifiers Permissions and Restrictions

  1. ram

    hai sir this ia ram.my doubt is,if a method is protected and we need to over ride that method in subclass so what access specifier must be use to over ride that method

  2. keyword research

    Hi! I could have sworn I’ve been to this site before but after checking through some of the post I realized it’s new to me.
    Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and checking back
    frequently!

Leave a Reply

Your email address will not be published. Required fields are marked *