Typing
-
A variable or identifier is simply a
name - a handle to access value
-
A value is a string of bits - the current contents
in main memory
-
In statically typed languages , types associated with
variables (e.g., Pascal, C)
-
In dynamically typed languages,types associated with values
(e.g., Lisp,Snobol)
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:
-
Instance of child class is in all respects representative
of parent
-
Thus, reasonable to assign child instance to variable of
parent class
-
However, reasonable parent instance to child class not generally
permitted
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
-
Class of declaration is static class for variables
-
Class of value is dynamic class of the variable
-
Even statically typed OOP languages permit these to differ
Subclasses and Subtypes
-
A subclass is a class formed using inheritance
-
A subtype is a class that can be substituted ( using polymorphism)
with no observable effect
-
not all subclasses are subtypes
-
In some languages (e.g., C++) can only be formed using subclasses
-
In other languages (e.g., smalltalk, Objective-C) subclasses
and subtypes are totally independent
-
In java subtypes are formed using subclasses or using interfaces
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
Two
Aspects 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
-
reverse polymorphism often encountered with collection of
values
- given list of
items from the parent class, determine whether extracted item is
subclass instance
-
generally occurs in languages with single inheritance tree
-
Solving problem generally requires values to "know" their
own type
Static and dynamic Method binding
Should binding be on static class of variable or
dynamic class of variable?
-
Alice holds small Mammal - asks Bill "does this animal give
birth to live young?"
Static answer:
All mammals give birth to live young's, therefore yes
-
What if mammal is platypus?
Dynamic answer:platypus
lay eggs ,therefore no
-
Even statically typed OOP languages can use dynamic binding
But may use static
type to determine legality of operation
Merits of Static versus Dynamic Binding
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
-
All values polymorphic -maintain their own type information
-
Legality of message passing not checked until runtime
-can
be tested directly by programmers if desired
-
Reverse polymorphism not a problem in dynamically typed languages
-
Instances of classes are statically typed, not polymorphic
-
pointers and references can hold polymorphic values
-
Originally, values did not have any type information
-
now does have limited info with RTTI facilities
-
Methods binding can optionally be performed using dynamic
class for pointers and references (using virtual)
-
Legality of message passing checked at compile time based
on static type
-
Originally, no facilities for testing of reverse polymorphism
-
- now does have dynamic_cast and typeid
-
Assignment of reverse polymorphism variables can be performed
using cast or Dynamic_cast
-
All variables know their dynamic type
-
Reverse Polymorphism performed with a cast
-
- cast is dynamically checked.
-
Can test the run-time type of a value (instanceof)
-
No virtual keyword
-
Unique feature: data fields can also be overwritten
-
Subtype hierarchy can be formed using interfaces
-independent of subclass
hierarchy
-
Can be implemented by classes
-
Similar to classes,but have no implementation
-
Support inheritance, even multiple inheritance
-
Define subtypes - inheritance defines subclasses