C# - Can publicly inherited methods be hidden (e.g. made private to derived class)

前端 未结 10 1667
说谎
说谎 2020-12-04 19:00

Suppose I have BaseClass with public methods A and B, and I create DerivedClass through inheritance.

e.g.

public DerivedClass : BaseClass {}
<         


        
10条回答
  •  半阙折子戏
    2020-12-04 19:24

    Hiding is a pretty slippery slope. The main issues, IMO, are:

    • It's dependent upon the design-time declaration type of the instance, meaning if you do something like BaseClass obj = new SubClass(), then call obj.A(), hiding is defeated. BaseClass.A() will be executed.

    • Hiding can very easily obscure behavior (or behavior changes) in the base type. This is obviously less of a concern when you own both sides of the equation, or if calling 'base.xxx' is part of your sub-member.

    • If you actually do own both sides of the base/sub-class equation, then you should be able to devise a more manageable solution than institutionalized hiding/shadowing.

提交回复
热议问题