CSCI-383 Object-Oriented Programming & Design Lecture 17.

Slides:



Advertisements
Similar presentations
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Advertisements

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.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
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)
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
ATS Application Programming: Java Programming
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Advanced Programming in Java
Lecture 6: Polymorphism
Presentation transcript:

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