Here are the changes that we made on the code: Here is a sample code that shows how we can violate this principle: The subclass cannot throw an exception that is not thrown by the base class unless the exception is a subtype of the exception that may be thrown in the base class.The subclass should not change the state of an object that is not permitted by the base class.The invariants of a base class must not be changed by a subclass.The postconditions of a base class must not be weakened by the subclass.The preconditions of a base class must not be strengthened by the subclass.The return types of the subclass must be more restrictive or the same as the return type of the base class.The parameters in the subclass must be less restrictive or the same as those in the base class.It is also stated that in order to comply with the LSP we must follow these rules: This principle simply states that all subclasses should operate the same way as the base class. Then (y) should be true for objects y of type S where S is a subtype of T. Let (x) be a property provable about objects x of type T. The third principle in the SOLID principles is the Liskov Substitution Principle (LSP), it was named after Barbara Liskov as together with Jeannette Wing she was able to come up with a definition for subtyping.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |