Java access level Modifier

Source: Internet
Author: User

The access level modifier determines whether other classes can access a field or method of the class.

There are two types of access control;

1. Top Level (for class modification)

There are two types of modifiers: public and package-private (modifier is not explicitly specified and nothing is written, that is, default;

2. Member level (for member modification)

There are four modifiers: Public, private, protect, and package-Private.

 

For Class access control, public indicates that the modified class is visible to any class. If no access modifier is written, that is, package-private, the modifier class is only visible to the class of the package to which the class belongs.

For access control of members, the definitions of public and package-Private are exactly the same as those of class access control. Two access modifiers are added for Members: Private and protect, private indicates that the member can only be accessed by the class to which the member belongs. Protect includes the package-Private visibility and has more visibility into the subclass of this class. The subclass can be in the same package of this class, it can also be in another package.

Java access control seems very simple, but I am a bit confused, that is, if the access modifier of a class is package-private (the modifier is not explicitly specified ), so what is the significance of defining a member of this class as a modifier of a method as public? Because this class cannot be accessed in other packages, the method of this class will certainly not be called. However, defining a program in this way does not report an error, so there must be some truth. I searched for a similar question on the Internet and found a good example to explain this question.

Code Package test;
Public abstract class test {
Public abstract void test ();
Public static test getinstance (){
Return new subtest ();
}
}
Class subtest extends test {
@ Override
Public void test (){
System. Out. println ("subtest's test ()");
}
}
Code for testing:
Import Test. Test;
Public class main {
Public static void main (string [] ARGs ){
Test T = test. getinstance ();
T. Test ();
}
}

 

This is the only method I know that can make the public or protect method of the package-private class accessible to other packages, I want to write it like this because it can indirectly reduce the access level of the method that a subclass overrides than that of the parent class, because it is not allowed to reduce the access level of the rewrite method. In this way, we assume that a public method is inherited or implemented, but other packages cannot know its method unless it is accessed by its parent class. It increases the flexibility of access control.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.