in , ,

Java Access Modifiers

Java Access Modifiers
Java Access Modifiers

Java Access Modifiers

In this tutorial, we will find out about the Java Access Modifier, its sorts, and how to use them with the help of examples.

What are Access Modifiers?

In Java, access modifiers are used to set the openness (perceivability) of classes, interfaces, variables, methods, constructors, data members, and setter methods. For instance,

class Animal {
    public void method1() {...}

   private void method2() {...}
}

In the above example, we have declared 2 methods: method1() and method2(). Here,

method1 is public – This means it can be accessed by other classes.
method2 is private – This means it can not be accessed by other classes.
Note the keyword public and private. These are access modifiers in Java. They are also known as visibility modifiers.

Note: You cannot set the access modifier of getters method


Types of Access Modifier

Before you learn about types of access modifiers, make sure you know about Java Packages.

There are four access modifiers keywords in Java and they are:

ModifierDescription
Defaultdeclarations are visible only within the package (package private)
Privatedeclarations are visible within the class only
Protecteddeclarations are visible within the package or all subclasses
Publicdeclarations are visible everywhere

Default Access Modifier

If we don’t expressly determine any entrance modifier for classes, methods, variables, and so forth, at that point by default the default access modifier is thought of. For instance,

package defaultPackage;
class Logger {
    void message(){
        System.out.println("This is a message");
    }
}

Here, the Logger class has the default access modifier. Furthermore, the class is noticeable to all the classes that have a place with the defaultPackage package. Be that as it may, in the event that we attempt to use the Logger class in another class outside of defaultPackage, we will get a compilation mistake.


Private Access Modifier

At the point when variables and methods are announced private, they can’t be accessed outside of the class. For instance,

class Data {
    // private variable
    private String name;
}

public class Main {
    public static void main(String[] main){

        // create an object of Data
        Data d = new Data();

        // access private variable and field from another class
        d.name = "worldofitech";
    }
}

In the above example, we have declared a private variable named name and a private method named display(). When we run the program, we will get the following error:

Main.java:18: error: name has private access in Data
        d.name = "worldofitech";
         ^

The error is generated because we are trying to access the private variable and the private method of the Data class from the Main class.

You may be thinking about imagining a scenario where we have to get to those private variables. For this situation, we can use the getters and setters method. For instance,

class Data {
    private String name;

    // getter method
    public String getName() {
        return this.name;
    }
    // setter method
    public void setName(String name) {
        this.name= name;
    }
}
public class Main {
    public static void main(String[] main){
        Data d = new Data();

        // access the private variable using the getter and setter
        d.setName("worldofitech");
        System.out.println(d.getName());
    }
}

Output

The name is worldofitech

In the above example, we have a private variable named name. So as to get to the variable from the external class, we have used methods: getName() and setName(). These strategies are called getter and setter in Java.

Here, we have used the setter strategy (setName()) to appoint an incentive to the variable and the getter technique (getName()) to access to the variable.

We have used this keyword inside the setName() to refer to the variable of the class. To learn more on this keyword, visit Java this Keyword.

Note: We cannot declare classes and interfaces private in Java. However, the nested classes can be declared private. To learn more, visit Java Nested and Inner Class.


Protected Access Modifier

At the point when method and data members are pronounced protected, we can get to them inside a similar package just as from subclasses. For instance,

class Animal {
    // protected method
    protected void display() {
        System.out.println("I am an animal");
    }
}

class Dog extends Animal {
    public static void main(String[] args) {

        // create an object of Dog class
        Dog dog = new Dog();
         // access protected method
        dog.display();
    }
}

Output

I am an animal

In the above example, we have a protected method named display() inside the Animal class. The Animal class is inherited by the Dog class. To learn more about inheritance, visit Java Inheritance.

We then created an object dog of the Dog class. Using the object we tried to access the protected method of the parent class.

Since protected methods can be accessed from the child classes, we are able to access the method of Animal class from the Dog class.

Note: We cannot declare classes or interfaces protected in Java.


Public Access Modifier

At the point when methods, variables, classes, etc are pronounced public, at that point we can access them from anyplace. The public access modifier has no extension limitation. For instance,

// Animal.java file
// public class
public class Animal {
    // public variable
    public int legCount;

    // public method
    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Animal animal = new Animal();

        // accessing the public variable
        animal.legCount = 4;
        // accessing the public method
        animal.display();
    }
}

Output

I am an animal.
I have 4 legs.

Here,

The public class Animal is accessed from the Main class.
The public variable legCount is accessed from the Main class.
The public method display() is accessed from the Main class.


Access Modifiers Summarized in one figure

Access modifiers are mainly used for encapsulation. I can help us to control what part of a program can access the members of a class. So that misuse of data can be prevented, visit Java Encapsulation.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java String

Java String

Java this Keyword

Java this Keyword