SOLID Principles: Liskov Substitution Principle (LSP)

Bad Example of LSP:

public class Bird{
public void fly(){}
}
public class Duck extends Bird{}

The duck can fly because it is a bird, But what about this:

public class Ostrich extends Bird{}

Ostrich is a bird, But it can’t fly, Ostrich class is a subtype of class Bird, But it can’t use the fly method, that means that we are breaking the LSP principle.

Good Example of LSP:

public class Bird{
}
public class FlyingBirds extends Bird{
public void fly(){}
}
public class Duck extends FlyingBirds{}
public class Ostrich extends Bird{}

ability to replace any instance of a parent class with an instance of one of its child classes without negative side effect.

continuo to the rest of SOLID

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store