Is an interface a class?

后端 未结 6 1238
醉梦人生
醉梦人生 2020-12-15 17:18

Is an interface a special kind of class or can you say that an interface isn\'t a class at all?

6条回答
  •  难免孤独
    2020-12-15 18:22

    An interface isn't a class, but you could say that both interfaces and classes are types.

    From the Java specification:

    In the Java programming language, every variable and every expression has a type that can be determined at compile-time. The type may be a primitive type or a reference type. Reference types include class types and interface types.

    Notice though there is a special class called Class that can represent both classes and interfaces:

    Instances of the class Class represent classes and interfaces in a running Java application.

    The fact that an interface is represented by a Class instance where isInterface is true could give you the impression that an interface is just a special type of class. However this is not the case.

提交回复
热议问题