190 likes | 414 Views
Static and Dynamic Behavior. CMPS 2143. 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
E N D
Static and Dynamic Behavior CMPS 2143
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
This lecture • Static versus dynamic typing • Static versus dynamic classes in statically typed languages • Static and dynamic binding of message and methods
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
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; }
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 (…);
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 run-time based on its current dynamic value. • Example: Assume classes Animal, Dog, Seal, Cat, Bird
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
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
Syntax test C++ Animal * aPet = ….; //pointer to an animal Dog * d = dynamic_cast <Dog *> (aPet); //null if not legal, nonnull if ok if (d != 0) { } Java if(aPetinstanceof Dog)
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;
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.
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(); //Woof ! pet = new Bird(); pet.speak(); //Tweet !
C++ class Animal { public: virtual void speak() {cout << “Animal Speak !”;} }; class Dog : Animal { public: virtual void speak() {cout << “Woof !”;} }; Animal pet; //automatic memory allocation!!! Dog d; d.speak(); //Woof ! pet = d; pet.speak(); //Animal Speak !
C++ class Animal { public: virtual void speak() {cout << “Animal Speak !”;} }; class Dog : Animal { public: virtualvoid speak() {cout << “Woof !”;} }; Animal * pet; //dyanmic memory allocation!!! Dog * d = new Dog(); D->speak(); //Woof ! pet = d; pet->speak(); //Woof!
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++.
Study questions • Pg 233: 1-3, 5,6, 8,9