The use of interfaces in Java

Source: Internet
Author: User

Interface in Java is very important content, the interface not only solves the single inheritance problem of the class, and the interface has the same function as the abstract class, and can enforce some functions of the interface. The interface, as its name implies, is the ability to connect to other interfaces or classes to implement multiple inheritance of classes in a particular way.

Let's look at the Declaration and implementation of the interface:

To declare an interface in Java with the keyword interface , the code for the Declaration is as follows:

 1  public  abstract  interface   interface name { 2 /**3  * define an interface  4  */5  public  abstract  void   print ();  6   can also be a constant of final declaration, static member variable  7 } 

As above is the declaration of the interface, the abstract in the above interface can not be written, because the interface default is abstract. Methods can also not write abstract, the default method is abstract, that is, the interface method is an abstract method, no body. The default access modifier in an interface is public, so it can also be written off.

Implementation of the interface:

The interface cannot be instantiated because the interface is an abstract class. So we have to create a class to implement the methods in the interface. There are two ways to implement an interface:

One is to implement all the methods in the interface, and the other is to declare themselves as abstract classes. Let's take a look at both of these methods:

 Public class class Name implements interface name {        // implementation   of the above interface public        void  Print () {                 System.out.print ("This is the implementation of the interface");}    }

There is also the declaration of yourself as an abstract class.

 Public class Abstract Implements method and member variables  in the interface name {        // class}

The implementation interface above uses a word implements, which is the keyword used to implement the interface. A class can also implement multiple interfaces, separating the interfaces with "," .

The use of interfaces in Java

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.