The core concept of Java: interface (interface)

Source: Internet
Author: User

The core concept of Java: interface (interface)

Interfaces are at the same level as classes, and in fact, interfaces are a special kind of abstract class.

For example: interface ia{} public interface:

The public interface is similar to a class where a file can have only one public interface and is the same as the file name. A public interface and a public class cannot be defined in a file at the same time.

In an interface, all methods are public, abstract, and all properties are public, static, and constant.

A class implements the format of an interface: Class Iaimple implements ia{}; A class implements an interface, which is equivalent to inheriting an abstract class.

A class must implement a method in an interface, otherwise it is an abstract class. The interface and class are the same in the implementation.

The public is not writable in an interface, but public is not saved in the process of implementing an interface in a subclass.

(If you have left public, you will be prompted with an error when compiling: The object cannot implement the method from the interface.) )

Note: ① A class can implement an interface in addition to inheriting another class; class Iaimpl extends java.util.Arrylist implement ia{}

Inheriting a class implements an interface so that multiple inheritance can be implemented in disguise.

② A class can inherit only another class, but it may inherit multiple interfaces, separated by "," in the middle.

Implements Ia,ib The so-called implementation of an interface, refers to the implementation of the interface method.

An inheritance relationship can be defined between the ③ interface and the interface, and multiple inheritance is allowed between interfaces.

Example: interface IC extends ia,ib{};  Interfaces can also be used to define objects IA i=new Iaimpl (); The implemented classes inherit from both the parent class and the interface to make the run-time type.

Iaimple extends A implement Ia,ib IB i=new iaimple ();

I instance of Iaimple; I instance of A; I instance of IA; I instance of IB; The returned result is true.

Interfaces and polymorphism are at the heart of Java technology.

Interfaces are often defined by us as a class of XX things. Interfaces are actually defining a specification, a standard.

① can realize the common properties of different levels and different system objects through interface, and implement write once as anywhere through interface. Take Java database connection as an example: JDBC formulates standards, data vendors implement standards, and users use standards. Interfaces are often used to mask the underlying differences.

The ② interface is also used to maintain the stability of the architecture for the above reasons.

The core concept of Java: interface (interface)

Related Article

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.