Closed martinjonsson01 closed 1 year ago
Nicely done! I'll mark this as resolved, but I still have some comments on your new reflection.
Because in this case: Volvo is a subtype of Car and the gas object in Volvo is the same as Car, so it means that it satisfies the contract of Car.
True, but you can fulfill the contract of a method while still changing what it does. You just need to be careful in order to preserve the expected behaviors.
We try to follow the Liskov Substitution principle but in the case of Scania when the car accelerates, the object for gas does not behave the same as the gas object in the car supertype.
However, Scania unfortunately doesn't [fulfill LSP].
This is still not really right. Since you now have specified the contract for gas in Car to be:
our gas contract has a condition which has to be fulfilled in order for it to work.
then Scania does not break LSP, since it still does what Car's contract for gas says. To clarify: the reason Scania no longer breaks LSP with its gas method is because you have written the contract in Car such that it allows for subtypes to implement different conditions. If you had not mentioned the condition at all in Car's gas contract, then you'd be breaking LSP.
We inherit the gas object from Car
I assume you mean gas method.
https://github.com/ghazalghorabi/TDA553-lab1/blob/01f3360df2ab6d8a3dae4522fac81312009bb5d9/src/lab2reflection.txt#L2-L10
You write:
which does not sound right. According to LSP, subtypes are not allowed to have stricter preconditions than their supertype. By adding an extra condition in Truck, you've strengthened the preconditions, which goes against LSP.
The contract you've given gas and brake in Car is actually not very easy to fulfill, as you seem to imply (but never out-right state) that the contract for gas is to accelerate the car non-conditionally. This is quite hard for subtypes to fulfill, since they often want to add extra conditions before allowing acceleration, thereby actually making it quite difficult to fulfill the contract.
Try again to concretely write down what the method contract is for gas and brake in Car. As I've already told you, your current design breaks LSP. Can you find a way to make your design follow the LSP? (Hint: This does not involve changing the code at all.)