Why can't classes and interfaces use private and protected? The method of the interface cannot use private, protected, default

Source: Internet
Author: User
Tags modifiers

For Java programmers, Java access rights modifiers public, protected, default, private and the difference between the use is certainly not a problem, here is no longer verbose, anyway, niang a search on a lot of. Recently, when finishing the training materials for Java core, I thought of several questions about permission modifiers:

    1. Why do classes and interfaces (regardless of internal classes and interfaces) not use private and protected modifiers, only public and default?
    2. Why can't the interface's methods be private and protected, only public (default can be written out, but automatically converted to public after compilation)?

Thought carefully, the reason is not difficult. The main thing is that although the Java is not a short time, this problem really did not seriously thought, really ashamed. Write down your thoughts, think more, and don't be the movers of the frames.

First question:


It is well understood that classes and interfaces, if defined as private, do not have access to any other classes, and such classes do not make sense to write them out.


This problem is better explained by contradiction, assuming that Class B and a are not in the same package, a is the protected class, then B can access a if B is the subclass of a, and B can be a subclass (or B can inherit a) if B can access a. These two conditions are mutually premise, cannot realize.

Second question:


Interfaces are required by other classes, and if the method is defined as private, then no other class can be accessed. Such a method is required to be implemented and not visible to any class, which is not possible.


(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 ();

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 to why it is not allowed to use protected, it may be for the purpose of simplifying the complexity of the modifier (if used, it is not available when the interface is public, the default interface is available), and for Default please refer to (3).


(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.

Therefore, the integration of the above four points, the interface method can only use public, since it can only be used Public,java compiler when you forget to write public in front of M simply automatically help you to turn public.

Finally, the above for personal understanding, did not find any official explanation, if mistakes, read this paragraph of the text of the friends themselves, welcome to discuss:)

Why can't classes and interfaces use private and protected? The method of the interface cannot use private, protected, default

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.