interface

Published 2026-07-29 08:42 Updated 2026-10-13 17:14 615 words 4 min read ... Page views AI-reviewed An AI proof-read this post, but it still deserves a careful read. AI-rewritten Someone else wrote the original; AI helped me rewrite and polish it.

AI-reviewed An AI proof-read this post, but it still deserves a careful read.

This paper introduces the concept, function and application of interfaces in object-oriented design. Interfaces are used to establish behavioral specifications, reduce coupling between classes, support multiple implementations and unified calls, and function extensions can be achieved through inheritance and implementation. Combined with the UML class diagram, it shows the generalization, implementation, dependence, and association relationship between interfaces and classes, embodying the idea of "interface-oriented programming" in object-oriented design, as well as the practical application of factory patterns and aggregation design.

Interface

an interface

Interface is used to describe a set of public behavioral norms. It mainly has the following functions:

  • Develop unified functional standards.
  • Reduce the coupling between the caller and the concrete implementation class.
  • Allows different implementation classes to be passed and used with a unified interface type.

interface definitions

use interface Keyword definition interface:

public interface IFirst {
    String NAME = "TOM";

    void firstMethod();

    default void firstMethod2() {
        System.out.println("firstMethod2");
    }

    static void firstMethod3() {
        System.out.println("firstMethod3");
    }
}

Fields in the interface are used by default public static final Modifications, and therefore are essentially constants; ordinary abstract methods use the default public abstract embellishment. Starting with Java 8, interfaces can also define default methods and static methods.

An interface cannot be directly instantiated and must be implemented by a class before creating an implementation class object.

implement interface

class uses implements Keyword implementation interface. A class can implement multiple interfaces; non-abstract implementation classes must override all abstract methods in the interface.

public class FirstImpl implements IFirst, ISecond {
    @Override
    public void firstMethod() {
        System.out.println("实现 firstMethod 方法");
    }

    @Override
    public void secondMethod() {
        System.out.println("实现 secondMethod 方法");
    }
}

When calling, the implementation class object is usually referred to by the interface type, thereby programming for the interface:

public class Test {
    public static void main(String[] args) {
        IFirst first = new FirstImpl();
        ISecond second = new FirstImpl();

        first.firstMethod();
        second.secondMethod();
    }
}

Inheritance between interfaces

interface may use extends Inherit one or more interfaces.

public interface ITest extends IFirst, ISecond {
}
image-001
image-001

Use interfaces to develop standards

For example, to create a website that aggregates resources from multiple video platforms, you can first define the video resource interface and then let each platform provide implementation separately. Business code only relies on interfaces and does not directly rely on specific classes of a platform.

public interface IVideo {
    void getResource(String name);
}

This allows you to replace specific implementations without modifying the main business processes.

factory design pattern

The factory pattern encapsulates the process of creating objects into factory classes. The caller fetches the interface object through the factory, without directly using the constructor of the concrete implementation class.

public class VideoFactory {
    public static IVideo getVideoObject() {
        return new AQYVideo();
    }
}
public class Test {
    public static void main(String[] args) {
        IVideo video = VideoFactory.getVideoObject();
        video.getResource("xxx");
    }
}

The above code still leaves the factory to decide which implementation class to create, but the caller only relies on IVideo Interface, the degree of coupling is lower than directly creating a concrete class.

object-oriented analysis and design

UML can assist object-oriented analysis and design. Common diagrams include use case diagrams, class diagrams, sequence diagrams, state diagrams, and activity diagrams. Among them, the class diagram mainly describes classes, interfaces and the relationships between them.

Common relationships in class diagrams

generalization relation

Represents the inheritance relationship between classes, commonly used in Java extends。

relations

Represents a class implementation interface, used in Java implements。

dependencies

An object temporarily uses another object in a method, usually as a method parameter, local variable, or method return value. For example, a driver-class driving method may receive parameters for the vehicle interface type.

Association relationship

An object holds another object for a long time, usually as a member variable. For example, the defined team attributes in the player class belong to a one-to-one association; multiple players stored in the team class belong to a one-to-many association. If both parties hold references from the other party, it is a two-way association.

Written by AI-rewritten

If you enjoyed this, leave a comment~

Loading...