CoreJavaTutorials
Contribute Us | Ask Question |
z

Access Modifiers in Java

Access Modifier as name implies is used to show how and where to access the classes and methods in a program. Mostly Programming language have three access modifiers, but java have four access modifiers.
A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, its constructors, fields and methods. Java access modifiers are also sometimes referred to in daily speech as Java access specifiers, but the correct name is Java access modifiers.
Classes, fields, constructors and methods can have one of four different Java access modifiers:

1) private
2) default (package)
3) protected
4) public

Each of these Java access modifiers will be covered in the following sections. Assigning an access modifier to a class, constructor, field or method is also sometimes referred to as "marking" that class, constructor, field or method as that which the access modifier specifies.

For example, apply the Java access modifier private to a method would be referred to as marking the method as private means it is called a private method.

Access Level of Access Modifiers

Understanding all java access modifiers

Level of reached of access modifiers in a simple table.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYNNN
DefaultYYNN
ProtectedYYYN
PublicYYYY

public-:

It have highest level of accessibility and can be access anywhere ,in any package and by any program.

Example of public access modifier

//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello

protected -:

It can be accessed within a same package or in any other (different) package but can be access only through inheritance.

Example of protected access modifier

//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello parent");}
}
//save by B.java package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Output:Hello parent

default-:

If we does not apply any access modifier it will be called as default access modifier.
for instance
void show() this method is treated as default access modifier.
Default access Modifier can be accessed same class or within same package .

Example of default access modifier

//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello default");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//compile time error beacause class A has default access modifier
obj.msg();//compile time error
}
}

private -:

It can be accessed within same class only .It can not be used by other class or package.

Example of private access modifier

//save by Simple.java
class A{
private int data=50;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}

Where Access Modifiers can be used?

Class -Only public access modifier can be used on class.
private can be used in nested class or inner class
methods ,constructors, fields - All modifiers(public,private,default,protected) can be apply.