Access Modifiers in Java

In this article, we will learn about Access Modifiers in Java with some code examples.

Introduction

Access modifiers in Java, as the names suggest, it restrict the scope of a class, constructor, variable, method, or data member. In Java, there are four types of access modifiers.

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

Java Access Modifiers Table:

DefaultPrivateProtectedPublic
Same ClassYesYesYesYes
Same Package SubclassYesNoYesYes
Same Package Non-SubclassYesNoYesYes
Different Package SubclassNoNoYesYes
Different Package Non-SubclassNoNoNoYes

Java Default Access Modifier

When no access modifier is given for a class, method, or data member, it is said to use the default access modifier.

  • Data members, classes, and methods that are not defined with any access modifiers, i.e. with the default access modifier, are only available inside the same package.

Code Example 01: In this example, we will construct two packages with the default access modifiers. And we will try to access a class from one package from a class in the second package.

// Java program to illustrate default modifier
package one;
 
// Class first is having Default access modifier
class First
{
    void display()
    {
        System.out.println("Welcome to softhunt.net");
    }
}

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

Output:

Compile time error

Java Private Access Modifier

The private access modifier is specified using the keyword private.

  • Private methods and data members are only available within the class in which they are declared.
  • These members will be inaccessible to any other class in the same package.
  • Private cannot be defined for top-level classes or interfaces because
    • Private indicates “only accessible within the enclosing class”.
    • The term “protected” refers to “only viewable within the enclosing class and any subclasses”.

As a result, in terms of class application, these modifiers only apply to nested classes and not to top-level classes.

Code Example 01: Within the same package one, we will construct two classes A and B in this example. We’ll designate a method in class A as private and observe what happens when we try to access it from class B.

// Java program to illustrate error while
 using class from different package with
 private modifier
package one;

class A
{
private void display()
	{
		System.out.println("Welcome to softhunt.net");
	}
}

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();

Java Protected Access Modifier

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.

Code Example 01: In this case, we’ll make two packages, one and two. Class A in package one is made public so that it may be accessed in package two. The method displayed in class A is protected, and class B is inherited from class A; this protected function is then accessible by constructing an object of class B.

// Java program to illustrate
 protected modifier
package one;

// Class A
public class A
{
protected void display()
	{
		System.out.println("Welcome to softhunt.net");
	}
}
// Java program to illustrate
 protected modifier
package two;
import one.*; // importing all classes in package one
 
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
    B obj = new B();
    obj.display();
}
   
}

Output:

Welcome to softhunt.net

Java Public Access Modifier

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.

Code Example 01:


// Java program to illustrate
 public modifier
package one;
public class A
{
public void display()
    {
        System.out.println("Welcome to softhunt.net");
    }
}
package two;
import one.*;
class B {
    public static void main(String args[])
    {
        A obj = new A();
        obj.display();
    }
}

Output:

Welcome to softhunt.net

Points to Remember

  • 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.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. Java Stack Class

Leave a Comment