In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public
, protected
and private
, while making class
and interface
and dealing with inheritance?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The difference can be found in the links already provided but which one to use usually comes down to the "Principle of Least Knowledge". Only allow the least visibility that is needed.
Access modifiers are there to restrict access at several levels.
Public: It is basically as simple as you can access from any class whether that is in same package or not.
To access if you are in same package you can access directly, but if you are in another package then you can create an object of the class.
Default: It is accessible in the same package from any of the class of package.
To access you can create an object of the class. But you can not access this variable outside of the package.
Protected: you can access variables in same package as well as subclass in any other package. so basically it is default + Inherited behavior.
To access protected field defined in base class you can create object of child class.
Private: it can be access in same class.
In non-static methods you can access directly because of this reference (also in constructors)but to access in static methods you need to create object of the class.
Often times I've realized that remembering the basic concepts of any language can made possible by creating real-world analogies. Here is my analogy for understanding access modifiers in Java:
Let's assume that you're a student at a university and you have a friend who's coming to visit you over the weekend. Suppose there exists a big statue of the university's founder in the middle of the campus.
When you bring him to the campus, the first thing that you and your friend sees is this statue. This means that anyone who walks in the campus can look at the statue without the university's permission. This makes the statue as PUBLIC.
Next, you want to take your friend to your dorm, but for that you need to register him as a visitor. This means that he gets an access pass (which is the same as yours) to get into various buildings on campus. This would make his access card as PROTECTED.
Your friend wants to login to the campus WiFi but doesn't have the any credentials to do so. The only way he can get online is if you share your login with him. (Remember, every student who goes to the university also possesses these login credentials). This would make your login credentials as NO MODIFIER.
Finally, your friend wants to read your progress report for the semester which is posted on the website. However, every student has their own personal login to access this section of the campus website. This would make these credentials as PRIVATE.
Hope this helps!
The official tutorial may be of some use to you.
In very short
public
: accessible from everywhere.protected
: accessible by the classes of the same package and the subclasses residing in any package.private
: accessible within the same class only.Access modifiers in Java.
Java access modifiers are used to provide access control in Java.
1. Default:
Accessible to the classes in the same package only.
For example,
This access is more restricted than public and protected, but less restricted than private.
2. Public
Can be accessed from anywhere. (Global Access)
For example,
3. Private
Accessible only inside the same class.
If you try to access private members on one class in another will throw compile error. For example,
4. Protected
Accessible only to the classes in the same package and to the subclasses
For example,