Download presentation
Presentation is loading. Please wait.
Published byCarol Bradford Modified over 9 years ago
1
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 2
3
This lecture Static versus dynamic typing Static versus dynamic classes in statically typed languages Static and dynamic binding of message and methods 3
4
Static vs Dynamic typing OOPNon-OOP Statically typedC++, Delphi Pascal, EiffelAda, Algol, C, Fortran C#, Java, Objective-CHaskell, ML, Modula Dynamically typedObjective-C, SmalltalkAPL, Forth, Lisp PythonProlog, Snobol 4
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 5
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: 6 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: 7 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 run- time based on its current dynamic value. Example: Assume classes Animal, Dog, Seal, Cat, Bird 8
9
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 10
11
Syntax test C++ Animal * aPet = ….; //pointer to an animal Dog * d = dynamic_cast (aPet); //null if not legal, nonnull if ok if (d != 0) { } Java if (aPet instanceof Dog) 11
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; 12
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. 13
14
Java 14 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 !
15
C++ 15 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 !
16
C++ 16 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++. 17
18
Study questions Pg 233: 1-3, 5,6, 8,9 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.