Monday, September 16

Liskov Substitution Principle (LSP) : Design Principal , Java Design Principal






Liskov Substitution Principle (LSP)

According to Liskov Substitution Principle, methods or functions which uses super class type must be able to work with object of sub class without any issue".


Let us understand this with an example 


 

Public class Cat extends Animal {
}



public class Animal{
}


Public class TestLSP{


public void execute(Animal animal){

}
public void executor(){

Animal animal =new Animal();

execute(animal);
 

Cat cat = new  Cat();
execute(cat);
}






So we are able to call execute method from executor method by passing Animal class instance as well as Cat class instance .

 Cat class extends Animal So is subclass to Animal class

Thus Animal class parameter expected in executor method works well with Animal as well as subtype Cat type of instance . that's what LSP says. 






No comments:

Post a Comment