The 4 Access Modifiers Explained in Java

Access modifiers are keywords placed before attributes, methods, or classes to manage how they’re accessed. They restrict which methods, classes, or packages can use the modified data.

Access modifiers are also sometimes referred to as visibility modifiers. This is an intuitive way of saying that they describe how visible certain parts of a program are to other components that may wish to access them.

Visibility modifiers enable you to limit how programmers access given classes. This actually fulfills a key principle of object-oriented programming—encapsulation.

These are the four access modifiers used in Java:


Default

When you don’t explicitly define a modifier, the Java compiler will use the default visibility access. At this access level, only classes in the same package as the defined class can access its variables or methods.

Related: Learn How to Create Classes in Java

The default modifier also applies to classes, not just its members. It gives the same visibility restrictions to classes as it does to its members.

The default modifier is also referred to as package-private.

To use the default access modifier, just define your class members without any modifier:

class Person{
int age;
String name;
int jump(){}
}

Public Modifier

This modifier allows members of a class to be accessed in all packages. Simply put, you can access them everywhere. The public modifier provides the least restrictive level of access.

MAKEUSEOF VIDEO OF THE DAY

It’s also important to note that the public modifier can also be used with classes.

Simply prefix the class or its member with public so as to give it a public visibility. See the example below:

public class Person{
public int age;
public String name;
public int jump(){}
}

Protected Modifier

This modifier allows members of a class to be accessed within the class and its sub-classes. It can provide access outside a package though only through inheritance. Unlike the two previous modifiers, protected can only be used with members of a class, not the class itself.

See the code below on how you can use it:

class Person{
protected int age;
protected String name;
protected int jump(){}
}

Private Modifier

This modifier allows members of a class to only be accessed within the class. Just like protected, private is also only applicable to members of a class.

Private is the strictest level of access and should only be used if you are completely sure that you don’t want your class members being used by other classes. For example, you will get a compile-time error if you attempt to access a constructor with a private modifier.

As with public and privatesimply add the keyword private to use this modifier.

class Person{
private int age;
private String name;
private int jump (){}
}

More Java Considerations

At this point, it’s important to question how you would manage these visibility modifiers when it comes to method overriding. The answer is to maintain a level of visibility that is either at the same level as that defined by the super class or higher.


For example, if the parent class has protectedyou can’t use the default or private modifiers in the overriding subclass.

The table below summarizes the access levels of each visibility modifier. You can use it to ground your knowledge on access modifiers.


Java access modifiers

From the table, it’s interesting to note that the members of a class are always accessible within a class. The rest of the columns show what you’ve already read above.

It’s important to note that you can use a mix of these access modifiers within a class. What determines how you choose the one to use is how accessible you want a certain part of the code to be.

Your choice process should be a gradual shift from the most restrictive to the least restrictive.

Java is pretty neat alone, but when paired with MySQL? The possibilities are limited only by your own creativity.


mysql-connect-java
How to Connect to a MySQL Database With Java

Java provides JDBC as a part of the Java SDK. In this article, let’s look at the details of connecting to a MySQL database and performing queries with it.

Read Next


About The Author

Leave a Comment