Access Specifiers In Java.

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 programming in either language…

… or in Java then you would have seen these access specifiers in Java or another language like public, private, and protected.|Access Specifiers In Java|

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.| Access Specifiers In Java|

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

private
public
protected
default

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 the same directory.

And class C is away from them in another package or another directory.

availability of access specifier in java
availability of access specifier in Java

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, that public members are available everywhere and their scope is the global scope.

Protected members of class A are available to class B, but not to 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 concerning 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.

You can also go through a few more amazing blog post links below related to core Java:

Keywords In Java In Hindi…
OOPS, Concepts In Java In Hindi…
Data Abstraction In Hindi…
Encapsulation In Hindi…
Wrapper Class In Java In Hindi…
Access Specifiers In Java…
JDBC Driver In Java In Hindi…
Types of JDBC Driver In Java…
Session Beans and their types…
EJB Features: Enterprise Java Beans Features…
Symptoms of an overloaded server…
EJB In Java In Hindi…
Java Beans In Hindi…
What is ODBC in Java in Hindi…
What is JDBC In Hindi…
What are JSP and its advantages…
Advantages and Disadvantages of Servlet…
What is Servlet and why it is used…
Static Keyword In Java In Hindi…

Conclusion:

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 specifiers 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 a5theorys@gmail.com 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 feedback in the comment section below.

Have a great time!

Anurag

I am a blogger by passion, a software engineer by profession, a singer by consideration and rest of things that I do is for my destination.