Access Modifiers

Access Modifiers :

Specifies the accessibility of members of a class (attributes /methods/constructors) based on the access modifier
we can restrict the member’s accessibility, we can decide where and all these members can be accessible to use it.

Java provides 4 types of Access Modifiers


1) private

2) default

3) protected

4) public

Non Access modifiers are static,abstract,synchronized,native,volatile,and transient etc.

1) Private Access Modifier


It specifies the accessibility within a class, and if we try to access outside the class, it gives compile time error.

So ensure to use private members only within a class.

Let’s see with an example

  1. package com.kb.accessmodifiers;
  2.  
  3. class Person{
  4.     private String name="kb";
  5.     private void printName(){
  6.         System.out.println("name is "+name);
  7.     }
  8. }
package com.kb.accessmodifiers;

class Person{
	private String name="kb";
	private void printName(){
		System.out.println("name is "+name);
	}
}


Example1.java

  1. public class Example1 {
  2. public static void main(String[] args) {
  3.     Person p1 = new Person();
  4.     System.out.println(p1.name); // Compile time error
  5.     p1.printName();//Compile time error
  6. }
  7.    
  8. }
public class Example1 {
public static void main(String[] args) {
	Person p1 = new Person();
	System.out.println(p1.name); // Compile time error
	p1.printName();//Compile time error
}
	
}


Here name and printName() are private members of a Person class and we are trying to access those members inside Example1 class so it’s not accessible.

Can we make Constrctor as private ? what happens if we make it private ?


Yes absolutely fine to make constructor as private.

If we make constructor as private, we can’t create an object of a class outside the class.

Let’s see below example

  1. package com.kb.accessmodifiers;
  2.  
  3. class PrivateConstrcutor{
  4.    
  5.     private PrivateConstrcutor() {
  6.        
  7.     }
  8. }
package com.kb.accessmodifiers;

class PrivateConstrcutor{
	
	private PrivateConstrcutor() {
		
	}
}


Example2.java

  1. public class Example2 {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         PrivateConstrcutor pc = new PrivateConstrcutor();//compile time error
  6.     }
  7.  
  8. }
public class Example2 {

	public static void main(String[] args) {

		PrivateConstrcutor pc = new PrivateConstrcutor();//compile time error
	}

}


This feature of making constructor as private helps in achieving Singleton pattern.


2) Default Access Modifier

If we don’t specify any modifier, then java adds a default modifier to it and its scope is the package where the class resides.

Hence not accessible outside the package.

Let’s see with an example

Below class is not having any access modifier and java keeps it as default package level access within this package.

  1. package com.kb.accessmodifiers1;
  2.  
  3.  class AccessModifier1 {
  4.  
  5. }
package com.kb.accessmodifiers1;

 class AccessModifier1 {

}


Below class tries to access default class in this new package

  1. package com.kb.accessmodifiers2;
  2.  
  3. import com.kb.accessmodifiers1.*;
  4. public class accessmodifiers2 {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.     AccessModifier1 accessModifier1 = new AccessModifier1(); //compile time error
  9.     }
  10.  
  11. }
package com.kb.accessmodifiers2;

import com.kb.accessmodifiers1.*;
public class accessmodifiers2 {

	public static void main(String[] args) {

	AccessModifier1 accessModifier1 = new AccessModifier1(); //compile time error
	}

}


3) Protected Access Modifier

It is accessible within and outside the package only through Inheritance

This modifier can be applied on class attributes and methods and cannot be applied on a class (because we inherit class features not class itself).

Let’s see an Example

  1. package com.kb.accessmodifiers1;
  2.  
  3. public class Protected1 {
  4.    
  5.     protected void sayHello(){
  6.         System.out.println("hello world");
  7.     }
  8.  
  9. }
package com.kb.accessmodifiers1;

public class Protected1 {
	
	protected void sayHello(){
		System.out.println("hello world");
	}

}


Above class is public, So it can be accessible anywhere and method is protected accessible only in Inheritance Hierarchy.

Let’s try to access protected member outside the package without any inheritance

  1. package com.kb.accessmodifiers2;
  2.  
  3. import com.kb.accessmodifiers1.Protected1;
  4.  
  5. public class Protected2 {
  6.  
  7.     public static void main(String[] args) {
  8.         Protected1 p1 = new Protected1();
  9.         p1.sayHello();//Compile time error
  10.     }
  11.  
  12. }
package com.kb.accessmodifiers2;

import com.kb.accessmodifiers1.Protected1;

public class Protected2 {

	public static void main(String[] args) {
		Protected1 p1 = new Protected1();
		p1.sayHello();//Compile time error
	}

}


So it gives compile time error when we try to access protected member ‘sayHello()’

To access protected member the above class must be inherited like below

  1. package com.kb.accessmodifiers2;
  2.  
  3. import com.kb.accessmodifiers1.Protected1;
  4.  
  5. public class Protected2 extends Protected1{
  6.  
  7.     public static void main(String[] args) {
  8.         Protected2 p2 = new Protected2();
  9.         p2.sayHello();//Compile time error
  10.     }
  11.  
  12. }
package com.kb.accessmodifiers2;

import com.kb.accessmodifiers1.Protected1;

public class Protected2 extends Protected1{

	public static void main(String[] args) {
		Protected2 p2 = new Protected2();
		p2.sayHello();//Compile time error
	}

}


4) Public Access Modifier

This can be accessible anywhere in the application and has no restriction to access.

Let’s see an Example

Below class is public and has public method as well.

  1. package com.kb.accessmodifiers1;
  2.  
  3. public class Public1 {
  4.    
  5.     public void sayHello(){
  6.         System.out.println("Hello world");
  7.     }
  8.  
  9. }
package com.kb.accessmodifiers1;

public class Public1 {
	
	public void sayHello(){
		System.out.println("Hello world");
	}

}


Above class and its method is accessible in the below class

  1. package com.kb.accessmodifiers2;
  2.  
  3. import com.kb.accessmodifiers1.Public1;
  4.  
  5. public class Public2 {
  6.  
  7.     public static void main(String[] args) {
  8.         Public1 p1 = new Public1();
  9.         p1.sayHello();
  10.     }
  11. }
package com.kb.accessmodifiers2;

import com.kb.accessmodifiers1.Public1;

public class Public2 {

	public static void main(String[] args) {
		Public1 p1 = new Public1();
		p1.sayHello();
	}
}


Access modifier can be remembered like below

Access_modifier

Example

  1. class Super{
  2.    
  3.     protected void show(){
  4.         System.out.println("show");
  5.     }
  6. }
class Super{
	
	protected void show(){
		System.out.println("show");
	}
}
  1. class SubClass extends Super{
  2.     @Override
  3.     private void show() { //Compile time error
  4. System.out.println("show");
  5.             }
  6. }
class SubClass extends Super{
	@Override
	private void show() { //Compile time error
System.out.println("show");
			}
}


Show() method is protected in Super class but can’t become private in subclass.

So we must change it to protected or public.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on