Java Interface Summary

Source: Internet
Author: User

Definition of the interface:

Use interface to define an interface. The interface definition is similar to the definition of a class and is also the Declaration and interface body of the interface, where the interface body consists of a variable definition and a method definition, and the basic syntax for defining the interface is as follows:

interface Interface Name [extends  parent interface list]{    [public] [static] [final  ] variable;    [Public] [abstract] method;}

Modifier: Optional parameter that specifies the access permissions for the interface, and the optional value is public. If omitted, the default access rights are used.

Interface Name: A required parameter that specifies the name of the interface and the interface name must be a valid Java identifier. In general, the first letter is required to capitalize.

Extends parent interface Name list: Optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter.

Method: The method in the interface is only defined and not implemented.

Note: The interface name generally has an unwritten rule that the first letter is I.

Cases:

 Public Interface Ijisuan {    publicvoid  Qiumianji ();      Public void Qiuzhouchang ();}

 Public Interface Ixingzhuang {    publicvoid  Hua ();}

 Public Abstract class Implements Ijisuan, Ixingzhuang {    publicvoid  Hua () {        System.out.println ("drawing a quadrilateral ");    }}

 Public classJuxingextendssibianxing {Private intA; Private intb;  PublicJuxing (intAintb) {         This. A =A;  This. B =b; } @Override Public voidQiumianji () {Doubles = A *b;    System.out.println (s); } @Override Public voidQiuzhouchang () {Doublec = a+b+a+b;            System.out.println (c); }     Public voidHua () {System.out.println ("Drawing a moment Shape"); }}

Can be compared to see

The interface is declared with the keyword interface .

The interface is equivalent to class, and all methods in the interface are abstract methods.

interface in the definition of the method, you can use the abstract keyword, you can omit the abstract keyword, (most of the time is omitted), the method is still abstract, cannot have the implementation of curly braces.

Interfaces and abstract classes function Similarly, interfaces cannot be instantiated, and interfaces can be thought of as a special abstract class (all abstract methods).

The polymorphic usage of an interface is similar to an abstract class, and a reference to an interface type can point to an object of the class that implements the interface.

Referring to the interface, it will be associated with abstraction, at this time everyone will feel the interface and abstraction is very similar, then what is the difference between them?

1. The interface itself is abstract, so his methods must all be abstract, but the methods in the abstract class can be abstract or concrete, as described in the previous article on the interpretation of abstract classes.

2. A class can implement an interface, (similar to a parent-child class with extends inheritance) using the keyword implement. We all know that Java is a single inheritance, but a class can implement multiple interfaces, such as the sibianxing written above to implement the Ijisuan, Ixingzhuang two interfaces.

3. If a class implements an interface or multiple interfaces, if the class is an abstract class, then this class does not implement the entire content of the interface, as sibianxing in the above example defines an abstract abstraction class, so there is no need to implement Ijisuan, Ixingzhuang all the contents of the two interfaces.

4. Similarly to 3, if a class implements an interface or multiple interfaces, if the class is not an abstract class and is a concrete class, then the class must implement the entire contents of the interface, as in the above example, juxing, although Juxing is an inherited sibianxing, But Sibianxing is an abstract class, and does not implement the contents of all interfaces, and juxing does not define abstract, is a concrete class, then juxing must Ijisuan, Ixingzhuang two interfaces all the contents of the implementation, This is what is written in the example above.

Java Interface Summary

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.