The interface is only used to define the type

When a class implements an interface, the interface as a type can be used to refer to instances of the class. Therefore, a class implements an interface, thus indicating how the client can handle instances of the class. It is not appropriate to define an interface for other purposes.

One type of interface that fails is the so-called constant interface. Such an interface does not contain any methods; it only contains static final properties, with a constant for each output. Classes that use these constants implement interfaces to avoid the need to qualify constant names with class names. Here is an example:

ublic interface PhysicalConstants {
    
    static final double AVOGADROS_NUMBER   = 6.022_140_857e23;

}

  The constant interface mode is a bad use of interfaces. The class uses some constants internally, which are completely implementation details. Implementing a constant interface will cause this implementation detail to leak into the class export API. For users of a class, it makes no sense to implement a constant interface. In fact, it may even confuse them. Even worse, it represents a promise: if the class is modified in a future version and it is no longer necessary to use constants, then it must still implement the interface to ensure binary compatibility. If a non-final class implements a constant interface, the namespace of all its subclasses will be polluted by the constants in the interface

Guess you like

Origin www.cnblogs.com/lIllIll/p/12702795.html