Object-Oriented Programming: Deep Understanding of Java Interfaces


insert image description here

Interface plays an important role in Java development, it defines the standard and specification for the interaction between classes. This article will deeply discuss the definition method, member composition, implementation method of Java interface and the relationship between interface and polymorphism, and introduce the new features in Java 8 and later versions.

1. Interface: definition and analogy in life

Interfaces can be seen everywhere in real life, and we can compare them to standards, specifications or conditions. For example, the USB interface of a computer has certain specifications for equipment such as U disk, keyboard and mouse, and the size of the elevator determines the size of the sofa, bed and cabinet that can be accommodated. In the code, the interface plays the role of defining standards and specifications, and requires the class that implements the interface to meet certain conditions.

2. How to define an interface

In Java, we use interfacekeywords to define interfaces. Interfaces can contain member variables and member methods.

// 定义一个简单的接口
interface MyInterface {
    
    
    int SOME_CONSTANT = 100; // 常量
    void someMethod(); // 抽象方法
}

3. Members in the interface

Members in interfaces are a bit special. In an interface, member variables are actually constants, and we can omit public static finalmodifiers; member methods are abstract methods, and we can omit public abstractmodifiers.

Interfaces cannot have constructors because constructors are used to initialize objects and interfaces cannot create objects directly.

4. Implementation of the interface

The interface itself cannot create objects directly, it needs to be implemented by the class. A class can implement one or more interfaces, and to implement an interface is to meet the criteria defined by the interface.

4.1 Implementation of a single interface

When a class implements an interface, all abstract methods in the interface need to be overridden.

class MyClass implements MyInterface {
    
    
    @Override
    public void someMethod() {
    
    
        System.out.println("实现接口中的抽象方法");
    }
}

4.2 Implementation of Multiple Interfaces

A class can implement multiple interfaces, which is also a manifestation of multiple inheritance in Java. A class can inherit from a parent class (single inheritance) and implement multiple interfaces (multiple implementations).

interface InterfaceA {
    
    
    void methodA();
}

interface InterfaceB {
    
    
    void methodB();
}

class MyClass implements InterfaceA, InterfaceB {
    
    
    @Override
    public void methodA() {
    
    
        System.out.println("实现接口A中的方法");
    }

    @Override
    public void methodB() {
    
    
        System.out.println("实现接口B中的方法");
    }
}

5. Polymorphism and interfaces

Polymorphism is an important concept in object-oriented development, which occurs when a class inherits a parent class or implements an interface. The three characteristics of polymorphism include: method rewriting, upward modeling and interface-oriented development.

insert image description here

6. New feature: default method and private method

Java 8 and later versions introduce some new features that enrich the functionality of the interface:

  • Default method: Use defaultkeywords to define the implementation body in the interface, and the implementation class can choose whether to override the default method. The advantage of this is that if the interface needs to add new methods, it will not affect the code of the implementation class.
interface MyInterface {
    
    
    default void defaultMethod() {
    
    
        System.out.println("默认方法的实现");
    }
}
  • Static method: Use statickeywords to define a static method in an interface, which can be called directly through the interface name.
interface MyInterface {
    
    
    static void staticMethod() {
    
    
        System.out.println("静态方法的实现");
    }
}
  • Private method: Use privatekeywords to define private methods in the interface, private methods can only be called in the default method or static method of the interface.
interface MyInterface {
    
    
    default void defaultMethod() {
    
    
        helperMethod();
    }

    private void helperMethod() {
    
    
        System.out.println("私有方法的实现");
    }
}

These new features bring more convenience and extensibility to the use of interfaces.

Summarize:

Interface is an important part of Java development, which defines the standards and specifications between classes. We learned how an interface is defined, what its members consist of, and how a class implements an interface. Interface and inheritance form the feature of multiple inheritance, which makes Java's object-oriented development more flexible and powerful. At the same time, we also learned about the new features introduced in Java 8 and later versions: default methods, static methods, and private methods. These new features bring more convenience and extensibility to the use of interfaces. In object-oriented development, interface is an important means to achieve polymorphism, which allows us to write more flexible and maintainable code.

insert image description here

Guess you like

Origin blog.csdn.net/qq_43546721/article/details/131960699