Methods that use References to Base Classes
must be able to use Objects of Derived Classes
without Knowing It
Inheritance in Object Oriented Programming is one of the three Pillars of OOP, along with Encapsulation and Polymorphism.
Extending the State and Behavior of a Class by deriving a new class from an existing class allows us use common functionality and extends common functionality for specific uses in the child class.
This should create an “Is A” relationship.
The derived, the child class, is a new version of the base class.
It has extended the Behavior of the original base class.
When a Derived Class Replaces the Original Functionality Intent of the Base Class
… It has Violated the Liskov Substitution Principle
The derived Class should Add new functionality but Not change the functionality of the Base Class.
If a Concrete is instantiated from the Base Class you should be able to substitute ANY Derived Class for the instantiation without any behavioral issues as all the original behavior should be available.
In Mathematics the geometric objects of rectangle and square are the same type of object.
A rectangle is a square and a square is a rectangle.
They both have height and width properties that define their size. A square’s height and width values are equal where as a rectangle’s are different values.
This fact creates as “Is A” relationship in geometry.
In Object Oriented Programming (OOP) one of the three pillars is Inheritance.
Base Classes create common State and Behavior functionality that can be inherited, and sometimes overridden, by derived or child classes.
Class inheritance creates an “Is A” relationship.
A derived class is a version of the base class and inherits common functionality because it “Is A” base class type.
It is the expectation of the inheritance hierarchy that this relationship is pure.
An Apple is a Fruit
and a Fruit is an Apple
Let us now take a look at the square and the rectangle relationship in OOP.
If we assume that the Rectangle is the base class and a Square is a rectangle and we can then create a derived child class Square.
We inherit the State Properties of Height, Width, Area, Color and Name.
We also inherit the behavior actions of SetHeight, SetWidth, SetColor, SetName and GetArea.
Our Square can now create a unique object and not have to create that common functionality as it is available to it through inheritance.
With close examination we can see that there is a conflict in the inheritance schema
A Rectangle needs a Height and a Width but a Square only needs one or the other
If we try to use either Height or Width as the single value required by the Square we run the risk of the square not being created as we have intended.
By setting the Height to the Width we can get unexpected results when typed to the base type.
A Rectangle Type should be interchangeable with the Square Type without unwanted artifacts
This is not possible because a Square Is Not a Rectangle in OOP.
This violates the Liskov Substitution Principle.
We can comply with LSP if we change the inheritance model to the base class is a Shape and we derive a Rectangle and a Square from the Shape base class.
The Shape base class inherits State Properties of Color and Name and the Behavior actions of SetColor and SetName but creates the Height and Width with the Area method that uses only the values that the derived type requires.
With this inheritance structure we are free to create other more complex shapes that hold an “Is a Shape” relationship such as Triangles and Hexagons.
Barbara Liskov, born Barbara Jane Huberman on November 7, 1939 in California, is a computer scientist. She is currently the Ford Professor of Engineering in the MIT School of Engineering’s Electrical Engineering and Computer Science department and an Institute Professor at the Massachusetts Institute of Technology.
Liskov is a member of the National Academy of Engineering and a fellow of the American Academy of Arts and Sciences and of the Association for Computing Machinery (ACM). In 2002, she was recognized as one of the top women faculty members at MIT, and among the top 50 faculty members in the sciences in the U.S.A.
In 2004, Barbara Liskov won the John von Neumann Medal for “fundamental contributions to programming languages, programming methodology, and distributed systems”. On 19 November 2005, Barbara Liskov and Donald E. Knuth were awarded ETH Honorary Doctorates. Liskov and Knuth were also featured in the ETH Zürich Distinguished Colloquium Series.
Liskov received the 2008 Turing Award from the ACM, in March 2009, for her work in the design of programming languages and software methodology that led to the development of object-oriented programming. Specifically, Liskov developed two programming languages, CLU in the 1970’s and Argus in the 1980’s. The ACM cited her contributions to the practical and theoretical foundations of “programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing.”
The following two tabs change content below.
I am a Principal Architect at Liquid Hub in the Philadelphia area specializing in Agile Practices as a Certified Scrum Master (CSM). I use Test Driven Development (TDD) and Acceptance Test Driven Development (ATDD) with Behavior Driven Development (BDD) as my bridge to Agile User Stories Acceptance Criteria in a Domain Driven Design (DDD) implementing true RESTful services