Java Access Modifiers


In Java language, there are two groups in modifiers. Such as access modifier and non-access modifier.

The access modifiers classified into four types:

  • private
  • default
  • protected
  • public

Access modifiers

Access modifiers Private Default Protected Public
Within Same class Yes Yes Yes Yes
Same Package Derived Class No Yes Yes Yes
Same Package non-derived Class No Yes Yes Yes
Different Package Derived Class No No Yes Yes
Different Package non-derived Class No No No Yes

Private Access Modifier

In private access modifier, you can access within class only.

Example:

 class Book{  
	private String Bookname;   
	public String getName()
    {  
		return Bookname;  
	}   
	public void setName(String Bookname)
    {  
		this.Bookname=Bookname; 
	}  
}  
public class AccessmodifierPrivate {  
	public static void main(String[] args){  
		Book b=new Book(); 
		b.setName("Onlinetpoint");  
		System.out.println(b.getName());  
	}  
}  

Default Access Modifier

In default access modifier, you can access within package only. It need not give any access modifier.

Example:

 interface Roll
{
	int rollno = 10;  
}
interface Name
{
	String name = "Sundarrajan"; 
	void showdata();  
}
class Student implements Name,Roll
{ 	
	public void showdata()
    {
    	System.out.println(rollno+" "+name); 
	} 
} 
public class Accessmodifierdefault{  
    public static void main(String args[])
    {  
        Student s1=new Student();
        s1.showdata(); 
    }
}

Protected Access Modifier

In protected access modifier, you can access within package and different package of the derived class.

Example:

class Book{  
	protected String Bookname;  
	//getter method for name  
	public String getName()
    {  
		return Bookname;  
	}  
	//setter method for name  
	public void setName(String Bookname)
    {  
		this.Bookname=Bookname; 
	}  
}  
public class Accessmodifierprotected{  
	public static void main(String[] args){  
		Book b=new Book(); 
        //setting the value for name field
		b.setName("Onlinetpoint");  
		//getting value for name field  
		System.out.println(b.getName());  
	}  
}  

Public Access Modifier

In public access modifier, you can access anywhere.

Example:

 class Book{  
	String Bookname;  
	//getter method for name  
	public String getName()
    {  
		return Bookname;  
	}  
	//setter method for name  
	public void setName(String Bookname)
    {  
		this.Bookname=Bookname; 
	}  
}  
public class AccessmodifierPublic{  
	public static void main(String[] args){  
		Book b=new Book(); 
        //setting the value for name field
		b.setName("Onlinetpoint");  
		//getting value for name field  
		System.out.println(b.getName());  
	}  
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.