Interface and Implementation of Java Program Development and Learning

(Study reference book: Java2 practical tutorial fifth edition)

1. Interface

Use the keyword interface to define an interface, similar to a class, an interface contains an interface declaration and an interface body:

interface 接口名{
    
    
	接口体内容;
}

Interface body: contains two parts : the declaration of constants (no variables) and abstract methods ; the methods in the interface body are only abstract methods. And in the interface body: the access rights of all constants must be public and static constants ( public final static modifiers are allowed to be omitted ), and the access rights of all abstract methods must be public (the public abstract modifiers are allowed to be omitted )

Second, implement the interface

(1) A class implements an interface.
A keyword implements needs to be used in a class to declare that the class implements one or more interfaces; the format is as follows:

class A (extends B) implements 接口1,接口2{
    
    
		...;
}

(2) Rewriting methods
in an interface If a non-abstract class implements an interface, then this class must rewrite all methods in this interface . Since the method in the interface must be a public abstract method, in addition to removing the abstract and writing out the method body, the rewritten method must add public , otherwise the access rights of the method will be reduced.
If the declaration of a class implements an interface, but does not override all the methods in the interface, then the class must be an abstract class. An abstract class can either override the methods in the interface or directly own the methods in the interface.

(3) Details of the interface

  • The program can use the interface name to access the constants in the interface, but if a class implements the interface, then the class can directly use the interface constants in the class body.
  • When defining an interface, if public is added before the keyword interface, the interface can be implemented by any class; if public is not added, the friendly interface can only be implemented by classes in the same package.
  • If the parent class implements an interface, its subclasses will naturally implement the interface, and there is no need to use implements to declare the implementation of this interface.
  • Interfaces can also be inherited by declaring that the child interface inherits the parent interface through extends. Because the constants and methods in the interface are of public type, the sub-interface will completely inherit all the constants and methods in the parent interface

Three, interface callback

Interfaces can also declare variables called interface variables. Interface callback refers to: assign a reference to an object created by a class that implements an interface to the interface variable declared by the interface, then the interface variable can call the interface method implemented by the class . In fact, when an interface variable calls an interface method implemented by a class, it is to notify the corresponding object to call this method.
Objects can call the original methods of the class and the interface methods implemented by the class, but interface variables can only call the interface methods implemented by this class through the object's reference transfer.

Fourth, the role of the interface

(1) Interfaces can abstract important behavior standards, which are represented by abstract methods.
(2) The reference of the object of the class that implements the interface can be assigned to the interface variable. The interface variable can call the interface method implemented by the class, which means that the class gives specific behavior according to the behavior standard in the interface.
(3) The idea of ​​the interface is that it can require certain classes to have methods with the same name, but the specific content of the methods can be different, that is, the interface is required to be implemented.

Five, the difference between abstract class and interface

(1) The subclass of the abstract class must rewrite all the abstract methods in the abstract class, and interface variables call the interface methods implemented by this class through the object.
(2) While the interface requires some classes to have methods with the same name, it does not force these classes to have the same parent class.
(3) The abstract class requires all subclasses to have this function, and the interface requires some classes to implement a certain function according to the situation.
(4) The abstract class is the abstraction of a class of things, and the interface is the abstraction of a certain behavior.

大体功能:
class abstract 一个具有共性方法的抽象类
interface 数个具有独有方法的接口
具体的功能子类1 extends 抽象类 implements 接口1,接口2
具体的功能子类1 extends 抽象类 implements 接口1,接口3
具体的功能子类1 extends 抽象类 implements 接口2,接口3

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112998587