The Liskov Substitution Principle states that a subtype should be substitutable for that type (without altering the correctness of the program).
- Can someone please provide an example of this principle in the domain of vehicles (automotives)?
- Can someone please provide an example of a violation of this principle in the domain of vehicles?
I've read about the square/rectangle example, but I think that an example with vehicles will give me a better understanding of the concept.
The Liskov Substitution Principle states that an object with a certain interface can be replaced by a different object that implements that same interface while retaining all the correctness of the original program. That means that not only does the interface have to have exactly the same types, but the behavior has to remain correct as well.
In a vehicle, you should be able to replace a part with a different part, and the car would keep working. Let's say your old radio doesn't have a digital tuner, but you want to listen to HD radio so you buy a new radio that has an HD receiver. You should be able to take the old radio out and plug in the new radio, as long as it has the same interface. On the surface, that means the electrical plug that connects the radio to the car needs to be the same shape on the new radio as it is on the old radio. If the car's plug is rectangular and has 15 pins, then the new radio's jack needs to be rectangular and have 15 pins as well.
But there are other considerations besides the mechanical fit: the electrical behavior on the plug has to be the same, too. If pin 1 on the connector for the old radio is +12V, then pin 1 on the connector for the new radio also has to be +12V. If pin 1 on the new radio was the "left speaker out" pin, the radio might short out, or blow a fuse. That would be a clear violation of the LSP.
You could also consider a situation of a downgrade: let's say your expensive radio dies, and you can only afford an AM radio. It doesn't have stereo out, but it has the same connector as your existing radio. Let's say the spec has pin 3 being left speaker out, and pin 4 being right speaker out. If your AM radio plays the monophonic signal out both pins 3 and 4, you could say that its behavior is consistent, and that would be an acceptable substitution. But if your new AM radio plays audio only on pin 3, and nothing on pin 4, the sound would be unbalanced, and that probably would not be an acceptable substitution. That situation would also violate the LSP, because while you can hear sounds, and no fuses blow, the radio doesn't meet the full specification of the interface.