The function and significance of java interface

1. What is an interface? A
Java interface is a declaration of a series of methods. It is a collection of method characteristics. An interface has only method characteristics and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can be implemented. have different behaviors (functions).

Second, the characteristics of the interface
Each method in the interface is implicitly abstract, and the methods in the interface will be implicitly designated as public. The abstract interface can
contain variables, but the variables in the interface will be implicitly designated as public. static final variable
The method in the interface cannot be implemented in the interface, only the class that implements the interface can implement the method in the interface
3. The difference between the interface and the class The
interface cannot be instantiated
The interface has no construction method
All methods in the interface must be Abstract methods, after java8, you can use the default keyword to modify non-abstract methods. Interfaces
cannot contain member variables, except static and final variables.
Interfaces support multiple implementations.
4. The difference between interfaces and abstract classes.
Methods in abstract classes can have method bodies, which means they can be implemented. The specific function of the method, but the method in the interface cannot have a method body. The
member variables in the abstract class can be of various types, while the member variables in the interface can only be of public static final type.
Interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods.
A class can inherit only one abstract class, and a class can implement multiple interfaces.
5. The declaration method of the interface
public interface UserService {     void study();

    void sleep();
}

1
2
3
4
5
6
public interface StudentService {
    void play();
}

1
2
3
4


public class StudentServiceImpl implements StudentService,UserService{
    @Override
    public void play() {

    }

    @Override
    public void study() {

    }

    @Override
    public void sleep() {

    }
}

Shangxuetang brings a brand-new Java 300 set course to students! A must-have high-quality tutorial for self-study Java for zero-basic beginners in java_Hand-in-hand diagrams to learn Java, making learning a kind of enjoyment_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/hutubiancheng/article/details/126996352