Access Modifiers in JAVA

As we have seen in the encapsulation that it provides a controlled access to the private members of the class. So, how exactly can we define a private member in the class and can protect it from the outside access or its scope, and what if we want to access the private data outside of the class? That is actually possible with Access Modifiers in JAVA.

Access modifiers are those which are applied before data members or methods of a class. These are used where to access and where not to access the data members or methods. In JAVA programming these are classified into four types:

  • Private
  • Default (not a keyword)
  • Protected
  • Public

Access modifiers are always used for, reusing the features within the package and access the package between class to class, interface to interface and interface to a class. Access modifiers provide features accessing and controlling mechanism among the classes and interfaces. An interface is an important feature which we will learn later.

access-modifiers-image

Rules for access modifiers:java-access-modifiers-table

private: Private members of the class are not accessible anywhere in the program. These are only accessible within the class. Private members are also called class level access modifiers.

Example

class Hello
{
private int a=20;  
private void show()
{
System.out.println("Hello java");
}  
}  
  
public class Demo
{  
 public static void main(String args[])
 {  
  Hello obj=new Hello();  
  System.out.println(obj.a); //Compile Time Error, you can't access private data  
  obj.show();   //Compile Time Error, you can't access private methods
 }
}

public: Public members of any class are accessible anywhere in the program in the same class and outside of class, within the same package and outside of the package. Public members are also called universal access modifiers.

Example

class Hello
{
public int a=20;  
public void show()
{
System.out.println("Hello java");
}  
}  
  
public class Demo
{  
 public static void main(String args[])
 {  
  Hello obj=new Hello();  
  System.out.println(obj.a);  
  obj.show();
 }
}

Output

20
Hello Java

protected: Protected members of the class are accessible within the same class and another class of the same package and also accessible in inherited class of another package. Protected are also called derived level access modifiers.

In below the example we have created two packages pack1 and pack2. In pack1, class A is public so we can access this class outside of pack1 but method show is declared as a protected so it is only accessible outside of package pack1 and only through inheritance.

Example

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

Output

Hello Java

default: Default members of the class are accessible only within the same class and another class of the same package. The defaults are also called package level access modifiers.

Example

//save by A.java  
package pack;  
class A
{  
 void show()
{
System.out.println("Hello Java");
}  
}  
//save by B.java  
package pack2;  
import pack1.*;  
class B
{  
 public static void main(String args[])
 {  
  A obj = new A(); //Compile Time Error, can't access outside the package
  obj.show();   //Compile Time Error, can't access outside the package  
 }  
}  

Output

Hello Java

Note: private access modifier is also known as native access modifier, default access modifier is also known as package access modifier, protected access modifier is also known as an inherited access modifier, public access modifier is also known as universal access modifier.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: