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
- 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
- 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
- package com.kb.accessmodifiers;
- class PrivateConstrcutor{
- private PrivateConstrcutor() {
- }
- }
package com.kb.accessmodifiers; class PrivateConstrcutor{ private PrivateConstrcutor() { } }
Example2.java
- 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.
- package com.kb.accessmodifiers1;
- class AccessModifier1 {
- }
package com.kb.accessmodifiers1; class AccessModifier1 { }
Below class tries to access default class in this new package
- 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).
Let’s see an Example
- 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"); } }
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
- 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()’
To access protected member the above class must be inherited like below
- 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.
Let’s see an Example
Below class is public and has public method as well.
- 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
- 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
Example
- class Super{
- protected void show(){
- System.out.println("show");
- }
- }
class Super{ protected void show(){ System.out.println("show"); } }
- 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 can’t become private in subclass.
So we must change it to protected or public.
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
}
}
This not compile error
That’s what even I have mentioned.
If you do not inherit then it will give compile time error.
Just read the program above this one in this article.