Does The Airplane Inhertance Example Violate Lsp?

  • Please log in to reply
No replies to this topic

#1 andypb


    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 February 2014 - 04:47 PM

Hello I am wondering if the Airplane example of inheritance given on pages 578/9 of the book violates the Liskov Substitution Principle?
To wit:

public class Airplane {
  private int speed;

  public void setSpeed(int speed) {
    this.speed = speed;
  public int getSpeed() {
    return speed;

public class Jet extends Airplane {
  private static final int MULTIPLIER=2;

  public void set setSpeed(int speed) {
    super.setSpeed(speed * MULTIPLIER);

  public void accelerate() {
    super.setSpeed(getSpeed() * 2);

Here the subclass cannot be substituted for its base class without changing expectations. For example, a client using a reference to an instance of an Airplane might be surprised, after setting the speed, to find it is twice as fast as expected after being passed an instance of a Jet. Isn't the Jet changing the postconditions for the setSpeed() method and thus violating LSP?

It is stated on this page "the subclass can change behaviour of its superclass, as well as call the superclass's methods. This is called overriding the superclass's behaviour." I come across many examples of doing exactly that in tutorials and examples on inheritance and it is often expressed as a desirable feature but it seems to me that this will be, in many cases, incompatible with LSP?

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users