18
Static and Dynamic Behavior CMPS 2143

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

Embed Size (px)

Citation preview

Page 1: 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

Static and Dynamic Behavior

CMPS 2143

Page 2: 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

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

Page 3: 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

3

This lecture

•Static versus dynamic typing•Static versus dynamic classes in statically typed

languages•Static and dynamic binding of message and methods

Page 4: 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

4

Static vs Dynamic typingOOP Non-OOP

Statically typed C++, Delphi Pascal, Eiffel Ada, Algol, C, FortranC#, Java, Objective-C Haskell, ML, Modula

Dynamically typed Objective-C, Smalltalk APL, Forth, LispPython Prolog, Snobol

Page 5: 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

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

Page 6: 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

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;}

Page 7: 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

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 (…);

Page 8: 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

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 run-time based on its current dynamic value.

•Example: Assume classes Animal, Dog, Seal, Cat, Bird

Page 9: 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

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

Page 10: 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

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

Page 11: 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

11

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 (aPet instanceof Dog)

Page 12: 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

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;

Page 13: 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

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.

Page 14: 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

14

Javaclass 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 !

Page 15: 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

15

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 !

Page 16: 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

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!

Page 17: 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

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++.

Page 18: 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

18

Study questions

•Pg 233: 1-3, 5,6, 8,9