Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>LSP states that a subclass should be substiutable for it's superclass (see stackoverflow question <a href="https://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle">here</a>). The question to ask yourself is, "Is writeable text a type of <strong>readonly</strong> text?" The answer is clearly "no", in fact these are mutually exclusive. So, yes, this code violates LSP. However, is writable text a type of <strong>readable</strong> text (not readonly text)? The answer is "yes". So I think the answer is to look at what it is you're trying to do in each case and possibly to change the abstraction a bit as follows: </p> <pre><code>class ReadableText { private string text = string.Empty; public ReadableText(string value) { text = value; } public string Text { get { return text; } } } class WriteableText : ReadableText { public WriteableText(string value):base(value) { } public new string Text { set { OnTextSet(value); } get { return base.Text; } } public void SetText(string value) { Text = value; // in reality we'd NotifyPropertyChanged in here } public void OnTextSet(string value) { // call out to business logic here, validation, etc. SetText(value); } } </code></pre> <p>Just to be clear, we're hiding the Text property from the Readable class using the new keyword on the Text property in the Writeable class.<br> From <a href="http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx</a>: When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload