1

LSP Principle -- if D is a subtype of B, then objects of type B in a program may be replaced with objects of type D without altering any of the desirable properties of that program.

for eg:

Derived d = new Derived();
Base b = new Base();
//as per the principle, the below code should work
b = new Derived();
b = d;

.Net framework takes care of this. As a developer I don't have anything to do with this. Then what is the use of defining such principle?

Rajes
  • 1,036
  • 10
  • 15
  • That just takes care of the type casting -- as a developer you need to ensure that a) any methods that are implemented in the derived classes don't cause unexpected behavior and b) that the consuming code isn't dependent on something that is specific to the implementation of the base class. It's tricky, because sometimes the details of the base implementation leak out into the consuming code and the derived class doesn't deliver it as expected. – dbugger Jul 12 '16 at 13:55

1 Answers1

1

As a developer, you can make a mistake, and code S in a way, that makes it unsuitabe as substitute for T, even if language and runtime don't hinder you to use it as such -- this might be behaviour inside the language (e.g. throwing exceptions in S, that T does not), or a side-effect that violates the side-effect constraints advertised by T, ad nauseam

See Can you explain Liskov Substitution Principle with a good C# example? for an examplea

Community
  • 1
  • 1
Tom Regner
  • 6,856
  • 4
  • 32
  • 47