Types of Access Modifiers in Java

Posted By : Sulabh Kumar | 30-Nov-2020

Java

Loading...

Java is an object-oriented language that is also platform-independent. We created methods and classes in java, to give special access to these methods and classes and to specify the accessibility and scope of the methods and classes we can use access modifiers.

Java has two types of modifiers -

1. Access Modifiers

2. Non-Access Modifiers

Access Modifiers- There are four types of access modifiers in java-

1.Public

2.Private

3.Default

4.Protected

Public- This type of access modifier can be accessible from within the class, outside the class ,within the package and outside the package.That means public type of access modifier can be accessible everywhere.

Example- We have two packages, one is 'package1' and second one is package 2'.To access a method from one package to another -

  1. package package1;
  2. public class Test{
  3. public void methods(){System.out.println("Hello World");}
  4. }

In Second package we can call above method as-

  1. package package2;
  2. import package.*;
  3. class test2{
  4. public static void main(String args[]){
  5. Test obj = new Test();
  6. obj.methods();
  7. }
  8. }

Private- This access level of modifiers can be accessed within the class.This modifier cannot be accessed from outside the class and package.

We created two class Class1 and Class2, we will create a private method and will try to access in another class-

  1. class Class1{
  2. private int x=40;
  3. private void method1(){System.out.println("Hello World");}
  4. }
  5. public class Class2{
  6. public static void main(String args[]){
  7. Class1 obj=new Class1();
  8. System.out.println(obj.x); // Compile Time Error will be displayed
  9. obj.method1(); //Compile Time Error will be displayed
  10. }
  11. }

Default- This type of access modifier can be accessed from within the package only,It cannot be accessed from outside of the package also if we do not specify any type of access modifier for a class or method then it will be considered as default.

We will create two packages- package1 and package2.

  1. package package1;
  2. class Test1{
  3. void method1(){System.out.println("Hello");}
  4. }
  1. package package2;
  2. import package.*;
  3. class Test2{
  4. public static void main(String args[]){
  5. Test1 obj = new Test1(); //Compile Time Error is displayed as we try to access the method from another package
  6. obj.method1(); //Compile Time Error is displayed as we try to access the method from another package
  7. }
  8. }

Protected- This type of access modifiers can be accessed from within the package and outside of the package by using child class, if we do not create child class then we cannot access from outside the package.

Here We will create two packages- package1 and package2.

  1. package package1;
  2. public class Test1{
  3. protected void method1(){System.out.println("Hello World");}
  4. }
  1. package package2;
  2. import package.*;
  3. class Test2 extends Test1{
  4. public static void main(String args[]){
  5. Test2 obj = new Test2();
  6. obj.method1();
  7. }
  8. }

Output will be Hello World


At Oodles ERP, we provide full-scale ERP software development services to solve complex business problems and streamline diverse business operations. Our custom ERP software development services are conducive to increasing business productivity and improving operational efficiency. For more information, contact us at [email protected].