Skip to content
Related Articles
Open in App
Not now

Related Articles

Access Modifiers in Java

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 02 Feb, 2023
Improve Article
Save Article

As the name suggests access modifiers in Java help to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java: 

  1. Default – No keyword required
  2. Private
  3. Protected
  4. Public

Diagram –

 

access-modifiers-in-java

  • Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default. 
    • The data members, classes, or methods that are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

Algorithm 

Here’s a basic algorithm for using access modifiers in Java:

  • Define a class: Create a class that represents the object you want to manage.
  • Define instance variables: Within the class, define instance variables that represent the data you want to manage.
  • Specify an access modifier: For each instance variable, specify an access modifier that determines the visibility of the variable. The three main access modifiers in Java are private, protected, and public.
  • Use private for variables that should only be accessible within the class: If you want to prevent access to a variable from outside the class, use the private access modifier. This is the most restrictive access modifier and provides the greatest level of encapsulation.
  • Use protected for variables that should be accessible within the class and its subclasses: If you want to allow access to a variable from within the class and its subclasses, use the protected access modifier. This is less restrictive than private and provides some level of inheritance.
  • Use public for variables that should be accessible from anywhere: If you want to allow access to a variable from anywhere, use the public access modifier. This is the least restrictive access modifier and provides the least amount of encapsulation.
  • Use accessor and mutator methods to manage access to the variables: In order to access and modify the variables, use accessor (getter) and mutator (setter) methods, even if the variables have a public access modifier. This provides a level of abstraction and makes your code more maintainable and testable.
  • In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of the second package.

Java




// Java program to illustrate default modifier
package p1;
 
// Class Geek is having Default access modifier
class Geek
{
    void display()
    {
        System.out.println("Hello World!");
    }
}


Java




// Java program to illustrate error while
// using class from different package with
// default modifier
package p2;
import p1.*;
 
// This class is having default access modifier
class GeekNew
{
    public static void main(String args[])
    {
        // Accessing class Geek from package p1
        Geek obj = new Geek();
 
        obj.display();
    }
}


Output:

Compile time error
  • Private: The private access modifier is specified using the keyword private
    • The methods or data members declared as private are accessible only within the class in which they are declared.
    • Any other class of the same package will not be able to access these members.
    • Top-level classes or interfaces can not be declared as private because
      1. private means “only visible within the enclosing class”.
      2. protected means “only visible within the enclosing class and any subclasses”

Hence these modifiers in terms of application to classes, apply only to nested classes and not on top-level classes

In this example, we will create two classes A and B within the same package p1. We will declare a method in class A as private and try to access this method from class B and see the result.

Java




// Java program to illustrate error while
// using class from different package with
// private modifier
package p1;
 
class A
{
private void display()
    {
        System.out.println("GeeksforGeeks");
    }
}
 
class B
{
public static void main(String args[])
    {
        A obj = new A();
        // Trying to access private method
        // of another class
        obj.display();
    }
}


Output:

error: display() has private access in A
        obj.display();
  • protected: The protected access modifier is specified using the keyword protected.
    • The methods or data members declared as protected are accessible within the same package or subclasses in different packages.

In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.

Java




// Java program to illustrate
// protected modifier
package p1;
 
// Class A
public class A
{
protected void display()
    {
        System.out.println("GeeksforGeeks");
    }
}


Java




// Java program to illustrate
// protected modifier
package p2;
import p1.*; // importing all classes in package p1
 
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
    B obj = new B();
    obj.display();
}
     
}


Output:

GeeksforGeeks
  • public: The public access modifier is specified using the keyword public
    • The public access modifier has the widest scope among all other access modifiers.
    • Classes, methods, or data members that are declared as public are accessible from everywhere in the program. There is no restriction on the scope of public data members.

Java




// Java program to illustrate
// public modifier
package p1;
public class A
{
public void display()
    {
        System.out.println("GeeksforGeeks");
    }
}


Java




package p2;
import p1.*;
class B {
    public static void main(String args[])
    {
        A obj = new A();
        obj.display();
    }
}


Output:

GeeksforGeeks

Important Points:

  • If other programmers use your class, try to use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants.

&list=PLqM7alHXFySENpNgw27MzGxLzNJuC_Kdj


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!