1. Use interface to define an interface. An interface defines a class-like definition that consists of an interface's declaration and interface body, where the interface body consists of a constant definition and a method definition. The basic format for defining an interface is as follows:
1 interface Interface Name [extends parent interface List]{2 [public] [static] [final ] constant; 3 [public] [abstract] method; 4 }
Modifier: Optional parameter public, if omitted, is the default access permission;
Interface Name: Specifies the name of the interface, by default, the interface name must be a valid Java identifier, in general, the first character is required to capitalize;
Extends parent interface Name list: Optional parameter that specifies which parent interface the defined interface inherits from. When you use the extends keyword, the parent interface name is a required parameter.
Method: The method in the interface is defined only and cannot be implemented
For example:
Public Interface istudent { finalint X = ten; float getscore (int studentno); int getstudentage (int studentno);}
2. The definition of the interface is finished, the following is the implementation of the interface:
Java implementation interface, with implements:
Class < category name > [extends parent class name] [implements interface list]{}
modifier: optional parameter that specifies the access rights for the class, with the optional values public, abstract, and final.
class Name: required parameter, which specifies the name of the class, and the class name must be a valid Java identifier. In general, the first letter is required to capitalize.
extends parent class name : Optional parameter that specifies which parent class the class to define inherits from. When you use the extends keyword, the parent class name is a required parameter.
implements interface list: An optional parameter that specifies which interfaces the class implements. When using the Implements keyword, the interface list is a required parameter. When multiple interface names exist in the interface list, each interface name is separated by commas.
1 Public classCireImplementsCalinterface2 { 3 Public floatGetarea (floatR)4 { 5 floatArea=pi*r*r;//Calculate Circle area and assign value to variable6 returnArea//returns the calculated Circle area7 } 8 Public floatGetcircumference (floatR)9 { Ten floatCircumference=2*pi*r;//calculates the perimeter of a circle and assigns a value to the variable circumference One returncircumference;//returns the circumference length after calculation A } - Public Static voidMain (string[] args) - { theCire C =NewCire (); - floatf = C.getarea (2.0f); - System.out.println (float.tostring (f)); - } +}
In the inheritance of the class, only single-inheritance can be done, but when implementing the interface, it is possible to implement multiple interfaces at a time, separating each interface with a comma ",". In this case, there may be a constant or method name conflict, when resolving the problem, if the constant conflict, you need to explicitly specify the interface of the constant, which can be achieved through the "interface name. Constant". If a method conflict occurs, only one method can be implemented.
Java Interface Definition and implementation