The Java Foundation--my view on the access modifier

Source: Internet
Author: User

1.java modifier access Rights--public,protected,private,default (package,friendly)

classes, generic variables, and methods that are modified by public can be accessed by any class within and outside the package;
classes, generic variables, and methods that are modified by protected, and any class within the package, and those that inherit such subclasses from the package, can be accessed;
The classes, generic variables and methods that are modified by private are inaccessible to any class outside the package;
If a class, generic variable, and method are not decorated with these three modifiers, it is default, or it can be called a friendly type or a package type.
Then any class within the package can access it, and no class outside the package will be able to access it, which also includes inheriting subclasses of this class outside of the package, so that such classes, generic variables, and methods are friendly and open to other classes within the package, and other classes outside the package are closed.

my view on the reasons why 2.private and protected cannot modify classes and interfaces

When you use eclipse as the IDE, when you create a class or interface, or you see a situation like this:

This means that the private and protected modifiers cannot be decorated with classes and interfaces. What is this for? The author analyses the following

(1) Restrictions on private access: private-decorated classes and interfaces cannot be accessed by any other class or interface. So what does it mean to define a class or interface like this? So don't.

(2) Restrictions on access rights according to protected: protected-Modified classes can only be accessed by classes in the same package or by their subclasses . So let's take an example: Suppose Class B and a are not in the same package, a is the protected class, and B can only be a subclass of a if B wants to access a, and B has access to a if it wants to be a subclass of a (or B can inherit a). These two conditions are mutually premise, cannot realize.

3. Interface methods cannot be decorated with the private,protected,default (friendly) modifier

The method of the interface must be implemented by other classes, otherwise it loses the meaning of the interface existence. Since it needs to be implemented by other classes, the interface cannot be private,protected decorated for the following reasons:

1) Assuming that the public interface I has a protected method m, then the public class C in the other package can implement this interface (method M is still protected). Then C's same packet Class D call method M can only access this way:

c C = new C ();
C.M ();
Cannot access this:

I C = new C ();
C.M ();

If interface I is c1,c2 by more than one class ... Implementation, other classes choose to invoke C1,C2 according to different circumstances,... Method M, it is not possible to provide a unified interface. Originally this one with an interface to access the M method, Callm (I c), now according to the different implementation class C1,C2,... To provide multiple interfaces. This loses the importance of using the interface: to provide a unified interface, interface-oriented programming ideas can not be reflected.

(2) Assuming that interface I is default, then the method m is protected theoretically there is no problem, and M can also be default, public. As for why not to use protected: since the class can only be implemented in this package, the other packages can not be implemented, which has lost the importance of using the interface: to provide a unified interface. Since public can be accessed by this package class, it can also be accessed by classes in other packages, he le. Oh

(3) Assuming that the public interface I has a default method M, then the other package does not implement interface I properly, because method m is not visible to it.

(4) Assuming that the default interface I has a default method m, then the method m is the default theoretically there is no problem, and M can also be proteced, public. The default may not be used for the same reason as (2) that protected cannot be used.



The Java Foundation--my view on the access modifier

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.