16 July 2023

Access Modifiers In Java - learngreen.net

 Access modifiers in Java are keywords used to specify the accessibility or visibility of classes, methods, and variables in a Java program. There are four access modifiers in Java public, protected, default and private.

Public Access Modifier:- This is the most  accessible modifier. Public access modifiers are accessible from any class or package. Public keyword is used to specify public acceess modifier.




//Example of Public Access Modifier
 package Interview;
 //Create class PB
 public class PB {
 //Create method display	 
 public void display()
 {
 System.out.println
 ("Public Access Modifier");	
 }
 public static void main(String[] args) {
 //Create object of class PB	 
 PB obj = new PB();
 //Call method display
 obj.display();
 //Output-> Public Access Modifier
	}
}

 


 import Interview.PB;
 public class PB1 {
 public static void main(String[] args) {
 PB obj = new PB();
 /* Call method display from package
  * Interview of class PB
  */
 obj.display();
 //Output-> Public Access Modifier
	}
  }

 Protected Access Modifier:- Protected access modifier are accessible within the same package and subclasses in different packages.



 /* Program is about
  * protected modifier 
  */
 package Interview;
 //Create class PT
 public class PT {
 protected void method() {
 System.out.println("Protected Method");	 
  }
 }

 


  package InterviewOne;
 /* Import class PT of package
  * Interview to call method
  * method
  */
 import Interview.PT;
 public class PT_One extends PT {
 public static void main(String[] args) {
 //Create object of class PT_One	 
 PT_One obj = new PT_One();
 //Call Method method
 obj.method();
 //Output-> Protected Method
  }
 }

 Default Access Modifier (No Keyword):-  When not any keyword is specified it is an default accesss modifier. These are accessible only within the same package.




  /* Program to describe
    * about default modifier
   */
 package Interview;
 /* create class default
  * access modifier which
  * contains method Exercise
  */
 public class DT {
 void Exercise() {
 System.out.println("Swimming"); 
  }

 }


 



  /* Program shows error
  * when we try to call 
  * default method from
  * class DT of package
  * interview to class DT1
  * of Package InterviewOne
  */
 package InterviewOne;
 import Interview.DT;
 public class DT1 {
 public static void main(String[] args) {
 DT obj = new DT();
 obj.Exercise();
 //Error-> 
 /* The method Exercise() from
  * the type DT is not visible
  */
   }
 }

Private Access Modifier:- We can access priavte access only from same class. They are not accessible from any other package or classs. They are specified using keyword private.

We can not declare super classes or interfaces as private because private denotes class is accessed only within the enclosing class and protected denotes class is accessed only within the super class and subclasses.


 


 package Interview;
 /* Program is about
  * private modifier
  * when we try to access 
  * method method from
  * PrivateOne class we do 
  * get an compilation error
  */
 //create class PrivateOne
 class PrivateOne {
 //create method method	 
 private void method(){
 System.out.println
 ("Private Modifier");	 
	}
 }
 public class Private {
 public static void main(String[] args) {
 PrivateOne obj = new PrivateOne();
 obj.method();
 //Error->
 /* The method() from the type
  * privateOne is not visible
  */
	}
 }

 

Access Modifiers In Java




No comments:

Post a Comment