What is interface teacher: Wang Shaohua QQ Group No.: 483773664 Learning Goal
Mastering the definition and use of interfaces
Understanding the difference between an interface and an abstract class
First, what is the interface
In real life, we often hear the word interface, such as the USB interface, what does it mean? We take the USB interface for example, regardless of the manufacturer of the USB memory, we can use the USB interface to let the computer recognize it, this is why? This is because these manufacturers follow a set of identical standards and follow this standard to generate USB devices.
In software, the interface is also a specification and standard, they can constrain the behavior of the class, is a collection of some method features, but there is no method of implementation. Therefore, the interface can also be regarded as a special "abstract class".
Second, the definition of the interface
Unlike the definition class, the keyword class is not used, and the keyword interface is used.
(a) define the syntax
The syntax is as follows
1234 |
[修饰符] interface 接口名 extends 父接口 1 ,父接口 2 ...{ 零个到多个常量定义... 零个到多个抽象方法定义... } |
Description
Modifiers can only be public or omitted
As long as the interface name conforms to the Java Identifier Naming specification, it is recommended that multiple meaningful words be concatenating, capitalized, and that there is no connector between words and words.
An interface can have multiple direct parent interfaces, but cannot inherit classes.
The attributes defined in the interface can only be constants and, regardless of whether the public static final decoration is used, the properties in the interface will always be decorated with these three modifiers
No constructors and initial blocks in the interface
method in an interface, only an abstract method
(ii) Define an interface
123456 |
public interface InterfaceDemo { //接口里定义的属性只能是常量 int MAX_LENGTH = 50 ; //接口里定义的方法只能是抽象方法 void getLength(); } |
(iii) Verification:whether the public static final modifier is used, the properties in the interface will always be decorated with these three modifiers
The properties in the interface are by default using the public static final decoration, so even if another class is under a different package, the interface's constant properties can be accessed through the interface
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7E/FF/wKioL1cPbICR9sJsAAAnaJqY1jk990.png " data_ue_src= "E:\My knowledge\temp\f34d0ef7-92d8-40df-9184-6435bd667dc8.png" >
third, the inheritance of the interface
The inheritance of interfaces is not the same as class inheritance. The inheritance of an interface is multi-inheritance, separated by multiple interfaces.
Similar to class inheritance, a subinterface extends the parent interface and obtains the parent interface's abstract methods, constant properties, and so on
12345678910111213141516171819 |
public interface InterfaceA {
String PRO_A=
"InterfaceA"
;
public void testA();
}
public interface InterfaceB {
String PRO_B=
"InterfaceB"
;
public void testB();
}
public interface InterfaceC
extends InterfaceA,InterfaceB{
String PRO_C=
"InterfaceC"
;
public void testC();
}
public class TestInterfaceExtends {
public static void main(String[] args) {
System.out.println(InterfaceA.PRO_A);
System.out.println(InterfaceB.PRO_B);
System.out.println(InterfaceC.PRO_C);
}
}
|
Iv. use of interfaces(i) Purpose of the interface
Interface cannot create an instance
An interface can be used to declare a reference-type variable, and the variable must point to an object that implements the implementation class for this interface;
The main purpose of the interface is implemented by the implementation class
(ii), the implementation of the interface
interface implementation, using the keyword: implements
A class can implement one or more interfaces, and multiple interfaces are separated by semicolons
Java has a single inheritance, multi-implementation features
123 |
[修饰符] class 类名 extends 父类 implements 接口 1 ,接口 2 ...{ 类体部分 } |
Once a class implements one or more interfaces, the class must fully implement all the abstract methods of those interfaces, otherwise the class must be defined as an abstract class
1. All implementations
123456789101112131415161718192021 |
public class DemoD
implements InterfaceA,InterfaceB,InterfaceC{
@Override
public void testC() {
// TODO Auto-generated method stub
}
@Override
public void testB() {
// TODO Auto-generated method stub
}
@Override
public void testA() {
// TODO Auto-generated method stub
} <> Section II What is an interface |