Home » Java programming language

Access specifiers in Java

Java access specifiers: Here, we are going to learn about the various access specifiers (private, public, default and protected) in Java.
Submitted by Preeti Jain, on July 25, 2019

Java access specifiers

We know that there are few access specifiers in java. We will explore access specifiers one by one. We will study first what is access specifiers in java? and then after we will study what are the uses of these access specifiers.

Access specifiers

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 access specifiers

In java, there are four types of access specifiers 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) public access specifiers

  • "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:

// 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) protected access specifiers

  • "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:

// 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) default access specifiers

  • "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:

// 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) private access specifiers

  • "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:

// 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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.