Does calling a virtual protected method from a base class violate the LSP?
https://softwareengineering.stackexchange.com/questions/120268
-
03-11-2019 - |
Question
Let's say we have this base class:
class MyBase
{
public string Name { get; set; }
public string GetDescription()
{
var descriptionList = new List<string>() { this.Name };
descriptionList.AddRange(this.ExtraDescriptions.ToList());
return string.Join(", ", descriptionList();
}
protected virtual IEnumerable<string> ExtraDescriptions()
{
return new List<string>();
}
}
Then add a derived class:
class MyDerived : MyBase
{
protected override IEnumerable<string> ExtraDescriptions()
{
return new List<string>() { "a", "b" };
}
}
It seems to me that this violates the LSP (because we're changing the behavior of the GetDescription
method), and yet it's also a fundamental feature of object-oriented programming. The base class obviously declares its intention to allow the ExtraDescriptions
method to be overridden, but this isn't necessarily visible to a consumer of the class.
Ever since I learned about the LSP, this problem has really been gnawing at me. So:
- Does this violate LSP?
- What's the correct alternative to this?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with softwareengineering.stackexchange