Java Interface Basics

Source: Internet
Author: User

Interface (interface)

1. The interface Body contains the declaration of the constant (no variable) and the abstract method two parts. There is only an abstract method in the interface body, there is no ordinary method, and all the constant access rights in the interface body must be public, and it is a static constant (which allows the public, final, and static modifiers to be omitted). Access to all abstract methods must be public (allow omitting public abstract modifier)

2. If a non-abstract class implements an interface, the class must override all methods in the interface. It should be noted that because the method in the interface must be the Public,abstract method, the class will not only remove the abstract modifier, give the method body when overriding the interface method, and the access permission of the method must be obviously decorated with public (otherwise, the access rights are reduced. This is not allowed). The non-abstract class implementing the interface realizes the method in the interface, and gives the concrete behavior function of the method.

3. If a class declaration implements an interface, but does not override all methods in the interface, the class must be an abstract class, that is, the abstract class can either override the methods in the interface or directly own the methods in the interface.

4. Define the interface is, if the keyword interface preceded by the public keyword, it is said that such an interface is a public interface. The public interface can be implemented by any one class. If an interface is not public decorated, it is called a friendly interface, and the friendly interface can be implemented with the class of the interface in the same package.

Demo Interface printable{    publicstaticfinalint MAX =;   equivalent notation: int MAX = +;     Public Abstract void Add (); // equivalent notation: void Add ();     Public Abstract float sum ( float x,float  y);}

Java Interface Basics

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.