Hello Friends, In this blog post(Access Specifiers In Java) we are going to let you know about the access specifier in java. If you have ever done the programming in either language or in Java then you would have seen these access specifiers in java or in another language like public, private, and protected.
We have seen that any class and its members within the class package should be declared public. Then only the class and its members will be available for use outside the package. Let us discuss what exact difference is there among various access specifiers in java.
An access specifier is a keyword that is used to specify how to access a member of a class or the class itself. It means we can use an access specifier with the members of a class or with the class also| (Access Specifiers In Java)
There are four access specifiers in Java: Access Specifiers In Java
We do not use the default keyword for specifying the default access specifier. If no other access specifier is used, then the java compiler will take it as a default access specifier.
In the figure below we are taking three classes class A, Class B, and Class C. Whereas class A and class B exist in the same package or in the same directory. and class C is away from them in another package or another directory.
Let us assume that there are four members with private, public, protected, and default specifiers in class A. Now let us discuss which of these members will be available to class B and class C.
Private members of class A are not available to class B and class C. This means any private member’s scope is limited only to that class where it is defined. So the scope of the private access specifier is class scope.
Public members of class A are available to class B and also class C. this means, public members are available everywhere and their scope is the global scope.
Protected members of class A are available to class B, but not in class C. But if class C is a subclass of class A then the protected members of class A are available to class C.
So we can conclude that the protected members of a class act as public with respect to the sub-class.
When no access specifier is used, it is taken as the default specifier. Default members of class A are available to class B which is within the same package.
They are not available to class C. So here we can conclude that the scope of the default member is within the package scope.
So friends, In this blog post(Access Specifiers In Java) we have learned about the access specifier in java. There are four types of access specifiers in java and they are public, private, protected, and default specifier in java. We have also understood the scope of the access specifier in java.
In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.
Hope! you would have enjoyed this post about Access Specifiers In Java.
Please feel free to give your important feedbacks in the comment section below.
Have a great time!