Inheritance-DBC

Inheritance-DBC - more strict More precisely, B can weaken...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Computer Science 340 © Ken Rodham 2006 Inheritance & Design By Contract
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Our previous treatment of DBC ignored inheritance Now that you’ve had time to internalize DBC, let’s throw inheritance into the mix DBC leads to a better understanding of inheritance, and helps us apply it more effectively
Background image of page 2
3 Client A R INV A PRE A.R POST A.R A a = new A(); a.R();
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Client A R INV A PRE A.R POST A.R B R INV B PRE B.R POST B.R ? A a = new B (); a.R();
Background image of page 4
5 Conceptually, B’s implementation must honor A’s contract; otherwise, clients will break when using a B instead of an A B can provide a “better” implementation than A, but not a “worse” one What do “better” and “worse” mean? Square root example
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Subclasses can be less strict, but not more strict B can be less strict on clients than A, but not
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: more strict More precisely, B can weaken Rs pre-conditions PRE B.R can place fewer requirements on clients than PRE A.R , but not more PRE B.R <= PRE A.R 7 Subclasses can do more, but not less Bs behavior must be consistent with As contract B can do better than A, but not worse More precisely, B can strengthen Rs post-conditions POST B.R can promise more to clients than POST A.R , but not less POST B.R >= POST A.R B must honor As class invariants INV B can promise more to clients than INV A , but not less INV B >= INV A 8 Another example Stack example 9 Liskov Substitution Principle Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T....
View Full Document

This note was uploaded on 03/02/2012 for the course C S 340 taught by Professor Woodfield,s during the Winter '08 term at BYU.

Page1 / 9

Inheritance-DBC - more strict More precisely, B can weaken...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online