2

Liskov says : you can't change the super class behaviors in child class.
Override says : you can change the super class behavior in child class.

I am confuse and i don't realize which one is correct?

Thank you for answers

Hakimeh Mordadi
  • 181
  • 1
  • 16
  • From [Wikipedia](https://en.wikipedia.org/wiki/Liskov_substitution_principle), "Substitutability is a principle [...] stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S, without altering any of the desirable properties of the program (correctness, task performed, etc.)" So you _can_ change the behaviours in the subclass, as long as you don't "alter any of the desirable properties of the program". – Sweeper Jun 30 '20 at 06:10
  • There are many gaps. 1. Liskov substitution principle is a principle which means it is a Good practice to do and it is not a Rule. 2. Override is a part of langauge syntax and langauge rules, (motivated by the concept of object orientedoverriding). 3. Liskov substitution principle never says you can't override or u should not override a method. It says that any child class object should be perfect substitution for parent class object. Suppose a Parent class Animal having a method walk and child class fish, now fish can't walk so it is a bad design, avoid such design. – nits.kk Jun 30 '20 at 06:48
  • Does this answer your question? [Liskov substitution principle - no overriding/virtual methods?](https://stackoverflow.com/questions/1735137/liskov-substitution-principle-no-overriding-virtual-methods) – jaco0646 Jun 30 '20 at 20:49

1 Answers1

2

Overriding is a technology. The Liskov Substitution Principle is a rule about how to use that technology. Many programming languages say you can override a method in a child-class, but Barbara Liskov says that you probably shouldn't in most cases.

Keep in mind that the Lisokv Substitution Principle does not say that you must never use override. You just should not use it in a way which causes code intended to work with your base class to not work anymore if it has to work with your child class instead. So if you override a method, you need to ensure that your override still fulfills the contract of the original method.

Philipp
  • 67,764
  • 9
  • 118
  • 153