Java - Access Specifiers (Modifiers) With Examples

Java access specifiers: Here, we are going to learn about the various access specifiers (private, public, default and protected) in Java. By Preeti Jain Last updated : March 23, 2024

We know that there are few Java - Access Specifiers (Modifiers) With Examples. We will explore access specifiers one by one. We will study first what is Java - Access Specifiers (Modifiers) With Examples? and then after we will study what are the uses of these access specifiers.

Java - Access Specifiers (Modifiers)

Access specifiers are the keywords like "public", "protected", "default" and "private" which has its special meaning in java.

It defines the access scope of the variable, methods, and classes and here the access scope means the area or space where a variable or classes or methods are accessible.

Types of Java Access Specifiers (Modifiers)

In Java, there are four types of access specifiers (modifiers) and the name of these access specifiers are given below:

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

Now, with the help of example, we will describe each access specifiers one by one in java.

1) Java public Access Specifier (Modifier)

  • "public" is the keyword which is introduced in java.
  • The access scope of the "public" is everywhere like in all classes and methods as well.
  • If we prefixed "public" keyword with any class, variable or method then it can be accessed by any class or methods.

Example of Java public Access Specifier (Modifier)

// ClassA save in package1

package package1;

public class ClassA {
    public void display() {
        System.out.println("We are in Java World");

    }
}

package package2;

// importing package1 because we are using ClassA of package1 
import package1.ClassA;

// class ClassB save in package2   
class ClassB {
    public static void main(String args[]) {
        ClassA ca = new ClassA();
        ca.display();
    }
}

Output

We are in Java World

2) Java protected Access Specifier (Modifier)

  • "protected" is the keyword which is introduced in java.
  • The access scope of the "protected" is not everywhere and it is accessible in the same class or its child class or in all those classes which are defined in the same package.
  • If we prefixed "protected" keyword with any class, variable or method then it can be accessed by the same class or its child classes or all the classes which are defined in the same package.

Example of Java protected Access Specifier (Modifier)

// ClassA save in package1  
package package1;
public class ClassA {
    protected void display() {
        System.out.println("We are in Java World");
    }
}

package package2;

// importing package1 because 
// we are using ClassA of package1 
import package1.ClassA;
// class ClassB save in package2   
class ClassB extends ClassA {
    public static void main(String args[]) {
        ClassA ca = new ClassA();
        ca.display();
    }
}

Output

We are in Java World

3) Java default Access Specifier (Modifier)

  • "default" is the keyword which is introduced in java.
  • The access scope of the "default" is not everywhere.
  • It is not mandated to prefixed "default" keyword with any class, variable or method because by default class, variable or method is default public in java and it can be accessed by all those classes which are defined in same package only.

Example of Java default Access Specifier (Modifier)

// ClassA save in package1  
package package1;
class ClassA {
    void display() {
        System.out.println("We are in Java World");
    }
}

package package2;
// importing package1 because we are using 
// ClassA of package1 
import package1.ClassA;

// ClassB save in package2   
class ClassB {
    public static void main(String args[]) {

        /*  Here we will get compiletime error because 
            ClassA is not public so we can't access 
            this class outside the package 
        */
        ClassA ca = new ClassA();
        ca.display();
    }
}

Output

ClassB.java:3: error: ClassA is not public in package1; 
cannot be accessed from outside package
import package1.ClassA;
               ^
ClassB .java:10: error: cannot find symbol
ClassA ca= new ClassA();
 ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:10: error: cannot find symbol
 ClassA ca= new ClassA();
           ^
  symbol:   class ClassA
  location: class ClassB
3 errors

4) Java private Access Specifier (Modifier)

  • "private" is the keyword which is introduced in java.
  • The access scope of the "private" is not everywhere.
  • If we prefixed "private" keyword with any variable or method then it can be accessed only in the same class.

Example of Java private Access Specifier (Modifier)

// ClassA save in package1  
package package1;
class ClassA {
    private void display() {
        System.out.println("We are in Java World");
    }
}

package package2;
// importing package1 because we are using 
// ClassA of package1 
import package1.ClassA;

// ClassB save in package2   
public class ClassB {
    public static void main(String args[]) {

        /*  Here we will get compiletime error because 
            ClassA method is private so we can't access 
            this method outside the class and other package too
        */
        ClassA ca = new ClassA();
        ca.display();
    }
}

Output

ClassB.java:3: error: ClassA is not public in package1; 
cannot be accessed from outside package
import package1.ClassA;
               ^
ClassB .java:10: error: cannot find symbol
ClassA ca= new ClassA();
 ^
  symbol:   class ClassA
  location: class ClassB
ClassB.java:10: error: cannot find symbol
 ClassA ca= new ClassA();
           ^
  symbol:   class ClassA
  location: class ClassB
3 errors


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.