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)