SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Task 18: Inheritance (possible solution)

The problem is the violation of the “Liskov Substitution Principle”.

More specific, the implicit contract of the superclass contains the post condition for “add()” that the movie is added to the list 1) with no pre condition other than maybe that the movie is not null.

This pre condition is strengthened by the subclass implementation of “add()”. Now, the movie also has to have a specific age (rating) for the post condition to be true.

This is the exact opposite of what the subclass is allowed to do! It should “Demand no more, promise no less”

The following code snippet illustrates the problem:

public ShoppingCart {
  public MovieList list;
  public int total = 0;

  public buy(Movie movie) {
    list.add(movie);
    total += movie.price;
  }
}

ShoppingCart cart = new ShoppingCart();
cart.list = new PublicMovieList();
cart.buy(aMovie);
assertEquals(aMovie.price, cart.total);
1)
that is: retrievable with “toList()”
teaching/lectures/oosc/2009/task18_solution.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2022