interface

Published 2026-07-29 08:42 Updated 2026-07-29 08:42 626 words 4 min read ... Page views

This paper introduces the concept, function and application of interfaces in object-oriented design. The interface is used to formulate behavioral specifications, reduce coupling between classes, support multiple implementations and unified calls, function expansion can be realized through inheritance and implementation, and system design is combined with factory patterns and UML class diagrams. The generalization, implementation, dependency, and association relationships in the class diagram reflect the structure and interaction between classes and interfaces.

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 the interface keyword to define the 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 decorated with public static final by default, so they are essentially constants; ordinary abstract methods are decorated with public abstract by default. 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 the implements keyword to implement the 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

Interfaces can inherit one or more interfaces using extends.

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 it up to the factory to decide which implementation class to create, but the caller relies only on the IVideo interface, which is less coupled than creating a concrete class directly.

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, and extends is commonly used in Java.

relations

Represents a class implementation interface, using implements in Java.

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.

If you enjoyed this, leave a comment~

... Page views
© 2026 跨越星轨的客 @Hoshiumi
Powered by theme astro-koharu · Inspired by Shoka