A great example illustrating LSP (given by Uncle Bob in a podcast I heard recently) was how sometimes something that sounds right in natural language doesn’t quite work in code.
In mathematics, a Square
is a Rectangle
. Indeed it is a specialization of a rectangle. The “is a” makes you want to model this with inheritance. However if in code you made Square
derive from Rectangle
, then a Square
should be usable anywhere you expect a Rectangle
. This makes for some strange behavior.
Imagine you had SetWidth
and SetHeight
methods on your Rectangle
base class; this seems perfectly logical. However if your Rectangle
reference pointed to a Square
, then SetWidth
and SetHeight
doesn’t make sense because setting one would change the other to match it. In this case Square
fails the Liskov Substitution Test with Rectangle
and the abstraction of having Square
inherit from Rectangle
is a bad one.
Related Posts:
- What is an example of the Single Responsibility Principle?
- What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__()
- What is Inversion of Control?
- What __init__ and self do in Python?
- When should you use a class vs a struct in C++?
- C++ error ‘Undefined reference to Class::Function()’ [duplicate]
- What’s the difference between a method and a function?
- How do I implement interfaces in python?
- What is polymorphism, what is it for, and how is it used?
- java – invalid method declaration; return type required [duplicate]
- java – invalid method declaration; return type required
- Is C++ an Object Oriented language?
- Difference between abstraction and encapsulation?
- What are metaclasses in Python?
- extends class and implements interface in java
- What is a “driver class”?
- What is difference between functional and imperative programming languages?
- How can I create a copy of an object in Python?
- What is polymorphism in Javascript?
- When should I be using classes in Python?
- How to create a subclass in C#?
- How would one write object-oriented code in C?
- How to get a JavaScript object’s class?
- Inheritance vs. Aggregation [closed]
- What is the difference between an Instance and an Object?
- What does ‘low in coupling and high in cohesion’ mean
- Will WordPress become completely OOP?
- How to name files of namespaced classes?
- What is the best way to instantiate a class of a plugin in your WordPress theme?
- problem with implementing widget via the_content()
- Load classes using spl_autoload_register
- UML diagrams of WordPress
- Trying to get property of non-object
- Static vs Dynamic methods in WordPress
- Autoloader not finding classes from my plugin
- Integration tests test script enqueue/register fails
- Error of “Call a member function on non object” while var_dump get correct result
- How pass args to wp_list_comments callback?
- Does &$this is really disallowed to use anywhere?
- Don’t filters violate the a class’ local variables visibility rules?
- Admin submenu does not call function to load the page
- Using Geo Data Store Plugin Code
- How to wait for WordPress Core to load when writing OOP?
- OOP – from plugin add new object and call static method in another file
- How to get parameters with add_filter with a static method?
- Reading from file in c++ ifstream
- What is a mixin, and why are they useful?
- When should I use ‘self’ over ‘$this’?
- fatal error LNK1169: one or more multiply defined symbols found in game programming
- Meaning of @classmethod and @staticmethod for beginner?
- What is the difference between object-oriented languages and non object-oriented languages?
- C# Foreach statement does not contain public definition for GetEnumerator
- What is the difference between a heuristic and an algorithm?
- Pass arguments to Constructor in VBA
- How to remove a filter that is an anonymous object?
- Should all plugins be encapsulated in a Class?
- PHP error with shortcode handler from a class
- How to use classes declared in another plugin?
- add_action in namespace not working
- How to create a flexible abstraction for WP_Query?
- Does an activated plugin automatically mean its methods are available to other WP functions?
- How to initialize $wpdb?
- Best way to extends core classes in theme?
- register child class in another plugin
- Ajax Request not coming back to class
- How to include and use custom class files in plugin?
- Is it a good practice to include custom options when registering a post type?
- array of objects to do_action, callback gets singular object?
- How to Create database table when Plugin installed/activated, and delete database when Plugin deleted
- Calling custom plugin class methods from a template
- How to get media objects
- Using $post->post_name in body id causing error: Trying to get property of non-object
- Same taxonomy for different object types
- plugin class inheritance. cannot change variable
- Trying to get property of non-object “ wordpress ”
- What is the proper method of using global $post?
- Using plugin functions/methods within templates
- accessing parent variables in child construct without executing action in parent
- OOP and WordPress shortcode
- How to Set Taxonomy Object Description?
- Remove an action by extending class and replacing it
- Extend Woocommerce Widget Class – WC_Widget_Product_Categories
- Remove an action added within a class in a WordPress plugin
- Benefits over using object method over property from $product?
- Wp ajax not working from “current_screen” admin hook
- add_action in namespace not working
- functions won’t fire after I converted my code from procedural code to OOP
- Custom Post Type works when registered in functions.php but not when registered in plugin
- Pass an argument into a function to extract from array
- How to set a public variable in someone else’s plugin class
- How to implement AJAX within a class storing shortcode atts as class variables
- Meta Box not being added in plugin
- Creating a register settings class that supports extended classes
- Autoloading a PHP class – working outside, but not inside WordPress plugin
- Property value of null when value is assigned inside a method hooked to the wp action hook
- How to execute add_action() function from custom plugin to Crontrol plugin or do_action()?
- Class property not visible inside ajax callback function?
- How To Remove The Filter That Adds JetPack Related Content To Dom [closed]
- Strict Standards: Non-static method in sidebar generator
- Plugin: Custom menu item problem