Table of contents
1.1: Overview of the interface (understanding)
1.2: Characteristics of the interface (memory)
1.3: Member characteristics of interfaces (memory)
1.4: The relationship between classes and interfaces (memory)
2. Interface composition update
2.1: Overview of interface composition updates (understanding)
2.2: Default method in interface [application]
2.3: Static method in interface [application]
2.4: Private methods in interfaces [application]
1. Interface
1.1: Overview of the interface (understanding)
-
An interface is a public normative standard. As long as it conforms to the normative standard, everyone can use it.
-
Two meanings of interface in Java
-
used to define specifications
-
for function expansion
-
1.2: Characteristics of the interface (memory)
Interfaces are decorated with the keyword interface
public interface 接口名 {}
Class implementation interface is represented by implements
public class 类名 implements 接口名 {}
-
Interface cannot be instantiated
We can create an implementation class object of the interface using
-
subclass of interface
Either override all abstract methods in the interface
Either the subclass is also an abstract class
1.3: Member characteristics of interfaces (memory)
-
Member characteristics
-
Member variables
Can only be constant Default modifier: public static final
-
Construction method
No, because the interface is mainly to extend the function, and there is no concrete existence
-
member method
only abstract methods
Default modifier: public abstract
Regarding the methods in the interface, there are some new features in JDK8 and JDK9, which will be explained later
-
-
code demo
-
interface
public interface Inter { public static final int NUM = 10; public abstract void show(); }
-
Implementation class
class InterImpl implements Inter{ public void method(){ // NUM = 20; System.out.println(NUM); } public void show(){ } }
-
test class
public class TestInterface { /* 成员变量: 只能是常量 系统会默认加入三个关键字 public static final 构造方法: 没有 成员方法: 只能是抽象方法, 系统会默认加入两个关键字 public abstract */ public static void main(String[] args) { System.out.println(Inter.NUM); } }
-
1.4: The relationship between classes and interfaces (memory)
-
class to class relationship
Inheritance relationship, only single inheritance, but multi-layer inheritance
-
class and interface relationship
The implementation relationship can be implemented by a single implementation or multiple implementations, and can also implement multiple interfaces while inheriting a class
-
interface to interface
Inheritance relationship can be single inheritance or multiple inheritance
2. Interface composition update
2.1: Overview of interface composition updates (understanding)
-
constant
public static final
-
abstract method
public abstract
-
Default method (Java 8)
-
Static methods (Java 8)
-
Private methods (Java 9)
2.2: Default method in interface [application]
-
Format
public default return value type method name (parameter list) { }
-
effect
Solve the problem of interface upgrade
-
example
public default void show3() { }
-
Precautions
-
Default methods are not abstract methods, so are not forced to be overridden. But it can be overridden, remove the default keyword when overriding
-
public can be omitted, default cannot be omitted
-
If multiple interfaces are implemented and the same method declaration exists in multiple interfaces, subclasses must override the method
-
2.3: Static method in interface [application]
-
Format
public static return value type method name (parameter list) { }
-
example
public static void show() { }
-
Precautions
-
Static methods can only be called by the interface name, not by the implementation class name or object name
-
public can be omitted, static cannot be omitted
-
2.4: Private methods in interfaces [application]
-
The reason for private methods
Java 9 added private methods with method bodies, which actually laid the groundwork in Java 8: Java 8 allows default methods and static methods with method bodies to be defined in interfaces. This may cause a problem: when two default methods or static methods contain the same code implementation, the program must consider extracting this implementation code into a common method, and this common method does not need to be used by others , so it is hidden with private, which is the inevitability of adding private methods in Java 9
-
Define the format
-
Format 1
private return value type method name (parameter list) { }
-
Example 1
private void show() { }
-
Format 2
private static return value type method name (parameter list) { }
-
Example 2
private static void method() { }
-
Precautions
-
Default methods can call private static and non-static methods
-
Static methods can only call private static methods
-
-