Logo

dev-resources.site

for different kinds of informations.

Liskov Substitution Principle

Published at
4/6/2021
Categories
liskov
lsp
solid
oop
Author
iggredible
Categories
4 categories in total
liskov
open
lsp
open
solid
open
oop
open
Author
10 person written this
iggredible
open
Liskov Substitution Principle

Liskov Substitution Principle

The Liskov Substitution Principle (LSP) states that when you substitute a parent object reference with an object of any of its subclasses, the program should not break.

If a thing looks like a duck and it quacks like a duck, then it better be a duck.

Suppose that your duck has a baby duck. The baby duckling also looks like a duck and quacks like a duck and it also... breathes fire. Whoopsie. That's not supposed to happen.

In this quack analogy, the duck is the parent class. It exhibits duck-like attributes. These duck-like attributes are parts of the duck contract (a duck contract sets up a certain expectation of what a duck class should be like). The duckling is the subclass. Being a descendent of a duck, you expect it to also exhibit duck-like attributes. However, this fire-breathing ability violates the contract because ducks don't breathe fire. This is a violation of the Liskov Substitution Principle.

The LSP doesn't prevent the subclasses from adding new methods. A babyDuck class can have differing attributes like size = "small" and color = "yellow" while the parent class has attributes size = "medium" and color = "brown". These differences won't violate the LSP because they fall within the user expectation. The subclasses don't need to return identical outputs as the parent, but they need to be consistent. A duck subclass with fireBreathing = true attribute is not consistent because it is not the trait of a duck. It is a duck-dragon chimera.

At the heart of LSP, it's all about having consistent interfaces and following contracts.

lsp Article's
30 articles in total
Favicon
How to Setup Vim for Kotlin Development
Favicon
Managing LSPs in Neovim: Enable/Disable for the Entire Session
Favicon
Integrating the ruff language server
Favicon
Series Belajar Solid Principle - Liskov Substitution Principle (LSP)
Favicon
Avoid conflicts between denols and tsserver in Neovim
Favicon
Avoid conflicts between denols and tsserver in Neovim
Favicon
Implementing Payment Gateways with the Liskov Substitution Principle
Favicon
SOLID Principles: Liskov Substitution Principle (LSP)
Favicon
Configure Helix to use ElixirLS
Favicon
The Power of Liskov Substitution Principle in Improving Software Design
Favicon
Neovim config from scratch (Part II)
Favicon
Neovim config from scratch (Part I)
Favicon
Integrating a Language Server (LSP) in a VS Code Extension
Favicon
My Neovim setup for React, TypeScript, Tailwind CSS, etc
Favicon
How i setup neovim for speed up React, Js, Ts, etc
Favicon
Solve nvim lsp denols vs tsserver clash
Favicon
Perl Navigator - Language Server for Perl
Favicon
Neovim LSP Setup + Code Completion Engine
Favicon
NeoVim: init.lua, built in LSP e mais
Favicon
How to fix [deno-ts 2339] in the code for Deno Deploy
Favicon
SOLID e o princรญpio do 6 por meia dรบzia
Favicon
Java LSP in Sublime Text
Favicon
Neovim: Migrando do coc.vim para nvim-lsp
Favicon
Integrating Scala Metals with Doom Emacs using LSP on Pop!_OS/Ubuntu 20.04 LTS
Favicon
Builtin Solidity Language Server
Favicon
Liskov Substitution Principle
Favicon
Emacs as SQL client with LSP
Favicon
Vim Configuration from Minimal to Complete
Favicon
LSP and ISP: The LI of SOLID
Favicon
Go LSP in Sublime Text

Featured ones: