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());
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com