Questions tagged [liskov-substitution]
For questions about Liskov substitution principle in object-oriented design.
103 questions
7 votes
3 answers
546 views
What SOLID principles does the Java List interface challenge?
The Java List<E> interface includes methods, such as add(E) and remove(Object), that work as the names suggest on instances of mutable concrete implementations, such as ArrayList<E> and ...
5 votes
7 answers
585 views
Exceptions and the Liskov Substitution Principle
Consider the following scenario. I have an interface IService: public interface IService { void DoSomething(); } with an implementation: public class Implementation : IService { // This might ...
2 votes
4 answers
315 views
Does changing the default value of a class member violate the Liskov Substitution Principle?
For example, I have a Label class where the default font color is black: public class MyLabel{ protected int r=0; protected int g=0; protected int b=0; public void setRGB(int r,int g,...
3 votes
2 answers
415 views
Invariant rule in Liskov Substitution Principle
From Liskov Substitution Principle, I am still not very clear about the invariant rule. I read through many posts but I still have doubts. My example is picked from this blog, the example is slightly ...
2 votes
2 answers
452 views
In "Liskov Substitution Principle", are "Preconditions can't be strengthened in a subtype" & "Postconditions can't be weakened in a subtype" the same?
According to Is this a violation of the Liskov Substitution Principle?, as I understand, the top answer currently says the code below is violating "Liskov Substitution Principle": public ...
21 votes
6 answers
7k views
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
In Python 3, I subclassed int to forbid the creation of negative integers: class PositiveInteger(int): def __new__(cls, value): if value <= 0: raise ValueError("value ...
0 votes
3 answers
314 views
Use aggregation like LSP in C++
I was reading about LSP (Liskov Substitution Principle) in a book called Clean Architecture: A Craftsman's Guide to Software Structure and Design. I have a question regarding how this would be ...
6 votes
1 answer
4k views
What are the examples of breaking Liskov Substitution Principle?
I'm aware that there are 4 ways to violate Liskov Substitution from here. But I'm not sure what these violations would like in practice. Can you show examples of code that breaks the principle for ...