Typing                             Error Detection with Static Typing

  Statically typed languages permit type error detection at compile time

   double r;
 
    r = 'x';
 
  Above result in a compile time error

                                Polymorphic Variables

 Object-Oriented concepts introduce a new twist:

      class Dog extends Mammal { . . . }
      class Cat   extends Mammal { . . . }
       . . .
       Mammal pet;
       Dog fido;
       Cat felice;
 
        pet = fido;      //legal
        pet = felice;   // legal
        fido = pet;    //illegal !

                                    Static and Dynamic Classes

 class Dog extends Mammal { . . . }
 class Cat   extends Mammal { . . . }
       . . .
       Mammal pet;
       Dog fido;
       Cat felice;
 
        pet = fido;      //legal

                             Subclasses and Subtypes                           Reverse polymorphism

polymorphism allow value from child class to be assigned to variable of parent class

Under what conditions can value of parent class be assigned to variable of child class

  class Dog extends Mammal { . . . }
  class Cat   extends Mammal { . . . }
       . . .
       Mammal pet;
       Dog fido;
       Cat felice;
 
        pet = fido;      //legal
        fido = pet;    //illegal !

this is problem of reverse polymorphism
 

Problem of identity -whether the variable declared as parent class actually holds value from subclass

Task of assignment -assigning a value from parent class to a variable declared as a subclass

some languages mechanism address these two problems together

other languages mechanisms separate them
 

        - given list of items from the parent class, determine whether extracted item is
            subclass instance                          Static and dynamic Method binding

  Should binding be on static class of variable or dynamic class of variable?

        Static answer: All mammals give birth to live young's, therefore yes         Dynamic answer:platypus lay eggs ,therefore no         But may use static type to determine legality of operation
    Efficiency:  static binding requires fewer CPU cycles
 
  Error detection: static permits error detection during compile time

  Flexibility: dynamic binding permits greater flexibility, static creates rigidity and inhibits reuse
 

          -can be tested directly by programmers if desired           - pointers and references can hold polymorphic values           - now does have limited info with RTTI facilities        -independent of subclass hierarchy