Keeping things tidy and simple, here is my initial Java pseudocode as an example:
public abstract class Vehicle {
private String owner;
private id plate;
public removeVehicle() {
if (typeof(this) == Car) {this.removeCar();}
if (typeof(this) == Truck) {this.removeTruck();}
}
}
public class Car extends Vehicle {
public removeCar() {
this.removeVehicle(this);
}
}
public class Truck extends Vehicle {
public removeTruck() {
this.removeVehicle(this);
}
}
From the book Agile principles patterns and practices in C# by Robert, Micah Martin says that it does not comply with Liskov principle since Vehicle
, in order to remove other vehicles types (e.g. Motorcicle
), needs to extend code and add another if
statement for this new type of vehicle.
I tried to follow solutions provided from here, but most are just taken from the book I just referrenced and I'm actively trying to properly understand.
How can I modify this pseudocode so it does comply with Liskov?
According to
"Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it."
see https://en.wikipedia.org/wiki/SOLID
The target of your exercise could be to define removeVehicle
in class Vehicle
as abstract
and implement it in the classes which extend Vehicle
.