Static and Dynamic Behavior

Download Report

Transcript Static and Dynamic Behavior

Static and Dynamic Behavior
CMPS 2143
2
Power of OOP
• Derives from the ability of objects to change their
behavior dynamically at run time.
• Static – refers to a property or feature that is bound at
compile time and thereafter cannot be modified
▫ int i = 5;
//i’s type bound at compile time
• Dynamic – refers to a property or feature that cannot be
bound until run time
▫ Python - i can change its type to a string during execution
3
This lecture
• Static versus dynamic typing
• Static versus dynamic classes in statically typed
languages
• Static and dynamic binding of message and methods
4
Static vs Dynamic typing
Statically typed
OOP
Non-OOP
C++, Delphi Pascal, Eiffel
Ada, Algol, C, Fortran
C#, Java, Objective-C
Haskell, ML, Modula
Dynamically typed Objective-C, Smalltalk
Python
APL, Forth, Lisp
Prolog, Snobol
5
Type
• All languages have concept of type
▫ Type may be property of variable (Statically typed)
 Attached at compile time
 int val;
 If implicit declared – inferred from program statements
 val = b + 2;
▫ Or type may be property of values (Dynamically typed)
 Attached at run time
 a <- 2.
 a <- true.
 a <- ‘true’. //now a is a string
6
Advantages/Disadvantages
• Statically typed languages
▫ Type checking can be done at compile time
▫ Memory layout can be determined at compile time for
automatic variables
▫ EFFICIENCE
• Dynamically typed languages
▫ FLEXIBILITY
▫ Example:
function max (left, right) {
if (left < right) return right;
return left;
}
7
Static and Dynamic classes
• OOP features in a statically typed language requires
relaxation of some of principles of static typing.
• Recall the principle of substitution.
▫ Static class used in declaration of object variables
▫ Dynamic class used to associate instance with value it
currently holds, which can change during execution.
 Example:
Employee e;
e = new Manager (…);
:
e = new Staff (…);
8
Cont.
• Legality of the message-passing expression is
determined at compile time based on the static class of
the receiver
• The actual receiver of the message is determine at runtime based on its current dynamic value.
• Example: Assume classes Animal, Dog, Seal, Cat, Bird
9
class Animal {
public void speak() {cout << “Animal Speak !”;}
}
class Dog : Animal {
public void speak() { bark();}
public void bark () { cout << “Woof !”;}
}
//static class is Animal, dynamic class is Dog
Animal pet = new Dog ( );
pet.speak();
//will work – Woof !
pet.bark();
//will not compile
10
Run-time type determination
• Principle of substitution can be viewed as moving a
value up the inheritance hierarchy.
• Occasionally we want to do the reverse.
▫ Need to be sure/determine if a value currently being held
by a variable declared of one static class type is, in fact,
derived from class that is lower.
▫ Example: Want to know if Animal object variable pet
actually is referencing a Dog object.
• Every OO language has ability to perform a test
11
Syntax test
C++
Java
Animal * aPet = ….; //pointer to an animal
Dog * d = dynamic_cast <Dog *> (aPet);
//null if not legal, nonnull if ok
if (d != 0) {
}
if (aPet instanceof Dog)
12
Downcasting (reverse polymorphism)
• Now you’ve determined that a value is from a given
class, convert the static type
• Language may combine the test and conversion (C++)
• Java example
Animal aPet;
Dog d;
d = (Dog) aPet;
13
Static versus Dynamic Method Binding
• In OOP languages, the binding of a method to execute in
response to a message is determined by the dynamic
value of the receiver.
14
Java
class Animal {
public void speak() {cout << “Animal Speak !”;}
}
class Dog extends Animal {
public void speak() {cout << “Woof !”;}
}
class Bird extends Animal {
public void speak() {cout << “Tweet !”;}
}
Animal pet = new Dog ( );
pet.speak();
pet = new Bird();
pet.speak();
//Woof !
//Tweet !
15
C++
class Animal {
public:
virtual void speak() {cout << “Animal Speak !”;}
};
class Dog : Animal {
public:
virtual void speak() {cout << “Woof !”;}
};
Animal pet;
Dog d;
d.speak();
//automatic memory allocation!!!
//Woof !
pet = d;
pet.speak();
//Animal Speak !
16
C++
class Animal {
public:
virtual void speak() {cout << “Animal Speak !”;}
};
class Dog : Animal {
public:
virtual void speak() {cout << “Woof !”;}
};
Animal * pet;
//dyanmic memory allocation!!!
Dog * d = new Dog();
D->speak();
//Woof !
pet = d;
pet->speak();
//Woof!
17
C++
• Object values referenced by pointers are polymorphic
(as long as methods are declared virtual).
• Other languages discussed in chapter 16.
▫ C# uses keyword virtual, but since it, like Java, doesn’t
have pointers – it’s rules not as complex as C++.
18
Study questions
• Pg 233: 1-3, 5,6, 8,9