In Java language, interfaces are implemented by classes in order to use the methods in the interface. A class needs to declare that the class implements one or more interfaces using the keyword implements in the class declaration. If you implement multiple interfaces, separate the interface names with commas.
For example:
classAimplementsPrintable, Addable//Class A implements the Printable and Addable interfaces classDogextendsAnimalimplementsEatable, Sleepable//Animal’s Dog subclass implements the Eatable and Sleepable interfaces
If a non-abstract class implements an interface, then the class must override all methods in the interface.
Note: Since the methods in the interface must be public abstract methods, when the class overrides the interface method, it must not only remove the abstract modifier, but also provide the method body, and the access rights of the method must be clearly modified with public.
If a non-abstract class that implements an interface implements a method in the interface, it is equivalent to giving the specific behavioral function of the method. Users can also customize interfaces, and a Java source file can be composed of classes and interfaces.
If a class declares to implement an interface but does not override all methods in the interface, then the class must be an abstract class. That is to say, the abstract class can either override the methods in the interface or directly own the methods in the interface.
Details:
Programs can use the interface name to access the constants in the interface, but if a class implements the interface, then the class can use the constants in the interface directly in the class body.
When defining an interface, if the keyword interface is preceded by the public keyword, it is called a public interface , and the public interface can be implemented by any class; if the public keyword is not added, it is called a friendly interface , and the friendly interface can be used with that class. The interface is implemented by classes in the same package.
If the parent class implements an interface, then the subclass will naturally implement the interface. That is to say, the subclass no longer needs to use the keyword implements to declare the implementation of this interface.
Interfaces can also be inherited, that is, you can declare that an interface is a sub-interface of another interface through the keyword extends. Since the methods and constants in the interface are common, the sub-interface will inherit all methods and constants in the parent interface.
Note: The interfaces provided by Java are all in the corresponding packages. Through the import statement, not only the classes in the package can be introduced, but also the interfaces in the package can be introduced.
For example:
importjava.io.*;//At this time, not only the classes in the java.io package are introduced, but also the interfaces in the package are introduced