Java Access Modifiers

Java Access modifiers include Public, Private, Protected and Default.

Access modifiers determines what classes can access to the element which has the modifier.

Let’s have a loot at the access modifiers one by one:

  • Public: The element can be accessed by any class, whether in the same package or not.
  • Protected: The element can be accessed only by the classes in it same package and any class that extends the class where is the element, where in the same package or not.
  • Default: The element can be accessed only by the classes in it same package.
  • Private: The element can be accessed only by the class where it is. It more common use is in the fields of the class. We don’t want the fields being modified by other class directly, it would break the encapsulation of the object.
Access modifiers
The same classOther class in the same packageSubclass in another packageOther class in another package
publicXXXX
protectedXXX
defaultXX
privateX

We shouldn’t use public in all the classes and elements that we create. Try to restrict ever much as possible. For example, if we do not need our class to be accessible from other packages, we will make it protected. Or, if you are making a method used just in it class, make it private. The more we restrict our code, the more secure and robust it will be.

Finally, let’s see an example using the modifiers. We are going to create a new project with 2 packages: package1 and package2. Package1 will have 2 classes, Person and Plant and package2 will have 2 classes too, Student and Oak. It will be an easy example just to see how access modifiers works so each class will have just one field: Person and Plant will have the field name, Oak the field age and student the field mark.

As you could see Student extends from Person, it’s to see better how modifiers work. We will use a method from the class Person to see it. Let’s make the method getName() with the modifier public.

And we are going to create a new method in the other classes to check the accessibility. Create the following method in the rest of the classes:

If we create a new Person object and call the method getPerson() you will see that there’s no problem in any class. That’s because the method is public.

If we change the method getName() to protected you will see that Oak and Student will be wrong. In the case of Student, we can fix it. We just have to call just getName(), without creating a new Person object. That’s because, we don’t need to create a new Person object to use this method in a class that extends from Person. As you see, with protected, we can use the method in the same package, and in a subclass in another package.

Now let’s delete the modifier. This means that now it’s default.

As you see, now the method just works with the class Plant, which is in the same package than person.

Finally, let’s make the method private.

Now, the method can’t be access by other class, this method will work just in the class where it’s declared.

You can read more about Java access modifiers here: https://www.w3schools.com/java/java_modifiers.asp

In the next section of this tutorial we will learn about Regular Expressions.