Access Modifier

We  can control the scope of method, class, constructor, and variable  in a program or package 📦

For this we use access specifier

We can change the access level of constructors, methods, and class by applying the access modifier on it.

There are four type of access specifier in java

  1. Public
  2. Private
  3. Protected
  4. Default

Lets’ understand it one by one

Public:

Public modifier can acces any where

We can acess with in a clas

We can acces within package

We  acess outside package by sub class only

We cannot use it outside of package

Example :

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

output

Hello

notice

here package is different from package of class b

here we create an object of class A and access the msg() method of class A

program run successfully no error.


Private :

By this acess specifie we only acess with in class

We can acess with in a class

We can’t acces within package

We  can’t access outside package by sub class only

We cannot use it outside of package

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

Note :

here we create an object of class A and access the msg() method of class A

this program throw error because msg method is a private method so we can’t use in other class


Protected :

Protected is  like a public it is also accessed within a package or outside of the package by subclass only

The only difference is that it was accessible within the package and outside the package but through inheritance only.

Example:

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

For in inheritate the class we use extend key word

Here class B extend class A it mean class B can acess class a

so it creates an object of class B and access msg method of class  A

because in inheritance
class acquire all property of  there parent class


Default :

If we don’t put any acess specifie then it become defalult

It mean

It only accessible with in a class or with in a package 📦

Example:

//save by A.java  
package pack;  
class A{  
  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();//Compile Time Error  
   obj.msg();//Compile Time Error  
  }  
}  

Notice both class belong to different package and

Here access specifier is defalult

So class be can’t acess method of class A



SHARE THIS PAGE





Important Link