1 / 15

Inheritance

Inheritance. Ohad Barzilay IDC, May 2004. Inheritance - terminology. A descendant of a class C is any class that inherits directly or indirectly from C (including C). A proper descendant – a descendant of C other than C.

hoyt-mccray
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Ohad Barzilay IDC, May 2004

  2. Inheritance - terminology • A descendant of a class C is any class that inherits directly or indirectly from C (including C). • A proper descendant – a descendant of C other than C. • An (proper) ancestor of C is a class A such that C is a (proper) descendant of A.

  3. A bit of Logic A, B logical expressions • A  B  ¬A  B • A is “stronger” than B: A  B • A is “weaker” than B: B  A • A  (A  B) B  (A  B) • (A  B)  A (A  B)  B

  4. Subcontracting • Invariants • The invariant of a class is the conjunction (AND) of its own invariant and the invariants of all its ancestors. • The invariants of all the ancestors of a class apply to the class itself.

  5. Subcontracting • Preconditions • The precondition of an overridden method must be weaker than the precondition in the ancestors. • Precondition are disjuncted (OR) over redefinitions. • The new version must accept all calls that were acceptable to the original.

  6. Subcontracting • Postconditions • The postcondition of an overridden method must be stronger than the precondition in the ancestors. • Postconditions are conjuncted (AND) over redefinitions. • The new version must guarantee at least as much as the original.

  7. class Foo { … /** * @pre i > 10 * @post $ret > 50 */ int foo(int i) { // do some work return 51; } class FooBar extends Foo { … /** * @pre i < 0 * @post $ret > 100 */ int foo(int i) { // do some work return 101; } Example FooBar:foo accepts (i < 0 || i > 10)

  8. Using abstract precondition • Try to think about examples with real classes • When Car extends Vehicle what is the precondition for drive() ? • When BoundedStack extends Stack what is the precondition for put() ? • How can we avoid from making the precondition stronger?

  9. “Or” vs. “Implies” Or Why be negative?

  10. Consider the following hierarchy: ReadOnlySet ListSet SubSet

  11. ReadOnlySet /** * Membership test. * @pre x != null, "Non-null element“ * @post !empty() || $ret == false, * "If empty, return false" */public boolean has(Object x);

  12. ListSet /** * @post $ret != true || rep.item.equals(x), * "If returned true, element equals the current * item" */public boolean has(Object x) { rep.start(); rep.find(x); return !rep.after();}

  13. Subset /** * @post $ret != true || test(x), * "Return true if the element satisfies the * condition" */public boolean has(Object x) { return base.has(x) && test(x);}

  14. Using implies ReadOnlySet empty()  $ret==false ListSet SubSet $ret==true  rep.item.equals(x) $ret==true  test(x) In JMSAssert use implies to denote “”

  15. Inheritance in JMSAssert

More Related