Search code examples
design-principlesliskov-substitution-principlesolid-principles

Does using virtual methods violates LSP( L part of SOLID principles) or there are some exceptions?


Does using virtual methods violates LSP( L part of SOLID principles) or there are some exceptions?

Thanks in advance, Saghar Ayyaz


Solution

  • Why do you think that? Virtual methods in themselves are just a mechanism for polymorphism. The Liskov Substitution Principle is a just constraint on how you implement the overrides (and other aspects of descendant classes) in order to get predictable polymorphic behaviour.