Access Modifiers in Java - public, protected, private, default
Access Modifiers in Java is used to define the scope of a variable, method or a class. There are the following types of access Modifiers:
- public
- private
- protected
- default
Consider the following table:
ACCESS MODIFIERS | WITHIN CLASS | WITHING PACKAGE | OUTSIDE PACKAGE (BY SUBCLASS) |
OUTSIDE PACKAGE |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
default | Yes | Yes | No | No |
private | Yes | No | No | No |
1. public:
This modifier is specified using public keyword.
If a field, method or class is declared with public keyword then it can be accessed from everywhere in the program.
2. private
This modifier is specified using private keyword.
If a field or method is declared with private keyword, then it will be visible only within the class where they are declared. And no other class in the same package or different package can access the private member.
Example:
class MyClass{
private String name;
public MyClass(String name){
this.name = name;
}
private sayHello(){
System.out.println("Hello" + this.name);
}
}
public class Program{
public static void main(String args[]){
MyClass m = new MyClass("Hitesh");
//System.out.println(m.name); shows an error, as name is not visible since it is a private variable
//m.sayHello() will also be not visible and so produces an error
}
}
Note: To access a private field, use setters and getters method.
3. protected:
This modifier is specified using protected keyword.
If a field or method is declared using the protected keyword, then it will be accessible within the same package or sub classes of different package.
Example:
package mypackage1;
class MyClass{
public String name;
protected int id;
public MyClass(String name, int id){
this.name= name;
this.id = I'd;
}
}
class Program{
public static void main(String args[]){
MyClass m = new MyClass("A", 1);
System.out.println(m.name); //Output: A
System.out.println(m.id); //Output: 1
//Both public and protected members are visible
}
}
package mypackage2;
import mypackage1.*;
class Tester extends MyClass{
public Tester(String name, int id)
{
super(name, id);
}
//Both public and protected member will be visible inside subclass of different package
public void show(){
System.out.println(this.name +":"+ this.id);
}
}
public class TestClass{
public static void main(String args[]){
MyClass m = new MyClass("B", 2);
System.out.println(m.name); // public member is visible
System.out.println(m.id); //Compilation error, id is not visible because it is protected and TestClass is not a subclass of MyClass class.
}
}
4. default:
When no access modifier is specified for a field, method or class, then it is said to be a default access.
The fields, methods and classes declared as default can only be accessible within the same package.
Example:
class MyClass{//default class
public String name;
int id; // default field
public MyClass(String name, int id){
this.name = name;
this.id = id;
}
void sayHello(){
//Default method
System.out.println("Hello"+name);
}
}
Comments
Post a Comment