I am developing a simple project. And I have a conflict with meaning of Liskov Principle in my project.
I simplified my question of my project with this example:
public class Animal {
public void feed() {
// do something here
}
}
public class Dog extends Animal {
// some methods and attributes
@Override
public void feed() {
// never call parent feed() method (super.feed())
}
}
So, my question is, if I don't call parent method and write a completely new codes in override method, Is this violates Liskov Principle?
thanks.
LSP principle is about contracts when you have animal instance or get it from somebody and try to feed it you suppose that dog do what it should do instead of example fly away or trying to kill you. Proper dog implementation that fulfill Animal contract should fit to every place where expects Animal. When dog do something strange (something not in contract) it's violation of LSP. How exactly you implement methods of dog is not matter of context of LSP when they fulfill contracts.
https://en.wikipedia.org/wiki/Liskov_substitution_principle
No, it will not violate the Liskov Principle as long as the subclass' implementation satisfies the expectations of the base class.