Access Modifiers provides certain level of restriction to the usage of the class members so that they are not accidentally modified.
Let us understand it in detail.
The main motto of using Classes and Objects is to maintain certain standards. So, that the data inside the objects do not get modified or deleted accidentally.
i.e. Let us consider the class 'Human'.
The above class 'Human' has an attribute called 'age'. And we are using a setter method
to set some value to the age.
And we have a getter method
to get the age.
So, we do not access the data members (i.e. 'age') directly outside the class. We need to use a getter and setter method to do that.
And this is where 'Access Modifiers' comes into picture. It puts a restriction on the variables.
Let us see each in detail :
A Private access modifier will never allow a class member to be accessed outside the class.Usually, an attribute is marked as private(i.e. age in the above class). So, that they should not be accessed outside the class.
A private data member can be accessed only inside a class.
A Public data member can be accessed outside the class. Usually, methods in a class are Public and attributes in a class are private.
Also, a class can be declared public, so that this class is accessible and visible to all the classes in all the packages in Java.
But if a public class you are trying to access, is in a different package. You need to import that class in your package.
If you are declaring an attribute of a class public. You can access it from outside the class. No getter and setter method is needed for that. But that's never a good practice.
1. A Protected data member can be accessed outside the class, but in the same package.
2. But if you want to access it from a class which doesn't belong to the same package, you need to use inheritance.
Let us understand with the below example :
1. Example with the protected data members are in the same Package.
Since, in the above example both Animal.java and Main.java were in the same package, we were able to access the 'protected' method legs(),
from the 'Main' class.
2. Example with the protected data members present in different Package.
Now, since the Animal class and Main class are in different package. We cannot access the protected method 'protected void legs()' from the 'Main' class.
So, what we do is :
1. Extend Animal class, so that all the protected methods gets inherited. Then access them using an object of the base class.
So, it is when we don't specify anything. The default modifier is placed by the compiler by default. The variables and methods with default modifiers can only be accessed inside the package.
It behaves like a public modifier, but inside a package.
In the above example, since we have not specified any access modifier in the 'age' attribute
It is the default modifier.
Let us have a look at the below table, which says when and where we can use a particular access modifier.
Access Modifier | In a class | In a package | outside the package but by subclass | outside of package |
---|---|---|---|---|
Private | Yes | No | No | No |
Default | Yes | Yes | No | No |
Protected | Yes | Yes | Yes | No |
Private | Yes | No | No | No |
Public | Yes | Yes | Yes | Yes |