20
S TATIC AND DYNAMIC B EHAVIOR Muhammad Adil Raja Roaming Researchers, Inc. cbna April 19, 2015 Muhammad Adil Raja ( Roaming Researchers, Inc. cbna Static and Dynamic Behavior April 19, 2015 1 / 20

Static and Dynamic Behavior

Embed Size (px)

Citation preview

Page 1: Static and Dynamic Behavior

STATIC AND DYNAMIC BEHAVIOR

Muhammad Adil Raja

Roaming Researchers, Inc.

cbna

April 19, 2015

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 1 / 20

Page 2: Static and Dynamic Behavior

OUTLINE I

1 INTRODUCTION

2 STATIC AND DYNAMIC TYPING

3 REFERENCES

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 2 / 20

Page 3: Static and Dynamic Behavior

Introduction

INTRODUCTION

How differences in static and dynamic features effectobject-oriented programming languages.Static versus Dynamic Typing.Static and Dynamic Classes in Statically Typed Languages.Static and Dynamic Method Binding in Statically TypedLanguages.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 3 / 20

Page 4: Static and Dynamic Behavior

Static and Dynamic Typing

WHAT DO THE TERMS STATIC AND DYNAMIC MEAN?

MESSAGE SYNTAX

In Programming languages: Static almost always means fixed orbound at compile time, and cannot thereafter be changed.Dynamic almost always means not fixed or bound until run time,and therefore can change during the course of execution.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 4 / 20

Page 5: Static and Dynamic Behavior

Static and Dynamic Typing

STATIC AND DYNAMIC TYPING

In a statically typed programming language (Java or Pascal), forexample, variables have declared types – fixed at compile time.In a dynamically typed programming language (Smalltalk orCLOS), a variable is just a name.Types are associated with values, not variables.A variable can hold different types during the course of execution.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 5 / 20

Page 6: Static and Dynamic Behavior

Static and Dynamic Typing

ARGUMENTS FOR THE AGAINST

Static and Dynamically typed languages have existed as long asthere have been programming languages.Arguments for and against:Static typing allows better error detection, more work at compiletime and hence faster execution time.Dynamic typing allows greater flexibility, easier to write (forexample, no declaration statements).Both arguments have some validity, and hence both types oflanguages will continue to exist in the future.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 6 / 20

Page 7: Static and Dynamic Behavior

Static and Dynamic Typing

THE POLYMORPHIC VARIABLE

The addition of object-oriented ideas in a statically typedlanguages adds a new twist.Recall the argument for substitution: an instance of a child classshould be allowed to be assigned to a variable of the parent class:

THE POLYMORPHIC VARIABLE

varpet : Mammal ;f i d o : Dog ;f e l i c e : Cat ;

beginpet := f i d o ; / / l e g a lpet := f e l i c e ; / / l e g a lf i d o := pet ; / / not l e g a l !

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 7 / 20

Page 8: Static and Dynamic Behavior

Static and Dynamic Typing

STATIC CLASS AND DYNAMIC CLASS

In a statically typed language we say the class of the declarationis the static class for the variable, while the class of the value itcurrently holds is the dynamic class.Most statically typed OO languages constrain the dynamic classto be a child class of the static class.

STATIC VS DYNAMIC

varpet : Mammal ;f i d o : Dog

beginpet := f i d o ; / / s t a t i c c lass i s Mammal, dynamic c lass i s Dog

end ;

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 8 / 20

Page 9: Static and Dynamic Behavior

Static and Dynamic Typing

IMPORTANCE OF STATIC CLASS

In a statically typed object-oriented language, the legality of amessage is determined at compile time, based on the static class.A message can produce a compile error, even if no run-time errorcould possibly arize:

class Mammal { }class Dog extends Mammal {

void speak ( ) { System . out . p r i n t l n ( " woof " ) ; }}

Mammal pet = new Dog ;pet . speak ( ) ; / / w i l l generate er ro r , Mammals don ’ t speak

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 9 / 20

Page 10: Static and Dynamic Behavior

Static and Dynamic Typing

REVERSE POLYMORPHISM

Polymorphism says we can assign a value from a child class to aninstance of the parent class, but can this assignment then bereversed?Under what conditions?This is known as the problem of reverse polymorphism.

REVERSE POLYMORPHISM

varpet : Mammal ;f i d o : Dog ;f e l i c e : Cat ;

beginpet := f i d o ; / / l e g a lf i d o := pet ; / / i s t h i s l e g a l ?

end ;

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 10 / 20

Page 11: Static and Dynamic Behavior

Static and Dynamic Typing

TWO ASPECTS OF REVERSE POLYMORPHISM

There are two specific problems associated with the question ofreverse polymorphism.The problem of identity – can I tell if a value declared as aninstance of a parent class actually holds a value from a subclass.The task of assignment – can I then assign the value from theparent class to a variable declared as the subclass.In some languages mechanisms are provided to address thesetwo problems together, while in other languages they areseparated.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 11 / 20

Page 12: Static and Dynamic Behavior

Static and Dynamic Typing

THE CONTAINER PROBLEM

The task of reverse polymorphism is often encountered inconnection with a collection of values - we have a list of items fromthe parent class (say a list of Mammals), and when we extract avalue we need to know if it is a more specific type.Generally occurs in languages with a single inheritance tree,where the only type we may have associated with a value is theclass “Object”.Solving this problem generally requires values to have “selfknowledge” of their own type. In some languages they do, in somelanguages values do not.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 12 / 20

Page 13: Static and Dynamic Behavior

Static and Dynamic Typing

STATIC AND DYNAMIC METHOD BINDING

Should the binding for information be associated with the staticclass of a variable or the dynamic class. Alice holds a smallMammal - asks Bill “does this animal give birth to live young”.Static answer – All mammals give birth to live young – thereforeyes.What if the Mammal is a platypus? Dynamic answer – Platypuslay eggs, therefore no.Even statically typed OOP languages can use dynamic binding.But may use static type to determine legality of operation.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 13 / 20

Page 14: Static and Dynamic Behavior

Static and Dynamic Typing

DOCUMENTING METHOD BINDING I

In many languages dynamic binding is the default.

If a child class overrides a method in the parent, using the sametype signature, then the selected method will be determined bythe dynamic type.

In other languages (C++, Delphi, C#) the programmer mustindicate which methods are dynamically bound and which arestatically type.

In C#, for example, this is done using the virtual keyword.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 14 / 20

Page 15: Static and Dynamic Behavior

Static and Dynamic Typing

DOCUMENTING METHOD BINDING II

METHOD BINDING

class Animal { Animal a ;public : Dog b ;

v i r t u a l void speak ( ) { cout << " Animal Speak ! \ n " ; } b . speak ( ) ;void r ep l y ( ) { cout << " Animal Reply ! \ n " ; } woof !

} ;

class Dog : Animal { a = b ;public : a . speak ( ) ;

ove r r i de void speak ( ) { cout << " woof ! \ n " ; } woof !void r ep l y ( ) { cout << " woof again ! \ n " ; }

} ; B i rd c ;c . speak ( ) ;

class Bi rd : Animal { tweet !public : a = c ;

v i r t u a l void speak ( ) { cout << " tweet ! \ n " ; } a . speak ( ) ;} ; tweet !

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 15 / 20

Page 16: Static and Dynamic Behavior

Static and Dynamic Typing

METHOD BINDING IN C++ I

C++ is the most complex language. Not only must theprogrammer use the virtual keyword, but true polymorphism onlyoccurs with pointer or reference variables.

We will see an explanation for the curious C++ semantics whenwe discuss memory management in the next chapter.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 16 / 20

Page 17: Static and Dynamic Behavior

Static and Dynamic Typing

METHOD BINDING IN C++ II

METHOD BINDING IN C++

class Animal { Animal ∗ a ;public : Dog ∗ b =new Dog ( ) ;

v i r t u a l void speak ( ) { cout << " Animal Speak ! \ n " ; } b−>speak ( ) ;void r ep l y ( ) { cout << " Animal Reply ! \ n " ; } woof !

} ;

class Dog : public Animal { a = b ;public : a−>speak ( ) ;

v i r t u a l void speak ( ) { cout << " woof ! \ n " ; } woof !void r ep l y ( ) { cout << " woof again ! \ n " ; } B i rd c = new Bi rd ( ) ;

} ; c−>speak ( ) ;

class Bi rd : public Animal { tweet !public : a = c ;

v i r t u a l void speak ( ) { cout << " tweet ! \ n " ; } a−>speak ( ) ;} ; tweet !

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 17 / 20

Page 18: Static and Dynamic Behavior

Static and Dynamic Typing

MERITS OF STATIC VS DYNAMIC BINDING

Arguments concerning static versus dynamic binding mirror thoseconcerning static versus dynamic typing.Efficiency – static binding uses least CPU cycles, dynamic bindingrequires more time.Error detection – static binding permits errors to be caught atcompile time rather than run-time.Flexibility – dynamic binding permits greater flexibility, staticbinding creates rigidity and inhibits reuse.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 18 / 20

Page 19: Static and Dynamic Behavior

Static and Dynamic Typing

SUMMARY

A statically typed language associated types with variables, adynamically typed language associates types with values.Static typing gives better error detection, better run-time efficiency,less flexibility.In a statically typed OO language, an object variable can still holdvalues from a child class.The static class is the class of the declaration, the dynamic classis the class of the value currently held.The legality of a message is checked using the static class.A message can be bound to a method using either the static ordynamic class. Most languages use the dynamic class.Some languages allow the programmer to choose which methodis used.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 19 / 20

Page 20: Static and Dynamic Behavior

References

REFERENCES I

Images and content for developing these slides have been takenfrom the follwoing book with the permission of the author.

An Introduction to Object Oriented Programming, Timothy Budd.This presentation is developed using Beamer:

CambridgeUS, dove.

Muhammad Adil Raja ( Roaming Researchers, Inc. cbna )Static and Dynamic Behavior April 19, 2015 20 / 20