The access modifiers in java define accessibility (scope) of variable, method, constructor or class.
There are 4 types of access modifiers in java.
Table of Contents
Public, Private and Protected
.
Public access modifier
Public
modifier is accessible in the whole java world. If you put class as the public that means that class
is available everywhere.
Let’s see with help of example:
Create a class named A.java in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com; public class A { public int a; public A(int a) { this.a=a; } public void methodA() { System.out.println("In method of class A"); } } |
Create another class named "B.java" in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org; import com.A; public class B { public static void main(String args[]) { A objA=new A(20); objA.methodA(); System.out.println("Value of variable a is: "+objA.a); } } |
When you run above program, you will get below output:
Value of variable a is: 20
Private access modifier
The private
access modifier is accessible only within class.
You can not use private and protected with class unless and until it is nested class.
Let’s understand it with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com; public class A { private int a; A(int a) { this.a=a; } private void methodA() { System.out.println("In method of class A"); } } class B { public static void main(String args[]) { A objA=new A(20); objA.methodA(); System.out.println("Value of variable a is: "+objA.a); } } |
You will get compilation error at line no.22 and 23, as you can not access private variables or methods from outside the class.
Default access modifier
If you do not provide any access, JVM considers it as default
access. In the case of default access modifier, you can not access method, variable or class outside of the package
.
Create a class named A.java in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com; public class A { int a; public A(int a) { this.a=a; } void methodA() { System.out.println("In method of class A"); } } |
Create another class named "B.java" in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org; import com.A; public class B { public static void main(String args[]) { A objA=new A(20); objA.methodA(); System.out.println("Value of variable a is: "+objA.a); } } |
Here you will get compilation error at line no.11 and 12,as we are trying accesss variable a and methodA of class A outside the package com
Protected access modifier
Protected access modifiers can be accessed within the same package or outside the package by inheritance only.
Let’s understand with the help of example:
Create a class named A.java in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com; public class A { protected int a; protected A(int a) { this.a=a; } protected void methodA() { System.out.println("In method of class A"); } } |
Create another class named "B.java" in package com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org; import com.A; public class B extends A{ B(int a) { super(a); } public static void main(String args[]) { B ObjB=new B(20); ObjB.methodA(); System.out.println("Value of variable a is: "+ObjB.a); } } |
When you run above program, you will get below output:
Value of variable a is: 20
As you can see, we are able to access class A’s variable a and methodA using inheritance.
That’s all about access modifiers in Java.