Title: Static and Dynamic Behavior
1Static and Dynamic Behavior
- Fall 2005 OOPD
- John Anthony
2Topics
- Static vs. Dynamic Typing
- Static Classes vs. Dynamic Classes
- Static vs. dynamic method Binding
Lets forget about inheritance and polymorphism
for a few moments.
3Static vs. Dynamic
- Static almost always means fixed or bound at
compile time, and cannot thereafter be changed. - (consider OO without polymorphism)
- Dynamic almost always means not fixed or bound
until run time, and therefore can change during
the course of execution - (consider every variable as a polymorphic
variable)
4Static vs. Dynamic Typing
- Statically Typed variables have a declared type
which is fixed at compile time.(consider Java) - Dynamically Typed types are associated with the
values only. The variable is just a name. - The variable can hold different types during the
course of execution. (consider Smalltalk)
5Benefits and Drawbacks
- Static and Dynamically typed languages have
existed as long as there have been programming
languages. Arguments for and against - Static typing allows better error detection, more
work at compile time and hence faster execution
time. - Dynamic typing allows greater flexibility, easier
to write (for example, no declaration
statements). - Both arguments have some validity, and hence both
types of languages will continue to exist in the
future.
6Remember the polymorphic variable???
- A variable (or reference) that can be attached to
more than one type of object.
TeamMember member new TeamMember()
polymorphic variable
InnovationLabArchitect architect new
InnovationLabArchitect ()
non polymorphic variable
7Static Class vs. Dynamic Class
- In a statically typed language we say the class
of the declaration is the static class. The
static class is fixed at compile time and never
changes. - The dynamic class of a variable is the class
associated with the value it currently holds.
The dynamic class can change during the course of
execution.
8Remember
Dynamic class
Static class
9Importance of Static Class
- In a statically typed object-oriented language,
the legality of a message is determined at
compile time, based on the static class.not the
current dynamic receiver.
class Animal class Dog extends Animal
void bark() System.out.println("woof")
// will generate error since mammals don't
speakalthough //the value Dog could respond to
the message. Animal pet new Dog pet.bark()
10Reverse Polymorphism
- Polymorphism says we can assign a value from a
child class to an instance of the parent class,
but can this assignment then be reversed? - There are two specific problems associated with
the question of reverse polymorphism. - The problem of identity - can I tell if a value
declared as an instance of a parent class
actually holds a value from a subclass.
(instanceof in Java) - The task of assignment - can I then assign the
value from the parent class to a variable
declared as the subclass. (casting)
11Static vs. Dynamic Method Binding
- Should the binding of a message be determined by
the static class of a variable or the dynamic
class of the value? - Most OO languages use the static class to
determine the legality of the message (compile
time check). - While the correct (method) implementation is
determined by the dynamic class (run time).