Menu
  • HOME
  • TAGS

Does overloading violate Liskov Substitution Principle?

c++,solid-principles,lsp

The LSP Liskov's Substitution Principle (or LSP) is about abstraction. Imagine a class Shape and two classes Square and Rectangle deriving from Shape. Now Shape has a (virtual) method getArea(). You would expect it to return the area that is covered by the (concrete, instanced!) shape regardless of what type...

How to write an ImmutableMap that follows the Lisvok Subsitution and other SOLID principles without code smells?

java,interface,immutability,code-smell,lsp

In my view, an ImmutableMap should implement Map. It would be a bad idea not to implement Map as there are many methods that accept a Map as an argument and only use it in a read-only sense. I don't believe this does violate the Liskov Subsitution principle because the...

Does adding public method to subclass violate LSP (Liskov substitution principle)?

oop,solid-principles,lsp

No as long as all the methods inherrited from the parent class follow the same contract as the parent then LSP is preserved. the whole point of LSP is to be able to pass around a subclass as the parent class without any problems. It says nothing about not being...

Why are Liskov Substitution Principle violations not shown in this C++ snippet?

java,c++,lsp

In Java, methods are virtual by default. In C++, member functions are non-virtual by default. So to mimic the Java example, you need to declare the member functions virtual in the base class.

What am I misunderstanding about the Liskov Substitution Principle

php,lsp

What am I getting wrong? I think most helpful would be an example of how a value can be passed that violates the assumptions the code makes about that value. You didn't misunderstand LSP, allowing a bigger type in a subclassed method doesn't violate the principle; in fact, what...

What is a Refused Bequest?

solid-principles,duck-typing,lsp

I think you get it. Refused Bequest is a code smell. But, what type of code smell? Quoting Martin Fowler's book Refactoring: improving the design of existing code: Subclasses get to inherit the methods and data of their parents. But what if they don't want or need what they are...

Is the LSP restriction on strengthening of preconditions in conflict with the suggestions that the need to downcast indicates bad design

inheritance,class-design,downcasting,class-hierarchy,lsp

LSP is all about behavioral subtyping. Roughly speaking, B is a subtype of A if it can be always used where A is expected. Moreover, such usage shouldn't change expected behavior. So, considering LSP application, the main point is what "expected behavior of A" is. In your example, it is...

Inheritance and Liskov substitution principle

c#,design-patterns,lsp

I think what you're encountering is not so much a Liskov Substitution Principle violation as you are encountering a polymorphism limitation in most languages. With something like List<CalendarBaseItem> the compiler is inferring that you're only dealing with CalendarBaseItem which obviously can't be true if CalendarBaseItem is abstract--but that's what a...

Why it's impossible to override `var` with `def` in Scala?

scala,immutability,lsp,mutability,uap

That's related to the Liskov Substitution Principle: you can't assign weaker access privileges in subclass (even for Java). Making var a def makes the setter def x_= (y: T ): Unit private (as @Staix said). So in case when Seadan Car has formal type Car it should not be accessed,...