Questions tagged [liskov-substitution]
For questions about Liskov substitution principle in object-oriented design.
103 questions
7
votes
3
answers
551
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 ...
2
votes
1
answer
200
views
Is widening parameter types in a PHP interface implementation a good practice?
I have an interface defined as follows:
interface MyInterface
{
public function setMyMethod(int $a);
public function getMyMethod(): int;
}
And I have a class that widens the parameter type:
...
5
votes
7
answers
587
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 ...
1
vote
3
answers
269
views
In "Liskov Substitution Principle", is "invariants can't be weakened in a subtype" a kind of "postconditions can't be weakened in a subtype"?
According to Invariant rule in Liskov Substitution Principle, I know one of the form of violation of "Liskov Substitution Principle" is violating "invariants can't be weakened in a ...
2
votes
4
answers
316
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,...
1
vote
1
answer
150
views
Can a standalone class be seen as violating the Liskov Substitution Principle
Say we have the interface
interface IColoredSquare
{
int GetSideLength();
void SetSideLength(int length);
string GetColor();
}
and I implement it like this
class ColoredSquare : ...
3
votes
2
answers
418
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 ...
0
votes
2
answers
233
views
Possible violation of LSP when adhering to ISP?
Recently I read about ISP and wanted to implement it into my project. While implementing my design I think I found a flaw which violates LSP but I'm not sure.
Given I have a game project, in which ...
2
votes
2
answers
456
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 ...
-1
votes
2
answers
268
views
Liskov Substitution Principle Edge Case
I thought I understood Liskov Substitution Principle but then I thought of a case and I wanted to ask the community here if I get it right.
So I read somewhere to check if we have violated Liskov or ...
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 ...
2
votes
3
answers
682
views
Object Oriented Programming - what is the best way to add new parameters?
I have a doubt about what would be the right OOP approach for implementing classes that do similar stuff with different parameters.
To provide a simple example, I would use two polynomial classes, ...
0
votes
3
answers
317
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 ...
3
votes
3
answers
487
views
Is there any redundancy within the scope of SOLID principles?
I have read in an article DIP in the Wild that
"When Robert Martin first discussed the DIP, he equated it a first-class combination of the Open Closed Principle and the Liskov Substitution ...
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 ...