Reference excerpt: http://www.runoob.com/java/java-interfaces.html
An interface (interface) is an abstract type in Java and a collection of abstract methods.
A class implements all the abstract methods in an interface through the implements inheritance interface. (except abstract classes)
declaring interfaces:
Interface animal{ publicvoid eat (); Public void Travel ();}
The interface has the following characteristics:
1. Interfaces are implicitly abstract and do not need to use the abstract keyword when declaring an interface
2. Each method in the interface is implicitly abstract, and the declaration does not require the abstract keyword
3. The methods in the interface are all public
Implementation of the interface:
class implements the interface using the Implements keyword.
When a class implements an interface, it is necessary to implement all the methods in the interface, otherwise the class must be declared as an abstract class
Instance:
Public classDogImplementsanimal{ Public voideat () {System.out.println ("Dog Eat"); } Public voidTravel () {System.out.println ("Dog Travel"); } Public voidOthermethod () {System.out.println ("This class method"); } Public Static voidMain (string[] args) {Dog d=NewDog (); D.eat (); D.travel (); }}
Operation Result:
Dog Eatdog Travel
Inheritance of interfaces:
Using the extends keyword, similar to inheritance between classes
//file name: Sports.java Public Interfacesports{ Public voidSethometeam (String name); Public voidSetvisitingteam (String name);}//file name: Football.java Public InterfaceFootballextendssports{ Public voidHometeamscored (intpoints);}//file name: Hockey.java Public InterfaceHockeyextendssports{ Public voidhomegoalscored ();}
The football interface itself declares a method, inherits two methods from sports, so that the class implementing the football interface needs to implement 3 methods
Multiple Inheritance of interfaces:
The class cannot inherit more, but the interface can
Public Interface extends Sports,football
Tag interface:
Interfaces that do not have any methods are called labeled interfaces.
Package Java.util; Public Interface eventlistener{}
Java Tour _ Object-oriented _ interface