Java Permissions Explained

Source: Internet
Author: User

Java access is as common as classes and objects, and is ubiquitous in Java programs.

Java access, depending on the permission range from large to small: public > Protected > Package > Private.

To explain before the explanation: the word "use" here means that the class is instantiated (instantiating an object), accessing the property, and invoking the method.

First, the access rights of the Java class are explained. Classes in Java have only two access rights, public and package (default access rights). If a class is modified by public, the class can be used in any package. If the permission of a class is a package, then the class can only be used in the packages in which it resides.

Next, you'll explain the access permissions for properties and methods. The first access to properties and methods is limited to the access rights of the classes, that is, the access rights of the properties and methods are the maximum for the class. When access to a property or method is public, the property or method can be called in any package if the access rights of the class to which it belongs are public, and if the permission of the class to which it belongs is package, then the property or method can only be called in the same packet as the class in which it belongs. When access to a property or method is a package, the property or method can only be called in the same packet as its class. When access to a property or method is private, the property or method can only be called in the class.

Then explain the protected to modify the properties or methods. If a property or method is protected decorated, first this property or method can only be used in the package where the class is located. Second, if a class inherits a property or method that is protected decorated in the parent class, the child class can also access the protected property or method in the parent class, even if the child class is not in the same package as the parent class.

Finally, let's explain the access permissions in the inheritance issue. In inheritance, when a subclass is covering a method of a parent class, the method must have access permissions greater than or equal to the access rights of the method in the parent class. If the child class and the parent class are in the same package, the subclass can inherit the properties and methods that are decorated by public, protected, and package in the parent class. If a subclass is not in the same package as the parent class, the subclass can inherit only the public, protected-modified methods in the parent class. If there is a class base in package A, there is class Son,son inheriting base in package B, base base = new Son (), then the base object must know when making a method call. The permissions of the methods that the base object can invoke depend on the access rights of the methods in the base class, not the methods in the son.

  

  

  

Java Permissions Explained

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.