Is there ever a reason to hide inherited members in an interface?

后端 未结 5 1671
孤独总比滥情好
孤独总比滥情好 2020-12-30 04:56

I understand that a class which inherits from another class may hide a property by using the new keyword. This, however, is hiding a specific implementation of

5条回答
  •  悲&欢浪女
    2020-12-30 05:28

    Hiding inherited members should never be done deliberately as part of a design. Method hiding is allowed by the language to prevent changes in the ancestor (in major releases of a library) from breaking descendants who happen to already have a member by that same name defined.

    That said, sometimes it is convenient to hide an inherited method with an equivalent method that returns a more specific type. In these cases, the only thing you're doing is typecast sugar - be careful not to change the semantics of the method because callers may just as easily call the ancestor method instead of yours.

    See also: Is there ever a situation where derived class should hide …?

提交回复
热议问题