LSP principle-the square is not a rectangle

Source: Internet
Author: User

The rectangle has two attributes: length and width. There is also a way to set the length and width, and a way to calculate the area.
As shown below
Private int length;
Private int width;
Public void setLength (int lenght ){
This. length = lenght;
}
Public void setWidth (int width ){
This. width = width;
}
Public int getArea (){
Return this. length * this. width;
}
If Square is a subclass of a rectangle. To ensure that the square length and width are equal, you must change the method corresponding to the square's second length and width.
Public void setLength (int lenght ){
This. length = lenght;
This. width = lenght;
}
Public void setWidth (int width ){
This. length = width;
This. width = width;
}
Let's look at the user's usage scenarios. We all know that the area of a rectangle is equal to the product of length and width. So when we use rectangles, we use them like this.
Rectangle rectangle = new Rectangle ();
Rectangle. setLength (5 );
Rectangle. setWidth (4 );
We want to know what the area is.
Rectangle. getArea ();
The result is 20. Of course, the result is very correct.
But what if we use a square instance for users?
Rectangle rectangle = new Square (); // Note: here the proxy principle is displayed. The user does not know that the real instance is a square, and the user only knows the rectangle.
Rectangle. setLength (5 );
Rectangle. setWidth (4 );
We want to know what the area is.
Rectangle. getArea ();
The result is 16, which violates the principle that the area of a rectangle is the product of length and width. The user will not understand why the answer is 16 if I set the length to 5 and 4 ?? Inconsistent with the premise
Therefore, a square cannot appear in this place instead of a rectangle.
That is to say, a square should not be considered as a subclass of a rectangle.

 

 

Where is the key to the square rectangle discussed previously? I think it is to change the side length of the rectangle. Can we consider that the side length of a rectangular instance should be variable? I think once the side length of a rectangle changes, it becomes another rectangle (a new instance ). Therefore, there should be no way to change the side length in the rectangle class. The side length of a rectangle instance should be determined when it is new, and they should be immutable. Based on this consideration, the rectangle and square classes I designed are as follows:
// Rectangle
Public class Rectangle {
Private final int width;
Private final int height;

Public Rectangle (int width, int height ){
This. width = width;
This. height = height;
}

Public int getWidth (){
Return width;
}

Public int getHeight (){
Return height;
}

Public int getArea (){
Return width * height;
}
}

// Square
Public class Square extends Rectangle {
Private final int side;

Public Square (int side ){
Super (side, side );
This. side = side;
}

Public int getSide (){
Return side;
}
}

This inheritance relationship conforms to the actual parent-child relationship and LSP. The reason for this design is that the methods of a class should not be able to change its nature. For example, there is a Men class, which can have the eat (), sleep (), work (), makeLovewith (Person p) methods, but if you define denatureToWomen () in it (), denatureToEunuch () is not appropriate, because it changes its nature, resulting in the Men instance no longer belongs to the Men class (at least not matching the reality. Unless the two methods cannot change the nature of the instance, defining the two methods in Men is inherently problematic.

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.