Conformance Flashcards
What is the meaning of the “Same or better” principle?
An overriding method should have at least the same functionality as the overridden method.
Namely:
Overriding should not pose surprises to client
What is the liskov substitution principle?
functions that use pointers or references to base classes
must be able to use objects of derived classes -
without knowing it!
What’s the meaning of no-variance?
The argument’s type cannot be changed
What’s the meaning of Covariance?
Change argument’s type in the same direction as the inheritance direction
What’s the meaning of Contravariance?
Change argument’s type in the opposite direction of the inheritance direction
is function arguments covariance safe?
No.
is function arguments contravariance safe?
Yes
is return type covariance safe?
yes
How variance works in c++ and java?
return type - covariance arguments - no-variance If a method has a covariant argument, It does not override the “old” method in java: overloading in c++: hiding
What’s the diffrence between java, smalltalk and c++ regarding access conformance?
smalltalk - all methods are public.
java - can only improve visibility in a subtype
c++ - not enforced.
What does design by contract means?
A method can declare pre-conditions - Expected arguments values A method can declare post-conditions - Guaranteed return value A class can declare invariants - Object’s state (when stable) Contract is checked at run time - Like an assert – exception thrown upon failure
What is the conformance in design by contract?
Pre-condition Conformance-
Overriding method must demand the same or less from its client (preconditions are or’ed).
Post-condition Conformance-
Overriding method must ensure the same or more to its client (post-conditions are and’ed).
Invariants Conformance-
Derived must keep Base invariants; can add invariants guarding any newly introduced fields.