University of Virginia, Department of Computer Science
CS201J: Engineering Software, Fall 2002

Notes: Thursday 17 October 2002
Schedule

Substitution

   MysteryType1 mt1;
   MysteryType2 mt2;
   MysteryType3 mt3;
   ... (anything could be here)
   mt1 = mt2.m (mt3);
If the Java compiler is happy with this code, which of these are guaranteed to be true:
  1. The apparent type of mt2 is MysteryType2
  2. At the last statement, the actual type of mt2 is MysteryType2
  3. MysteryType2 has a method named m
  4. The MysteryType2.m method takes a parameter of type MysteryType3
  5. The MysteryType2.m method returns a subtype of MysteryType1
  6. After the last statement, the actual type of mt1 is MysteryType1
class A {
   public RA m (PA p) ;
}
class B extends A {
   public RB m (PB a);
}
               Substitution Principle        Eiffel
Parameters     PB >= PA	              PB <= PA
Preconditions  pre_A implies pre_B    pre_B implies pre_A

Result         RB <= RA               RB <= RA
Postconditions post_B implies post_A  post_B implies post_A
#include <stdio.h>

class A {
  public: void other () { printf("is an empty func in A\n"); };
              virtual void other (class A *a) { printf("In A\n"); }
};
class B: public A {
   public: void other (class B *b) { printf("In B\n"); }
 };
class C: public A {
     public: void other (class C *c) { printf("In C\n"); }
 };
void main(void) {
    A a; B b; C c;
    A *aPtr = &a; B *bPtr = &b; C *cPtr = &c;
    aPtr = bPtr;
    aPtr->other(bPtr); 
    bPtr->other();      
}

CS201J University of Virginia
Department of Computer Science
CS 201J: Engineering Software
Sponsored by the
National Science Foundation
cs201j-staff@cs.virginia.edu