Why is it possible to omit default values in overridden member functions of sub-types?

后端 未结 2 896
無奈伤痛
無奈伤痛 2021-02-02 09:44

Just as stated in the title: Why is it possible to omit default values in overridden member functions of sub-types?

Is this normal or to be expected?

         


        
2条回答
  •  失恋的感觉
    2021-02-02 10:10

    Is this normal or to be expected?

    Yes.

    I suspect this was primarily a language design / usability decision. From this perspective, there were four options available:

    1. Declare in supertype only.
    2. Declare in subtype only.
    3. Declare in both, but do not allow subtype to change the default.
    4. Declare in both, allow subtype to override the default in the supertype.

    The Kotlin designers chose option #1. This makes sense because:

    Option #2 and #4 all imply that callers would not know what the default value is unless they were aware of which implementation they were using, which is of course highly undesirable. The caller would require extra logic to determine if a value is needed to override a default, which means the default would be useless.

    Option #3 violates the DRY principle. Why force the declaration to be in two places?

    That leaves Option #1 as the only sane choice.

提交回复
热议问题