Reproduced:
Java does not support multiple inheritance, that is, a class can have only one parent class
To overcome the disadvantages of single inheritance, Java uses interfaces, and a class can implement multiple interfaces
An interface is a collection of abstract methods and constant value definitions, and is a special kind of abstract class
The interface contains only the definitions of constants and methods, and there are no implementations of variables and methods
All the methods in the interface are abstract
The access type of the member in the interface is public
The variables in the interface use the public static final identity by default (can be defined without this adornment, system default)
The interface is declared by using the keyword interface.
Format: Name of the interface interface
Interface Body:
The interface body contains a constant definition and a method definition two parts
Only the declaration of the method in the interface body is not allowed to provide the implementation of the method
The definition of a method has no method body and ends with a semicolon
public interface Runner
{
int id=1;
void run ();
}
interface allowed to be inherited, can inherit an existing interface with extends
public interface Runner
{
int id=1;
void run ();
}
Interface Animal extends Runner
{
void Breathe ();
}
Implement all the methods in an interface by using implements
Class Fish implements Animal
{
public void Run ()
{
System.out.println ("Fish is swiming");
}
public void Breathe ()
{
System.out.println ("Fish is bubbling");
}
}
If you only need to implement some of the methods defined in an interface, you can define an abstract class to implement
Abstract class Landanimal implements Animal
{
public void Breathe ()
{
System.out.println ("Landanimal is Breathing");
}
A class can inherit one parent class and implement one or more interfaces, and the extends keyword must precede the Implements keyword
Class Student extends person implements Runner
{
......
public void Run ()
{
System.out.println ("The Student is Running");
}
......
}
When implementing a method of an interface in a class, the name, return type, number of arguments, and type of the method must be exactly the same as in the interface
The methods in the interface are public by default, and all classes must be decorated with public when implementing the interface method.
If the return type of the interface's method is not void, the method body must have at least one return statement when implementing the interface method in the class
In the case of void type, the class body can have no statement except for two curly braces
The interfaces provided in Java are in the corresponding packages, and the interfaces provided by Java can be used by the introduction of packages, and interfaces can be defined by themselves
A Java source file is made up of classes and interfaces.
Interfaces can add functionality that many classes need to implement, and different classes can use the same interface, and a single class can implement multiple interfaces
Interfaces are only concerned with functionality, and do not care about the specific implementation of the feature, classes that use the same interface do not necessarily have an inheritance relationship
Public interface: When an interface is declared, the keyword interface is preceded by the Public keyword, which can be used by any one class
Friendly interface class: an interface is not decorated, and a friendly interface can be used by a class in the same package.
Interface in Java