Java Interface (interface)

Source: Internet
Author: User

Interface definition: [modifier] Interface interface name extends parent interface Name 1, parent interface Name 2 ... {

}

Interface can be said to be a special kind of abstract class. An interface can only define methods, not instances of methods.

1. The interface is capable of defining abstract methods, all of which are public by default.

2. The properties declared in the interface are default to public static final, or public static final only.

 Public Interface output{    // The system automatically adds the public static final modifier    to the member variable in the interface / / The result of the following two lines is exactly the same     int max_size =;      Public Static int Max_size =  ;}

3. The interface can inherit other interfaces and add new properties and methods. An interface can have multiple parent interfaces, but interfaces only inherit interfaces and cannot inherit classes.
class implements the syntax format of an interface

class extends Implements interface 1, interface 2 ... {    class body part}

After a class implements one or more interfaces, the class must fully implement all of the abstract methods (i.e. overrides) defined in these interfaces, otherwise the class will retain an abstract method inherited from the parent interface, which must be defined as an abstract class.

//defining the Sing interfaceInterfacesing{voideat (); voidsleep ();} Public classTestImplementssing{//overriding methods in an interface@Override Public voideat () {System.out.println ("Can Eat"); } @Override Public voidsleep () {System.out.println ("Can Sleep"); }     Public Static voidMain (string[] args) {Sing P=NewTest (); //output: Can eatp.eat (); //output: Can sleepP.sleep (); }

Use an interface.

Java Interface (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.