Object-Oriented Design Principles liskov substitution principle)

Source: Internet
Author: User

Liskov substitution principle LSP is one of the basic principles of object-oriented design.

As mentioned in the Rys replacement principle, subclass can certainly appear where any base class can appear.

LSP is the cornerstone of inheritance reuse. Only when the primary class can replace the base class and the functions of the software unit are not affected can the base class be reused, the category class can also add new behaviors based on the base class.

 

As a result, the question arises: "How do we measure the quality of the inheritance relationship ?"

In 1987, liskov proposed a principle on inheritance.

 

"Inheritance shocould ensure that any property proved about supertype objects also holds for subtype objects ."

"Inheritance must ensure that the properties of the superclass are still valid in the subclass ."

 

That is to say, when a subclass instance should be able to replace any of its superclasses, There Is A is-a relationship between them. This principle is referred to as liskov substitution principle -- Lishi replacement principle.

 

Principles:

1. The subclass must fully implement the abstract method of the parent class, but cannot overwrite the non-abstract method of the parent class;
2. You can add your own unique method to the subclass;
3. When the subclass method reloads the method of the parent class, the preconditions (that is, the method parameters) of the method are looser than the input parameters of the parent class method;
4. When the subclass method implements the abstract method of the parent class, the post-condition (that is, the return value of the method) of the method is stricter than that of the parent class.

Advantages:
1. Improve code reusability. subclass has the methods and attributes of the parent class;
2. Improve code scalability. sub-classes can be similar to parent classes, but different from parent classes, retaining their own characteristics;

Disadvantages:
1. inheritance is invasive. As long as inheritance is inherited, it must have all the methods and attributes of the parent class, which restricts the subclass to a certain extent and reduces the flexibility of the Code;
2. Added coupling. When the constant, variable, or method of the parent class is modified, you need to consider the modification of the subclass. Therefore, once the parent class has changed, it may cause
Very bad results. A lot of code needs to be reconstructed.

Object-Oriented Design Principles liskov substitution principle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.