Skip to content

Java Access Modifiers | Default, Public, Protected & Private

  • by

In Java, you can set an access level or restrict the scope of Java Variables, methods, class, constructor, and data member. To set a scope of them you have to use Java Access Modifiers in your program code.

Java Access Modifiers ,Default, Public, Protected & Private tutorial example

Java Access Modifiers Types

There are 4 types of access levels in Java

  1. private: Visible only in the same class
  2. default: Visible within the package level
  3. protected: Within package and outside the package but need to use inheritance then only.
  4. public: Visible to the Everywhere

Chart all Java Access Modifiers

Here is a simple table of all Java Access Modifiers for a much better understanding.

Access ModifierWithin classWithin packageOutside package by subclass onlyOutside a package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes

Let’s Start overview of Access modifiers with simple examples:

We are adding the one-one example of all java modifiers for understanding, there is a lot of examples to do the same. But always start with the easy one.

1. Java Private Access Modifier

Most restricted access modifier is Private, access only within the class level.

Let’s try the example of creating a 2 class A and B, and try private data member of A class by B class. It should be a compile-time error. Check out this Private Access Modifier Example.

java Private Access Modifier Example
class A {
    private int a = 40;

    private void message() {
        System.out.println("Hello java");
    }
}
public class B {
    public static void main(String args[]) {
        A obj = new A();

        //Compile Time Error
        System.out.println(obj.a);
        obj.message();
    }
}

Output & Screenshot:

Error:(7, 12) java: message() has private access in A
Error:(6, 31) java: a has private access in A

Private Access Modifier Example and error

2. Java Default Access Modifier

The default modifier is accessible only within the package only. If no modifier has to define then it’s default one.

Let’s Create Example of default access modifier, Create 2 packages – eye.hunts and other. Trying to accessing the A class from outside its a package, since A class is default one, so it can’t be accessed from outside the package.

package eye.hunts;

class A {
    public void message() {
        System.out.println("Hello");
    }
}
package other;

import eye.hunts.A;

class B {
    public static void main(String args[]) {
        A obj = new A();
        obj.message();
    }
}

Output: Error:(3, 17) java: eye.hunts.A is not public in eye.hunts; cannot be accessed from outside package ......

Java Default Access Modifier example and error

3. Java Protected Access Modifier

Java Protected access modifier is accessible within the package and outside the package but through inheritance only.

Let’s see the example of a Protected access modifier. Using the above example and just changed the Message method of this package is declared as protected, so it can be accessed from outside the class only through inheritance. And class A is public.

package eye.hunts;

public class A {
    protected void message() {
        System.out.println("Hello");
    }
}
package other;

import eye.hunts.A;

class B extends A {
    public static void main(String args[]) {
        B obj = new B();
        obj.message();
    }
}

Output: Hello

4. Java Public Access Modifier

The Public Access modifier has a wider access level as compare to others, it can access everywhere. See the example of it.

Java Public Access Modifier example
package eye.hunts;

public class A {
    public void message() {
        System.out.println("Hello");
    }
}
package other;

import eye.hunts.A;

class B {
    public static void main(String args[]) {
        A obj = new A();
        obj.message();
    }
}

Output: Hello

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Access Modifiers – Default, Public, Protected & Private Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *