Access Modifiers in Java – Enhance Your Programming Skills

Access modifiers in Java serve the purpose of limiting the access of particular entities in a program. Consider the real-life example of a bank.

The address of the bank is available to all. This is open publicly so that everyone in and around knows about the presence of the bank. However, protection is there on passwords or the access PIN’s of individual account holders in the bank, i.e only the bank and the account holder knows the PIN. No other person has access to a particular detail. This is done in programming by defining access as we shall learn in this article.

Access modifier in java

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Access Modifiers in Java

Access modifier in Java is the reserve keyword which limits or allows particular entities such as classes, methods to be accessible by other entities in the program. It simpler words, it restricts the scope of the particular class, variable or method.

There are 4 different types of entities

1. Default
2. Public
3. Private
4. Protected

1. Default Access Modifier in Java

The default access modifier is assigned to a particular program entity when no access specifier is explicitly mentioned. It limits the access of all those under its scope to the package in which the class is defined. It means that classes from other packages cannot access the entity, be it a variable, a class or even a variable.

In order to examine this, we will create two packages p1 and p2. All of the variables in these classes will be of default datatype.

Let’s see whether we can access it from a different package or not.

Java program to illustrate the use of the Default access modifiers:

//The first class

package com.dataflair.acessspecifiers.p1;
import java.io. * ;
public class AccessSpecifiers {
  void speak() {
    System.out.println("Hello DataFlair!");
  }

}

//The second class in a different package p2

package com.dataflair.acessspecifiers.p2;

import com.dataflair.acessspecifiers.p1. * ;
import java.io. * ;
class AcessSpecifier2 {
  public static void main(String[] args) throws IOException {
    AccessSpecifiers ob = new AccessSpecifiers();
    ob.speak();
  }
}

Output

speak() is not declared public in com.dataflair.accessspecifiers.p1.AccessSpecifers. Cannot be accessed from the outside package.

2. Public Access Specifier in Java

The public specifier has the widest access boundary ever. We can access it through classes and packages. There is absolutely no restriction on the access of public variables, classes or variables.

Java program to explain the concept of public access specifier:

package com.dataflair.acessspecifiers.p1;

public class PublicCLass1 {
  public void speak() {
    System.out.println("Hello I am speaking in public mode!");
  }
}

//Now we include the second class of a different package

package com.dataflair.acessspecifiers.p2;
import java.io. * ;
import com.dataflair.acessspecifiers.p1. * ;
public class PublicClass2 {
  public static void main(String[] args) throws IOException {
    PublicCLass1 ob = new PublicCLass1();
    ob.speak();
  }

}

Output

Hello I am speaking in public mode!

3. Private Access Specifier in Java

Private access modifier limits the scope of access to the class within which we declare them. It means that all the methods and the variables which are private cannot be used by a different class of the same package.

However, there are certain limitations to this access specifier. We cannot use it in top-level classes. We can access these entities only within the classes they are defined in.

Java Program to illustrate the private access specifier:

//Class 1 
package com.dataflair.acessspecifiers.p1;
class PrivateCLass {
  private void speak() {
    System.out.println("Hey DataFlair I am speaking privately!");
  }
}

//class 2
package com.dataflair.acessspecifiers.p1;

import java.io. * ;
class PrivateClass2 {
  public static void main(String[] args) throws IOException {
    PrivateCLass ob = new PrivateCLass();
    ob.speak();
  }
}

Output

speak() has private access in com.dataflair.acessspecifiers.PrivateCLass

As we can see both of the classes are from the same package. But, the private keyword limits access. Hence the second class cannot create an object of the first class’s method namely speak.

4. Protected Access Specifier in Java

Protected has an interesting scope of access. It is accessible by the same package or subclasses in a different package in java. Any other entities cannot access it.

If there is a need to access protected members from a different package other than the one it is defined in, then we use inheritance. If a constructor is declared as protected, then instances of the class cannot be created outside the package in which it is defined.

A protected variable or a method can only be overridden to other classes by using a public or protected keywords only. The outer class or interface should not be protected.

Java program to evaluate the functioning of protected access:

//First program in package p1

package com.dataflair.acessspecifiers.p1;

public class ProtectedClass1 {
  protected void speak() {
    System.out.println("Hello DataFlair! I am speaking in a protected way!");
  }
}

//Now the second program 

package com.dataflair.acessspecifiers.p2;
import com.dataflair.acessspecifiers.p1. * ;

public class ProtectedClass2 extends ProtectedClass1 {
  public static void main(String[] args) {
    ProtectedClass2 ob = new ProtectedClass2();
    ob.speak();
  }

}

Output

Hello DataFlair! I am speaking in a protected way!

Summary

Summing up, these are the access specifiers that are important in Java programming and play an integral part in the access to the scope of different programs. Without access specifiers, it would be impossible to grant specific access to specific parts of the program. This enables Java to be a superior language having high security. Any applications we develope by this programming language is trustworthy because of these constraints which programmers can put.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.