Java Modifer & #39;p ublic& #39; is reduntant for interface methods

Source: Internet
Author: User
Tags modifier modifiers

http://androidren.com/index.php?qa=322&qa_1=java-%E4%B8% Ad-modifer-public-is-reduntant-for-interface-methods

It is often seen that the public modifier is written on the interface, and then the IDE, for example: Eclipse or idea often prompts public to be redundant. Then I looked it up a bit. The Java default interface method is public and abstract, so it is not required. At the same time, assume that you use private or protected will error.

So, feel relieved to delete the public bar.

Http://stackoverflow.com/questions/17011374/are-public-and-public-final-redundant-for-interface-methods

You cannot has afinalMethod declared in an interface. Fields is alwaysfinalBut methods is alwaysabstract(And neverfinal). You cannot define an interface method, which is to be implemented only by classes in the same package.* FROM section 9.3 of the Java Language specification:

every Field declaration in the body of a interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of the these modifiers for such fields.

and from section 9.4:

Every method declaration in the body of a interface is implicitly public (§6.6).

Every method declaration in the body of an interface are implicitly abstract, so it body is always represented by a Semico Lon, not a block.

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a me Thod declared in an interface.

* As Paul Bellora points out in a comment, you can make the interface itself package-private (or protected, or even privat e) If you want to restrict its visibility.


In addition, an interface can contain constant declarations. All constant values defined in an interface is implicitlypublicstatic, andfinal. Once again, you can omit these modifiers.

Other than that. All constant values are public static final by default, not joined.

Java Modifer & #39;p ublic& #39; is reduntant for interface methods

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.