Access Specifiers

Java Access Specifiers (also known as Visibility Specifiers) regulate access to classes, fields and methods in Java.These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access.

Let’s take an example to understand it clearly. Nowadays, everybody is familiar with Facebook and it has privacy settings for content you post, which you can customize as per your requirements. Like you can make your Post to Public (Visible by Everyone), Private (Visible to You only), Protected (Visible to Friends of Friends) and Default (Visible to Friends only).

Access specifiers are an integral part of object-oriented programming. Also they (or access specifiers) are keywords in OOPS that set the accessibility of classes, methods, and other members and these modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.

Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes.

The four access levels are:

1.Visible to the package the default. No modifiers are needed.

2.Visible to the class only (private).

3.Visible to the world (public).

4.Visible to the package and all subclasses (protected).

The following table summarises that what access specifiers may be assigned to different elements

Access Specifiers
  Different Class Same Package Different Package
Same Class
Child Class
Non-Child
Child Class
Non- Child
Class Class
Public Yes Yes Yes Yes Yes
Protected Yes Yes Yes Yes No
Default Yes Yes Yes No No
Private Yes No No No No

 

Default Access Modifier – No keyword:

A default member may be accessed only if the class accessing the member belongs to the same package,whereas a protected member can be accessed (through inheritence) by a subclass even if the subclass is in a different package.

A variable or method reported with no access-control modifier can be obtained to another group in exactly the same program. The areas within an interface are implicitly public static closing and the strategy within an interface are automatically people.

Default and protected behavior differ only when we talk about subclass.If the protected Keyword is used to define a member , any subclass of the class declaring the member can access it through inheritence.Whereas default access doesen’t extend any special consideration to subclasses , so when you think of default access ,think package restriction.

Example:

Variables and methods can be declared without any modifiers, as in the following examples:

Example:

String version = “1.5.1”;

boolean processOrder() {

return true;

}

Private Access Modifier – private:

Members marked private can’t be accesses by a code in any class other than the class in which the private member was declared.A private member is invisible to any code outside the member’s own class.When a member is declared private,a subclass can’t inherit it.

Can a private method be overridden by a subclass?That’s an interesting question,but the answer is technically no.Since the subclass cannot inherit a private method,it therefore cannot override the method–overriding depends on inheritence.

Techniques, Variants and Constructors which can be declared private can only be obtained within the stated type it self.Variants which are declared private can be accessed outside the group if community getter methods can be found in the group.

Example:

The following class uses private access control:

public class Logger{
private String format;
public String getFormat(){
return this.format;
}
public void setFormat(String format){
this.format=format;
}
}

Here, the structure variant of the Logger class is personal, so there isn’t any means for additional categories to recover or set its value immediately.

Therefore to get this variable available to the external world, we described two public methods: getFormat(), which yields the value of structure, and setFormat(String), which establishes its worth.

Public Access Modifier – public:

When a method or variable member is declared public, it means all other classes,regardless of the package they belong to, can access the member(assuming the class itself is visible).

For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.

Protected Access Modifier – protected:

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

Remember that any time talk about a subclass having a access to a superclass member ,we could be talking about the subclass inheriting the member,not simple accessing the member through a reference to an instance of the superclass.

Example:

package other;                                      //Different Package

import certification.Parent;

class Child extends Parent {

public void testIt() {

System.out.println("x is " + x);                   //No problem;Child inherits x
}
}

Example:

The Following parent class uses protected access control,to allow its child class override openSpeaker() method:

Class AudioPlayer{
protected boolean openSpeaker(Speaker sp){
//implementation details
}
}
Class StreamingAudioPlayer{
Boolean openSpeaker(Speaker sp){
//implementation details
}
}

Here, if all of us determine openSpeaker() procedure as personal, then it wouldn’t normally be reachable from another group additional than AudioPlayer. If we all determine it as people, then it might become accessible to each of the external planet. But our in tension would be to uncover this system to its subclass simply, that’s why we utilized shielded modifier.

 

%d bloggers like this: