I. What is an interface?
Interface (Interface), which is an abstract type in the Java programming language, is a collection of abstract methods. Interfaces are usually declared in interface. A class inherits the abstract method of an interface by inheriting the interface.
Two. Create an interface
Before creating an interface we need to know some of the necessary rules for the interface:
The interface in 1.Java only supports public or package access, cannot be private or protected permissions, but the interface of the package access rights will be inherited, only the interface under this package can inherit, which will easily lead to unknown errors, Therefore, it is recommended that all interfaces be set to public permissions.
2. All methods in the interface are the default public abstract methods without the method body, so when we create the method in the interface, we usually do not explicitly confirm that the method is public.
The field can be set in 3.Java. But the fields are both static and final, that is, constants.
A new feature has been added to the 4.javase8, where static non-abstract methods can be placed in the interface.
From the 4th we can see that we can create a main method in the interface and make it run.
Here's an interface we created ourselves:
Package Com.cjm.myinterface; Public Interface myinterfacetext { int num = 0; constant void F1 (); Abstract method staticvoid F2 () { System.out.println (" JavaSE8 's new feature can write non-abstract static methods in the interface! "); } }
Create a Main method in the interface and make it run:
Public Interface MYINTERFACETEXT1 { publicstaticvoid main (string[] args) { System.out.println ("Asdas");} }
The results are as follows:
Three. Interface Inheritance interface
Suppose you now have two interfaces, and one interface wants to implement another interface, what is the method?
interface inherits the same interface as the class implementation, and the interface implements the interface through the extends keyword.
The code is as follows:
Public Interface extends Myinterfacetext, MyInterfaceText1 { // implements an interface that inherits multiple interfaces }
Multiple interface inheritance is separated using commas.
Then the child interface will have all the abstract methods of the parent interface.
Four. Multi-inheritance for Java via interface
C + + allows a class to have more than one parent class (the base class), but Java only allows one parent class but can implement multiple inheritance through multiple interfaces. (Logically speaking, Java is indeed better than C + +, there is only one father, then the class should be so to conform to this law, after all, computer language is only a real life mapping).
This also brings great benefits, which greatly reduce the resources needed to open up the subclass. Multiple parent classes will load all the resources of the parent class once, some of which may not be required by subclasses.
The class that implements the interface must also implement the abstract method in the interface, and if you want to inherit the class and implement the interface, you must put the inheriting class in front and implement the interface behind it.
Public class Implements myinterfacetext { @Override publicvoid F1 () { // TODO auto-generated Method Stub System.out.println ("Implement the methods in the interface! "); } Public Static void Main (string[] args) { new classinterface ();} }
When we implement an interface for a class, we can transform the instance object of the subclass up to the interface type. The flexibility of polymorphism is the reason for designing interfaces.
Five. Nested definition interfaces
Interfaces can be nested defined in interfaces or classes.
To define an interface in a class to be nested is to allow the interface to be private.
class classhasinterface { privateinterface A {// using nested definitions, interfaces can be private void f (); } }
Interfaces in Java