Static and Dynamic Behavior - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Static and Dynamic Behavior

Description:

Static almost always means fixed or bound at compile time, and cannot thereafter ... The variable is just a name. ... Animal pet = new Dog; pet.bark(); Reverse ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 12
Provided by: JA
Category:
Tags: behavior | by | dynamic | meaning | name | pet | static

less

Transcript and Presenter's Notes

Title: Static and Dynamic Behavior


1
Static and Dynamic Behavior
  • Fall 2005 OOPD
  • John Anthony

2
Topics
  • Static vs. Dynamic Typing
  • Static Classes vs. Dynamic Classes
  • Static vs. dynamic method Binding

Lets 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 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).
  • 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.

TeamMember member new TeamMember()
polymorphic variable
InnovationLabArchitect architect new
InnovationLabArchitect ()
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.
  • 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
Dynamic class
Static 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
speakalthough //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).
Write a Comment
User Comments (0)
About PowerShow.com