User Tools

Site Tools


principles:liskov_substitution_principle

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
principles:liskov_substitution_principle [2016-03-07 22:17]
66.228.70.98 [Strategies] fix typo (waken -> weaken)
principles:liskov_substitution_principle [2018-05-27 18:40] (current)
2600:6c56:7a00:e45:0:7bb6:125a:4a85
Line 17: Line 17:
 ===== Description ===== ===== Description =====
  
-Object-oriented programming languages ​allow to derive ​subtypes from base types and subtype polymorphism allows ​to pass an object of a subtype where ever an object of the supertype is specified. Suppose ''​P''​ and ''​Q''​ are types (i.e. classes or ''​interface''​s) and ''​Q''​ is derived from ''​P''​ (so ''​Q''​ is the subtype and ''​P''​ is the base type or supertype). A method ''​m''​ requiring a parameter of type ''​P''​ can be called with objects of type ''​Q''​ because every object of type ''​Q''​ is also an object of type ''​P''​. This is always true as typically object-oriented programming languages are constructed in that way.+Object-oriented programming languages ​permit the derivation of subtypes from base typesand subtype polymorphism allows ​the passing of an object of a subtype where ever an object of the supertype is specified. Suppose ''​P''​ and ''​Q''​ are types (i.e. classes or ''​interface''​s) and ''​Q''​ is derived from ''​P''​ (so ''​Q''​ is the subtype and ''​P''​ is the base type or supertype). A method ''​m''​ requiring a parameter of type ''​P''​ can be called with objects of type ''​Q''​ because every object of type ''​Q''​ is also an object of type ''​P''​. This is always true as typically object-oriented programming languages are constructed in that way.
  
-But the programming language does not enforce that the subtype ​also behaves like the supertype. Method ''​m''​ may work with an object of type ''​P''​ but not with an object of type ''​Q''​. LSP demands that a subtype (''​Q''​ in the example) has to be constructed in a way that it behaves like the supertype if it is called through the supertype interface. ''​Q''​ may have further methods and it may do additional things not observable by ''​m''​ but ''​m''​ shall be able to safely assume that its parameter behaves like an object of type ''​P''​ with respect to all observable state.+The programming language does not enforce that the subtype behaves like the supertype. Method ''​m''​ may work with an object of type ''​P''​but not with an object of type ''​Q''​. LSP demands that a subtype (''​Q''​ in the example) has to be constructed in a way that it behaves like the supertype if it is called through the supertype interface. ''​Q''​ may have further methods and it may do additional things not observable by ''​m''​ but ''​m''​ shall be able to safely assume that its parameter behaves like an object of type ''​P''​ with respect to all observable state.
  
  
principles/liskov_substitution_principle.txt · Last modified: 2018-05-27 18:40 by 2600:6c56:7a00:e45:0:7bb6:125a:4a85