CSCI-383 Object-Oriented Programming & Design Lecture 17
Static and Dynamic Behavior When we left off last time, we were able to derive new types from existing types, and we could upcast from a derived type to its base type This was useful because it allowed us to share some code However, it really didn’t give us the polymorphism we want to extract from similar types
Static and Dynamic Behavior For example, suppose we have the following inheritance hierarchy class Shape {... void draw(void) const;... }; class Circle:public Shape{class Rectangle:public Shape{...void draw(void) const;...};
Static and Dynamic Behavior Suppose we declared an array of shapes like this Shape* shapeList[3]; shapeList[0] = new Circle(...); // Upcast to Shape* shapeList[1] = new Square(...); // Upcast to Shape* shapeList[2] = new Rectangle(...); // Upcast to Shape* We might want a function to draw all of our shapes in the shape list void draw(int numShapes, Shape* shapeList[]) { for(int i=0; i<numShapes; i++) shapeList[i]->draw(); }
Static and Dynamic Behavior Even though we created a Circle object, a Rectangle object, and a Square object, drawShapes only sees them as generic Shape objects There are different ways of solving this problem, and we’ll look at a couple of them
Type Fields One way to solve this polymorphism problem is to manually store the type within every Shape, Circle, or Rectangle object We could do this by defining an enumerated type, and marking each object in its constructor with its proper type However, this is very error prone and tends to lead to a lot of switch statements and casting throughout your code Write the drawShape function that uses type fields (DONE IN CLASS)
Static vs. Dynamic Type So far, we haven’t been able to break the bounds of the declaration type, or the static type, of an object The declared type of an object or an object pointer has been determining which method will be called The compiler pays no attention to the true type of the object – which is the type we provided when we allocated it with new. This is also called the object’s dynamic type
Chapter 11 Static and Dynamic Behavior
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd What do the terms Static and Dynamic Mean? Much of the power of object-oriented languages derives from the ability of objects to change their behavior dynamically at run time In Programming languages Static almost always means fixed or bound 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
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Static and Dynamic Typing In a statically typed programming language (e.g., C++, Java, Pascal), variables have declared types - - fixed at compile time In a dynamically typed programming language (e.g., Smalltalk, Python), a variable is just a name. Types are associated with values, not variables. A variable can hold different types during the course of execution
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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 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
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd The Polymorphic Variable The addition of object-oriented ideas in statically typed languages adds a new twist. Recall the argument for substitution: an instance of a child class should be allowed to be assigned to a variable of the parent class
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Static Class and Dynamic Class In a statically typed language we say the class of the declaration is the static class for the variable, while the class of the value it currently holds is the dynamic class Most statically typed OO languages constrain the dynamic class to be a child class of the static class
Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd 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 A message can produce a compile error, even if no run-time error could possibly arise
Binding Times The binding time of a function call is when the call is bound to a specific function implementation There generally are two binding times: static binding and dynamic binding Static binding (also known as early binding) is when the function choice is based on the characteristics of the variable (i.e., the type of the variable) Static binding generally takes place at compile time Function overloading and overriding in C++ are examples of static binding
Binding Times Dynamic binding (also known as late binding) is when the function choice is based on the dynamic characteristics of the variable (i.e., the type of the data in the variable) Dynamic binding is deferred until run time In the previous example, if one sends a draw message to an instance of Rectangle, Circle, or Square, one would want the appropriate implementation of draw to be executed However, if at compile time one knows only that the receiving object is “an instance of a Shape subclass, to be determined at run time”, static binding cannot be used. In this case, dynamic binding is used Dynamic binding is a powerful by dangerous tool To eliminate the danger of run time errors in C++, dynamic binding has been limited to virtual functions
Virtual Functions The way to retain the behavior of the instantiated type is through the use of virtual functions To declare a method to be a virtual function, you simply use the virtual keyword when declaring the method in the class definition When you call a function, it will check the dynamic type of the object before choosing which function to call – this process is reification
Virtual Functions For example, we could declare our Shape, Circle, and Rectangle classes as follows class Shape {... virtual void draw(void) const;... }; class Circle:public Shape{class Rectangle:public Shape{ virtual void draw(void) const; virtual void draw(void) const;......};
Virtual Functions Now we get the polymorphic behavior we want Shape* shapeList[maxShapes]; shapeList[0] = new Circle(...); // Upcast to Shape* shapeList[1] = new Square(...); // Upcast to Shape* shapeList[2] = new Rectangle(...); // Upcast to Shape* shapeList[0]->draw(); // Calls Circle::draw() shapeList[1]->draw(); // Calls Square::draw() shapeList[2]->draw(); // Calls Rectangle::draw()
Virtual Functions It is important that you declare the function to be virtual throughout your class hierarchy, or its behavior will be quite unexpected A virtual method can call a non-virtual method and vice-versa Overloaded operator functions can be virtual functions, but static methods can’t
Virtual Functions A constructor cannot be a virtual function because it needs to know the exact type to create However, a destructor can be declared as virtual, and generally should be virtual functions may not seem significant at first, but they enable a ton of code reuse when using class hierarchies