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

前端 未结 10 1634
说谎
说谎 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:34

    That sounds like a bad idea. Liskov would not be impressed.

    If you don't want consumers of DerivedClass to be able to access methods DeriveClass.A() and DerivedClass.B() I would suggest that DerivedClass should implement some public interface IWhateverMethodCIsAbout and the consumers of DerivedClass should actually be talking to IWhateverMethodCIsAbout and know nothing about the implementation of BaseClass or DerivedClass at all.

提交回复
热议问题