Interface
1. Multiple unrelated classes can implement the same interface
2, a class can implement a number of unrelated interfaces
3. Similar to inheritance, there is polymorphism between interface and implementation class
4. Define the syntax format for Java classes
< modifier> class < name>[extends< superclass>][implements< interface>[, interface]*]{
<declarations>*
}
Report:
1, interface (interface) is the combination of the abstract method and the definition of the solid value.
2, in essence, the interface is a special kind of abstract class, this kind of abstract class contains only the solid and the definition of the method, but not the implementation of variables and methods.
3, the interface definition example:
Public Interface runner{ publicstaticfinalint id = 1; Public void STRRT (); Public void run (); Public void stop ();}
Interface Features:
1, interface can achieve multiple inheritance
2, the properties of the interface declaration default to public static final, or only public static final;
3, the interface can only define abstract methods, and these methods default to public, can only be public;
4, the interface can inherit other interfaces, and add new attributes and abstract methods;
The use of interfaces and the implementation of interfaces are polymorphic:
Public classtestinterface{ Public Static voidMain (String args[]) {Singer student=NewStudent ("Studentname"); Student.sing (); Student.sleep (); Teacher Teacher=NewTeacher ("TeacherName"); Teacher.painter (); Teacher.eat (); Painter Painter=(Painter) teacher; Painter.painter (); Painter.eat (); //The following is a partial implementation of polymorphismTestInterface T1 =NewTestInterface (); T1.F (student); T1.F (teacher); } Public voidF (Singer s) {s.sing (); }}Interfacesinger{ Public voidSing (); Public voidsleep ();}Interfacepainter{ Public voidpainter (); Public voideat ();}classStudentImplementssinger{PrivateString name; PublicStudent (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidStudy () {System.out.println ("Studying ..."); } Public voidSing () {System.out.println ("Study is singing"); } Public voidsleep () {System.out.println ("Study is Sleeping"); }}classTeacherImplementssinger,painter{PrivateString name; PublicTeacher (String name) { This. Name =name; } PublicString GetName () {returnname; } Public voidSing () {System.out.println ("Teacher is singing"); } Public voidsleep () {System.out.println ("Teacher is sleeping"); } Public voidpainter () {System.out.println ("Teacher is paintering"); } Public voideat () {System.out.println ("Teacher is eating"); }}
Operation Result:
Memory Analysis Diagram:
Java Interface interface