Java - Is it possible to subclass an array? And more questions about arrays in Java

后端 未结 3 739
自闭症患者
自闭症患者 2020-12-21 04:14

These questions are purely asked out of curiosity. I don\'t actually need to subclass an array, I\'m just trying to figure out more about how they work in Java.

3条回答
  •  天涯浪人
    2020-12-21 04:36

    The Java Language Specification answers all these questions:

    The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.

    So no, there isn't a common base class for all arrays, and therefore no JavaDoc either. The members of arrays are defined by the spec instead.

    Subtyping among array types is defined in section 4.10.3 - and yes, String[] is a subtype of Object[]. See also ArrayStoreException.

    Yes, String[].class != String[][].class. (c.f. section 10.8)

    No, there is no syntax to subclass arrays. In particular, the extends clause must contain a class type (and array types are not class types).

提交回复
热议问题