Access Modifiers in Java
Access Modifiers determines the level of access or visibility of data
or method or constructor. There are four types of access modifiers in Java :
Public
Private
Protected
Default
Public
They have highest level of accessibility or visibility. The keyword is "public". They are
accessible from everywhere in the java project.
Private
The keyword is "private". They are accessible within the
class only in which they are declared or created.
Class1
having public and private data:
public class Test1{
private Test1() {
System.out.println("This is private constructor test.");
}
private int x = 10;
private void method1() {
System.out.println("This is private method test.");
}
public Test1(String test) {
System.out.println("This is public constructor test.");
}
public int xx = 20;
public void methodA() {
System.out.println("This is public method test.");
}
}
Class2
: trying to use data from Class1
public class Test2 {
public static void main(String[] args) {
Test1 a = new Test1(); //you get compilation error when
you try to create object of private constructor.
Test1 b = new Test1("ok"); //you do not get compilation error because this is public.
b.method1(); //getting
compilation error
b.methodA(); //Not getting compilation error
System.out.println("private variable " +b.x);//getting compilation error
System.out.println("public variable " +b.xx);//Not getting compilation error
}
}
Note: When you comment out the statements showing error in
class2, and run, you get below output:
This is
public constructor test.
This is
public method test.
public variable 20
Protected
They use the keyword "protected". They are accessible within
the package and if the class is extended(inherited) by another
class, outside the package, then they are accessible in the child class as
well. Through inheritance, protected data are accessible outside the package.
While
running inside same package "packageA":
Class1:
package packageA;
public class Test1{
protected Test1() {
System.out.println("This is protected constructor test.");
}
protected int x = 10;
protected void method1() {
System.out.println("This is protected method test.");
}
}
Class2 inside same package:
package packageA;
public class Test2 {
public static void main(String[] args) {
Test1 a = new Test1();
a.method1();
System.out.println("Protected variable " +a.x);
}
}
Output:
This is
protected constructor test.
This is
protected method test.
Protected variable 10
Getting
compilation error when tried to use the data in different/outside package "packageB":
package packageB;
import packageA.Test1;
public class Test3 {
public static void main(String[] args) {
Test1 a = new Test1(); // getting compilation error
a.method1(); // getting
compilation error
System.out.println("Protected variable " +a.x); // getting compilation error
}
}
Now,
let's apply inheritance and use the data in the outside package "packageB".
Now, we should not get compilation error :
package packageB;
import packageA.Test1;
public class Test3 extends Test1{
public static void main(String[] args) {
Test3 a = new Test3();
a.method1();
System.out.println("Protected variable " +a.x);
}
}
Output:
This is
protected constructor test.
This is
protected method test.
Protected variable 10
Default
When you do not mention the access modifier in constructor, method or variable, then it is default access
modifier. The data is accessible within the package only where the data is
created.
While
running inside same package "packageA":
Class1:
package packageA;
public class Test1{
Test1() {
System.out.println("This is default constructor test.");
}
int x = 10;
void method1() {
System.out.println("This is default method test.");
}
}
Class2 inside the same package "packageA".
package packageA;
public class Test2 {
public static void main(String[] args) {
Test1 a = new Test1();
a.method1();
System.out.println("Default variable " +a.x);
}
}
Output:
This is
default constructor test.
This is
default method test.
Default
variable 10
Getting
compilation error when tried to use the data in different package "packageB":
package packageB;
import packageA.Test1;
public class Test3 {
public static void main(String[] args) {
Test1 a = new Test1(); //getting compilation error
a.method1(); //getting
compilation error
System.out.println("Protected variable " +a.x); //getting compilation error
}
}
Let's
apply inheritance and try to use the data in different package "packageB", still we should get compilation
error.
package packageB;
import packageA.Test1;
public class Test3 extends Test1 { //getting compilation error
public static void main(String[] args) {
Test3 a = new Test3();
a.method1(); //getting
compilation error
System.out.println("Protected variable " +a.x); //getting compilation error
}
}
No comments:
Post a Comment