Access Modifiers in Java





Access Protection:

  • Classes and packages are both means of encapsulating and containing the namespace and scope of variables and methods.

  • Packages act as containers for classes and other subordinate packages.

  • Classes act as containers for data and code. The class is Java’s smallest unit of abstraction.

  • Because of the interplay between classes and packages, Java addresses four categories of visibility for class members:

    • Subclasses in the same package
    • Non-subclasses in the same package
    • Subclasses in different packages
    • Classes that are neither in the same package nor subclasses

Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors.

There are 4 types of Java access modifiers:

  1. private
  2. default
  3. protected
  4. public

1. private access modifier :

Anything declared private cannot be seen outside of its class.

Example :

class Test
{  
 private int n = 40; 
 private void msg() 
 {
  System.out.println("Hello");
 }  
}

public class Demo
{
 public static void main(String []ar)
 {  
   Test ob = new Test();
   System.out.println(ob.n);//Compilation Error
   ob.msg();//Compilation Error
 }  
}           

2. default access modifier :

If you don't use any modifier, it is treated as default bydefault.

it is visible to subclasses as well as to other classes in the same package.

Example :

package pack;
class Test
{  
 int n = 40;  //default
 void msg()  //default
 {
  System.out.println("Hello");
 }  
}
package mypack;
import pack.Test;
public class Demo
{
 public static void main(String []ar)
 {  
   Test ob = new Test();//Compilation Error
   System.out.println(ob.n);//Compilation Error
   ob.msg();//Compilation Error
 }  
}           

In the above example, the scope of class Test and its method msg() is default so it cannot be accessed from outside the package.


3. protected access modifier :

The protected access modifier is accessible within same package and outside the package but through inheritance only.

Example :

package pack;
class Test
{  
 protected void msg() 
 {
  System.out.println("Hello");
 }  
}
package mypack;
import pack.Test;
public class Demo extends Test
{
 public static void main(String []ar)
 {  
   Demo ob = new Demo();
   ob.msg();
 }  
}           
OUTPUT :
Hello       

4. public access modifier :

Anything declared public can be accessed from anywhere.

Example :

package pack;
public class Test 
{  
 public void msg() 
 {
  System.out.println("Hello");
 }  
}
package mypack;
import pack.Test;
public class Demo extends Test
{
 public static void main(String []ar)
 {  
   Test ob = new Test();
   ob.msg();
 }  
}           
OUTPUT :
Hello       

Point to Remember :

A class cannot be private or protected except nested class.

Next topic is encapsulation




 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved