[JAVA] interface (1), java Interface

Source: Internet
Author: User
Tags constant definition

[JAVA] interface (1), java Interface

I. Concepts of interfaces

An interface is a more thorough abstraction. Interfaces are abstracted from multiple similar classes. Interfaces do not provide any implementation. Interfaces reflect the design philosophy of standardization and implementation separation.

Ii. Interface Definition

The class keyword is no longer used for interface definition, but the interface keyword is used.

The basic syntax for defining interfaces is as follows:

[Modifier] interface Name extends parent interface 1, parent interface 2...

{

// Constant definition

// Abstract method definition

}

  • The modifier can only be public or omitted. If it is omitted, it indicates the package access level. That is, this interface can be accessed only under the same package.
  • An interface can have multiple parent classes, but it can only inherit and cannot implement interfaces.
  • The interface defines the common public behavior norms of multiple classes. Therefore, the Field, method, internal class, and enumeration class defined in the class are all public access permissions.
  • Methods In the interface can only be abstract methods. abstract methods cannot be modified together with static methods. Therefore, all methods in the interface are always public abstract. This can be left blank by default and will be added by default.
  • The Field defined in the interface is interface-related and can only be a constant. Therefore, all fields defined in the interface are modified by public static final, which can be left blank by default. The system will add the Field by default.
  • The constructor and initialization block cannot be defined in the interface..
  • Internal classes, enumeration classes, and interfaces can be defined in the interface. By default, all objects are modified using public static and can only be modified using public static.

The following is an example of Interface Definition:

  

Package interfaceDemo; public interface OutPut {// the Field defined in the interface can only be a constant int MAX_CACHE_LINE = 50; // This sentence is equivalent to the above sentence // public static final int MAX_CACHE_LINE = 50; // The methods defined in the interface can only be abstract, no method body void out (); void getData (String msg );}

Iii. interface inheritance

Different interfaces and classes support multi-inheritance. Multiple parent interfaces are separated by commas (,) following the extends keyword. Like class inheritance, when an interface inherits a parent interface, it obtains all the abstract methods and constants defined in the parent interface.

  

public interface InterfaceA {    int A = 1;    void print();}public interface InterfaceB {    int B = 2;    void say();}public interface InterfaceC extends InterfaceA, InterfaceB{    int C = 3;        public static class Test    {        public static void main(String[] args) {            System.out.println(InterfaceC.A);            System.out.println(InterfaceC.B);            System.out.println(InterfaceC.C);        }    }}

Output result:

1
2
3

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.