If the base class actually allows setting the name (as opposed to exposing an abstract setter which isn't guaranteed to work), derived classes should do so as well. If you want a hierarchy which includes some classes where the name can be set and some where it cannot, both kinds of class should inherit from an abstract ReadableXX
class with a non-virtual read-write Name
property that chains to abstract GetName
and SetName
methods. It should also provide some means of indicating whether SetName
will be supported (e.g. a CanSetName
method). A read-only variant of the class could define a "new" read-only Name
property that would chain to GetName
, and have its SetName
override throw NotSupportedException
. If the specification for the class says that SetName
will only work if CanSetName
returns true, then having SetName
throw an exception in classes where CanSetName
returns false would not violate the Liskov Substitution Principle.
↧
Answer by supercat for What exception to throw when a property setter is not allowed?
↧