Static and Dynamic Behavior CMPS 2143. Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Polymorphism, Virtual Methods and Abstract Classes.
Implications of Substitution Fall 2005 OOPD John Anthony.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Bindings. Typical stages of program execution First, the program is compiled Then everything is loaded into memory Then it is linked to any library routines.
Chapter -6 Polymorphism
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
Names, Scope, and Bindings Programming Languages and Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Memory Management in Java Mr. Gerb Computer Science 4.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Data Types In Text: Chapter 6.
CMSC 202 Polymorphism.
Object-oriented Programming in Java
Polymorphism, Virtual Methods and Abstract Classes
7. Inheritance and Polymorphism
ATS Application Programming: Java Programming
More inheritance, Abstract Classes and Interfaces
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Class Inheritance
CSC 533: Organization of Programming Languages Spring 2007
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Static and Dynamic Behavior CMPS 2143

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

This lecture Static versus dynamic typing Static versus dynamic classes in statically typed languages Static and dynamic binding of message and methods 3

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

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

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; }

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 (…);

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 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

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

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

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

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

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 !

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 !

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!

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

Study questions Pg 233: 1-3, 5,6, 8,9 18