Access Modifiers

Access Modifiers :
They 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 when 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
2
3
4
5
6
7
8
package com.kb.accessmodifiers;
 
class Person{
    private String name="kb";
    private void printName(){
        System.out.println("name is "+name);
    }
}
package com.kb.accessmodifiers;

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

Example1.java

1
2
3
4
5
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
}
    
}
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
2
3
4
5
6
7
8
package com.kb.accessmodifiers;
 
class PrivateConstrcutor{
    
    private PrivateConstrcutor() {
        
    }
}
package com.kb.accessmodifiers;

class PrivateConstrcutor{
	
	private PrivateConstrcutor() {
		
	}
}

Example2.java

1
2
3
4
5
6
7
8
public class Example2 {
 
    public static void main(String[] args) {
 
        PrivateConstrcutor pc = new PrivateConstrcutor();//compile time error
    }
 
}
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
2
3
4
5
package com.kb.accessmodifiers1;
 
 class AccessModifier1 {
 
}
package com.kb.accessmodifiers1;

 class AccessModifier1 {

}

Below class tries to access default class in this new package

1
2
3
4
5
6
7
8
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
    }
 
}
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).

Lets see an Example

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

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

}

Here class is public, 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
2
3
4
5
6
7
8
9
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
    }
 
}
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()’

So to access protected member the above class must be inherited like below

1
2
3
4
5
6
7
8
9
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
    }
 
}
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.

Lets see an Example

Below class is public and has public method as well.

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

Note:
Whenever we override a method in the sub class, the sub class method should not narrow the access modifier specified in the super class however it can widen it.

This means if method is protected in super class then it cannot be private/default in subclass however it can be public in subclass.

Example

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

Show() method is protected in Super class but cannot become private in subclass.

So we must change it to protected or public.

About the Author

Karibasappa G C (KB)
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