Java Access Modifiers (AKA access specifiers) Tutorial

In this section we will learn what the access modifiers are and how to use them in Java.

What is Access Modifiers in Java: (AKA Access Specifiers in Java)

Access specifiers which also known as access modifiers are used to set the level of access for classes, attributes and methods.

For example, a method that has `public` accessibility means it can be accessed from objects, methods in the same class, and derived classes that inherit from the owner class.

Types of access modifiers in Java

In short there are four types of access specifiers:

– private

– public

– protected

– default

Java `private` access modifier

Attributes and methods of a class can be declared as `private`. A member of a class that is declared as private is only accessible from within the body of that class only. That means no objects and no derived classes can access such member (attribute or method).

Note: we can’t set the access specifier of a class as `private`. That would be a compile time error.

Example: using `private` access modifier in Java

public class Person {
    private String name;
    private String lastName;

    protected String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Both the `name` and `lastName` attributes (variables) are declared as private and so only methods in this class can access these members.

Java `public` access modifier

A class and attributes and methods of a class can all be declared as `public`.

For a class to be `public` means objects can be created from that class. Also the class is accessible from other packages. 

If attributes and methods of a class are declared as public then objects that are created from that class, can access and work with these members.

Note: it’s a good practice to set the attributes of a class as `private` and only set the methods of that class as `public` so that the access to attributes can only be possible via methods.

This way and via methods of a class we can control and filter what range of values are allowed to be stored in the attributes.

Example: using `public` access modifier in Java

class Main{

	public static void main(String[] args) {
        sMeth();
    }
    public static void sMeth(){
        String name = "John Doe";
        System.out.println(name);
    }
   
}

Java `protected` access modifier

The use of `protected` access modifier can be applied to methods and attributes of a class.

When applying the `protected` access modifier to the members of a class, other than the members of a class, a protected member is accessible from the derived classes in the same and other packages and also only objects that are created in the same package where the class is defined.

In the inheritance section, we will explain this access specifier in greater details.

Java Protected vs Private Access Modifiers

A member of a class that is declared as private is only accessible within the body of the class itself. Basically, there’s no way to directly access such member from outside of the class itself.

On the other hand, a `protected` member of a class is still accessible within a child class if there’s one and inherited from this class (the one with protected members).

Note: because we didn’t touch the inheritance topic yet, it’s too soon for you to understand this difference. But after the inheritance section, you’ll see this difference easily.

Java `default` access modifier

If we don’t use either of the mentioned access specifiers for a class, attributes or methods, they will have the default access specifier (also called package level) which means they are accessible only within the package that they are defined. No other packages can access classes, attributes and methods with default access specifier

Example: using `default` access modifier in Java

In your IDE create a package named `packOne` and inside that package, create a class named `Person` and paste the code below into that class:

class Person {
    String name;
    String lastName;

    String getName() {
        return name;
    }
    void setName(String name) {
        this.name = name;
    }
    String getLastName() {
        return lastName;
    }
    void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Note: as you can see, the `Person` class, attributes and methods of the class all have default access specifier.

Now create another package named `packTwo` and create a class in there named `Simple` and paste the code below into that class:

import packOne.Person;
public class Simple {
    public static void main(String[] args) {
        Person person = new Person();
    }
}

Now if we compile and run the example above, we will get compile time error which is saying:

Error:(2, 15) java: packOne.Person is not public in packOne; cannot be accessed from outside package

As you can see, the `Person` class that is in `packOne` package, has package level access specifier and so because we tried to access this class in a different package, we got error.

Now if we change the accessibility of the class to `public` we won’t get compile time error but because the members of the class are still having the default access specifier, we can’t access none of those members after we’ve created an object from this class.

`public` access modifier Example:

First change the access specifier of the `Person` class to `public`:

public class Person{…}

Now change the code of the `Simple` class like this:

public class Simple {
    public static void main(String[] args) {
        Person person = new Person();
        person.getName();
    }
}
Output: 
Error:(6, 15) java: getName() is not public in packOne.Person; cannot be accessed from outside package

As you can see, this time we could create an object of type `Person` but because we tried to access the `getName()` method and the access specifier of this method is still set to default, we got compile time error.

To get rid of this error, we need to change the access specifier of this method to `public` as well.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies