Liskov substitution principle

The Liskov substitution principle is a design principle in object-oriented software development. It is regarded as one of the five most important design principles used by programmers. This and other principles were developed to make programming more logical and reduce the chance of errors popping up in other parts of code. The Liskov substitution principle essentially says that subsets should have the same qualities of a master category, so the subsets can be substituted for the master category in all instances.

rssalemscience-20220201-6-191546.jpgrssalemscience-20220201-6-191779.jpg

Background

Computers require programs and other operating information that tell them what to do. These are collectively called software and include procedures, programs, and routines. They are called software to set them apart from hardware, which refers to the physical parts of computer systems. The major types of software are system software, which controls the internal operations of the computer through an operating system and connected devices such as monitors and printers; and application software, which directs the computer to execute user commands and includes word processing and spreadsheets. Communication between computers in a network are managed by network software.

Early computer programming involved long algorithms full of go-to instructions that told the computer if a specific condition is satisfied, it should “go to” another part of the program. Algorithms are mathematical instructions. Computers use them to follow steps to complete a task. Some computer algorithms allow for machine learning, such as the ability to recognize patterns. The go-to instructions were written into the programs to keep them on track in case the code led to situations the programmer did not anticipate. These programs were difficult to read because they were so complicated and frequently unpredictable. Things began to change in programming in the 1960s with contributions from some newcomers in the field.

Barbara Liskov studied mathematics at the University of California, Berkeley. She then earned her doctorate in computer science at Stanford University in 1968. She wanted to approach programming from a mathematical perspective by attacking the problem logically. She went on to work in industry and programming methodology.

Liskov began teaching at the Massachusetts Institute of Technology (MIT), where she led a team in creating CLU, meaning “cluster.” CLU was the first programming language that was not built on go-to instructions. Instead, it organizes code into modules using a method called data abstraction, which Liskov created. All significant programming languages used in modern times owe their development to CLU. Liskov introduced her substitution principle in 1987 and published it with Jeanette Wing several years later.

Alan Kay completed his doctorate in computer science in 1969 at the University of Utah after studying mathematics and molecular biology at the University of Colorado. He coined the term object-oriented programming (OOP) while in grad school. While working for Xerox Corporation during the 1970s, he worked on one of the first OOP languages, Smalltalk. This grew out of his interest in how children could use computers. Kay was influential in developing ideas that led to laptop computers and was involved in developing laser printing, Ethernet, and other technology.

OOP makes programming faster by using modular units such as classes, subclasses, and objects. Use of modular units also makes maintaining programming easier. A class is a software blueprint for objects. It defines the methods and variables specific kinds of objects have in common. Objects are useful because much information is hidden but the operations are accessible, so programmers can focus on individual parts of the program. Java became the premiere object-oriented language during the 1990s and remains among the most successful in modern times.

Overview

A number of principles are regarded as important in OOP. Five of the most significant, according to software engineer Robert C. Martin, are the single responsibility principle, open/closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion, known by the acronym SOLID. These principles address problems he identified, such as code to which almost every change requires further adjustments to dependent modules, or breakage of code in multiple locations when a change is made. Such problems can appear in areas of code that are seemingly unrelated to the areas in which changes are made.

The single responsibility principle states that only one reason should exist for a class to change. If multiple reasons for change exist, complex classes should be broken down into smaller classes with focused responsibilities. According to the open-closed principle, a class or module should be open for extension but closed for modification, or modules should be written so they do not have to be modified to add features. The interface segregation principle holds that breaking main classes into several more specific classes is better than trying to work with a single large, generalized class. According to the dependency inversion principle, one should depend on abstractions, not concretions. High-level modules provide complex logic, while low-level modules provide utility features. High-level modules should not be affected by changes in low-level modules. Both high-level and low-level modules should depend on abstractions. Abstraction means to hide the details that are not necessary for the user to see.

According to the Liskov substitution principle, objects of a superclass (also called supertype) should be replaceable with objects of its subclasses (subtypes) without breaking the application, or changing the correctness of the program. For this to work, the objects of the subclasses must act in the same way as the objects of the superclass. A superclass has characteristics that all its subclasses share. However, subclasses can override some actions of the superclass.

A general way of looking at superclasses, subclasses, and substitution is to look at the mammal family Canidae. This family includes more than thirty species, but they all have some traits in common. All species have long, pointed snouts and long legs. They have five toes on their front feet and four on their hind feet; their feet are padded and have non-retractable claws. They all eat meat. The subtypes do not break the functions of the parent group; none have retractable claws, for instance. Any species can represent Canidae. However, subtypes may override some actions. Domestic dogs, for example, evolved to live with humans. Some Canidae are omnivores.

Bibliography

Bagala, Fabrizio. "Liskov Substitution Principle." DEV Community, 18 Nov. 2023, dev.to/fabriziobagala/liskov-substitution-principle-1o1k. Accessed 18 Nov. 2024.

“Barbara Liskov.” A.M. Turing Award, 2008, amturing.acm.org/award‗winners/liskov‗1108679.cfm. Accessed 24 Feb. 2022.

D’Agostino, Susan. “The Architect of Modern Algorithms.” Quanta Magazine, 20 Nov. 2019, www.quantamagazine.org/barbara-liskov-is-the-architect-of-modern-algorithms-20191120/. Accessed 24 Feb. 2022.

Elliott, Eric. “The Forgotten History of OOP.” Medium, 31 Oct. 2018, medium.com/javascript-scene/the-forgotten-history-of-oop-88d71b9b2d9f. Accessed 24 Feb. 2022.

Geuze, Marco. “SOLID Principles in Delphi[3]—The Liskov Substitution Principle.” GDK Software, gdksoftware.com/knowledgebase/solid-principles-in-delphi-3-the-liskov-substitution-principle. Accessed 24 Feb. 2022.

Gillis, Alexander S., and Sarah Lewis. “Object-Oriented Programming (OOP).” TechTarget, July 2021, www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP. Accessed 24 Feb. 2022.

Janssen, Thorben. “SOLID Design Principles Explained: The Single Responsibility Principle.” Stackify, 1 Apr. 2020, stackify.com/solid-design-principles/. Accessed 24 Feb. 2022.

Liskov, Barbara. “How Data Abstraction Changed Computing Forever.” TED Talks, May 2019, www.ted.com/talks/barbara‗liskov‗how‗data‗abstraction‗changed‗computing‗forever. Accessed 24 Feb. 2022.

“Liskov Substitution Principle in Java.” Baeldung, 15 Aug. 2020, www.baeldung.com/java-liskov-substitution-principle. Accessed 24 Feb. 2022.

“Women in STEM with Dr. Barbara Liskov.” Institute of Electrical and Electronics Engineers Computer Society, 2021, www.computer.org/publications/tech-news/events/women-in-stem-barbara-liskov. Accessed 24 Feb. 2022.