Java Learning Interface

Source: Internet
Author: User

First, the basic concept

Interface (Interface), which is an abstract type in the Java programming language, is a collection of abstract methods. Interfaces are usually declared in interface. A class inherits the abstract method of an interface by inheriting the interface.

If a class consists only of abstract methods and global constants, it is not defined as an abstract class in this case. will only be defined as an interface, so the interface strictly speaking belongs to a special class, and this class is only abstract methods and global constants, even the construction method is not.

Second, the use of the interface

1, because there are abstract methods inside the interface, the interface object cannot be instantiated directly using the keyword new. The following principles apply to the interface:
(1) The interface must have sub-classes, but at this time a subclass can use the Implements keyword to implement multiple interfaces;
(2) The subclass of the interface (if it is not an abstract class), it must overwrite all the abstract methods in the interface;
(3) An object of an interface can be instantiated with an upward transformation of the subclass object.

2, for subclasses, in addition to implementing the interface, you can also inherit the abstract class. If you want to inherit an abstract class and also implement an interface, use the syntax format:

3. In Java, an abstract class can inherit only one abstract class, but an interface can use the extends keyword to inherit multiple interfaces at the same time (but the interface cannot inherit abstract classes).

4, from the concept of the interface, the interface can only be composed of abstract methods and global constants, but the internal structure is not limited by the concept, as the abstract class can be defined as an abstract inner class, in the interface can also define ordinary inner classes, abstract inner classes and internal interfaces (but in terms of actual development, It is uncommon for the user to define an internal abstract class or internal interface themselves, as shown in the following example, an abstract inner class is defined in the interface:

  

Java Learning Interface

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.