Download presentation
Presentation is loading. Please wait.
1
Static and Dynamic Behavior Fall 2005 OOPD John Anthony
2
Topics Static vs. Dynamic Typing Static vs. Dynamic Typing Static Classes vs. Dynamic Classes Static Classes vs. Dynamic Classes Static vs. dynamic method Binding Static vs. dynamic method Binding Let’s forget about inheritance and polymorphism for a few moments….
3
Static vs. Dynamic Static almost always means fixed or bound at compile time, and cannot thereafter be changed. (consider OO without polymorphism) Dynamic almost always means not fixed or bound until run time, and therefore can change during the course of execution (consider every variable as a polymorphic variable)
4
Static vs. Dynamic Typing Statically Typed – variables have a declared type which is fixed at compile time. (consider Java) Dynamically Typed – types are associated with the values only. The variable is just a name. The variable can hold different types during the course of execution. (consider Smalltalk)
5
Benefits and Drawbacks Static and Dynamically typed languages have existed as long as there have been programming languages. Arguments for and against: Static and Dynamically typed languages have existed as long as there have been programming languages. Arguments for and against: Static typing allows better error detection, more work at compile time and hence faster execution time. Static typing allows better error detection, more work at compile time and hence faster execution time. Dynamic typing allows greater flexibility, easier to write (for example, no declaration statements). Dynamic typing allows greater flexibility, easier to write (for example, no declaration statements). Both arguments have some validity, and hence both types of languages will continue to exist in the future. Both arguments have some validity, and hence both types of languages will continue to exist in the future.
6
Remember the polymorphic variable??? A variable (or reference) that can be attached to more than one type of object. A variable (or reference) that can be attached to more than one type of object. TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); polymorphic variable non polymorphic variable
7
Static Class vs. Dynamic Class In a statically typed language we say the class of the declaration is the static class. The static class is fixed at compile time and never changes. In a statically typed language we say the class of the declaration is the static class. The static class is fixed at compile time and never changes. The dynamic class of a variable is the class associated with the value it currently holds. The dynamic class can change during the course of execution. The dynamic class of a variable is the class associated with the value it currently holds. The dynamic class can change during the course of execution.
8
Remember… TeamMember InnovationLabArchitect member architect before after TeamMember member = new TeamMember(); InnovationLabArchitect architect = new InnovationLabArchitect(); InnovationLabArchitect architect = new InnovationLabArchitect (); member = architect; Static class Dynamic class
9
Importance of Static Class In a statically typed object-oriented language, the legality of a message is determined at compile time, based on the static class….not the current dynamic receiver. class Animal { } class Dog extends Animal { void bark() { System.out.println("woof"); } // will generate error since mammals don't speak…although //the value Dog could respond to the message. Animal pet = new Dog; pet.bark();
10
Reverse Polymorphism Polymorphism says we can assign a value from a child class to an instance of the parent class, but can this assignment then be reversed? There are two specific problems associated with the question of reverse polymorphism. The problem of identity - can I tell if a value declared as an instance of a parent class actually holds a value from a subclass. (instanceof in Java) The task of assignment - can I then assign the value from the parent class to a variable declared as the subclass. (casting)
11
Static vs. Dynamic Method Binding Should the binding of a message be determined by the static class of a variable or the dynamic class of the value? Most OO languages use the static class to determine the legality of the message (compile time check). While the correct (method) implementation is determined by the dynamic class (run time).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.