O'Reilly Forums: Does The Airplane Inhertance Example Violate Lsp? - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Does The Airplane Inhertance Example Violate Lsp?

#1 User is offline   andypb 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 04-February 14

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users