Access modifier in Java

The access modifier in Java defines access to variables, methods of this class from other classes in the same package or in two different packages.

There are four types of access modifiers in Java: public, protected, default, and private. Overview of each access modifier has the following meanings:

  • public: all classes inside or outside the package and internally class have access to variables and public methods of this class.
  • protected: only those classes in the same package or classes that extend from this class and internally class can access public variables of this class.
  • default: Only classes in the same package with this class and internally class will be able to access variables, public methods of this class.
  • private: Only those classes within this class will have access to public class variables and methods.

For you to understand more, let’s go through each access modifier.



Public access modifier

This is access modifier without limit the access, the public class or interface can be accessed from all packages, including internal classes or interface, to classes in different packages.

For example, we have a Student class defined as a public class in the com.huongdanjava package and it has a public variable named “name” and a public method goToSchool() as follows:

And we have another class named Example located in the default package, this class can access the variables, methods of the Student class as follows:Access modifier in Java

Protected access modifier

Variables, methods defined with the protected access modifier only access from the classes in the same package, and child classes extend from this class.

For example, in the public access modifier section, if I now modify the access modifier of the variable “name” and the goToSchool() method to the protected as follows:

Now in the Example class, the compile error will appear:

Access modifier in Java

You can only access these variables, methods in the Example class when it extends from the Student class and does not create new Student object like this:

Access modifier in Java

Default access modifier

Variables, methods defined without using an access modifier will have the default access modifier. These variables can only be accessed from classes in the same package.

Following above example, I now delete the access modifier defined for the “name” variable and goToSchool() methods of the Student class as follows:

Returning to the Example class, you will get a compile error:

Access modifier in Java

To solve this problem, we only have to move the Example class located in the same package as the Student class:

Access modifier in Java

At this point, the compile error will automatically disappear.

Access modifier in Java

Private access modifier

This access modifier restricts access the most, with this access modifier, the variables, the methods can only access within the class.

For now, I will add private to the variables, methods of the Student class:

At this point, the Example class will fail to compile.

Access modifier in Java

If you create another class within the Student class with content similar to the Example class, then there will be no compile error

Access modifier in Java


Access modifier with override method in subclass

If a child class overrides a method of the parent class, this override method does not reduce access to that method; for example, if the access modifier of method in the parent class is the default access modifier, then in the child class, we have not defined the access modifier for this override method to private method.

For example, we have Student class as follows:

And the Example class extends from the Student class. Now if I wanted to override the goToSchool() method in the Example class, I have to define the access modifier of the overridden method as protected or public.

Access modifier in Java

or

Access modifier in Java

You cannot define it to private method.

Access modifier in Java

5/5 - (1 vote)

Add Comment