Does anyone actually do this?

Similar documents
What are the characteristics of Object Oriented programming language?

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function

What is Inheritance?

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Supporting Class / C++ Lecture Notes

Week 6: Data. Let's focus first on application domain data.

Introduction to Inheritance

Implementing Interfaces. Marwan Burelle. July 20, 2012

Session 3: Data. Session 3a: Data

C++ Important Questions with Answers

COP 3330 Final Exam Review

C++ Programming: Polymorphism

C++ Inheritance and Encapsulation

CS-202 Introduction to Object Oriented Programming

Chapter 5 Object-Oriented Programming

Week 11: Case study: Designing, building, & testing a Person class Background for the Project Needed in many applications Is it possible? practical?

Inheritance -- Introduction

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Session 4b: Review of Program Quality

Data Abstraction. Hwansoo Han

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lecture Notes on Programming Languages

G Programming Languages - Fall 2012

Casting in C++ (intermediate level)

Java Object Oriented Design. CSC207 Fall 2014

Today s lecture. CS 314 fall 01 C++ 1, page 1

Session 3b: Defining data items

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Chapter 1 Getting Started

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Polymorphism. Zimmer CSCI 330

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Early programming languages ca. 1960

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

PROGRAMMING IN C++ COURSE CONTENT

Data Structures (list, dictionary, tuples, sets, strings)

Polymorphism. Arizona State University 1

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inclusion Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Short Notes of CS201

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CS201 - Introduction to Programming Glossary By

9 Working with the Java Class Library

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

Super-Classes and sub-classes

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Strict Inheritance. Object-Oriented Programming Spring 2008

Simple Factory Pattern

Ways of documenting Session 5: detailed requirements The Data Dictionary one any The Project A data dictionary Data Dictionary may be maintained

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Introduction to Object-Oriented Programming

Comp 249 Programming Methodology Chapter 8 - Polymorphism

1. Write two major differences between Object-oriented programming and procedural programming?

EL2310 Scientific Programming

Programming II (CS300)

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Chapter 10 :: Data Abstraction and Object Orientation

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

CS260 Intro to Java & Android 03.Java Language Basics

Unit 1 : Principles of object oriented programming

Ways of documenting Session 5: detailed requirements The Data Dictionary one any The Project A data dictionary Data Dictionary may be maintained

1. Software Systems Complexity, OO Paradigm, UML

Object Oriented Programming. Solved MCQs - Part 2

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

STRUCTURING OF PROGRAM

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Week 5: Background. A few observations on learning new programming languages. What's wrong with this (actual) protest from 1966?

Evolution of Programming Languages

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Inheritance and Polymorphism

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

Strict Inheritance. Object-Oriented Programming Winter

Classes, Objects, and OOP in Java. June 16, 2017

Polymorphism Part 1 1

Chapter 9 :: Data Abstraction and Object Orientation

The. dark side of JAVA. Java bien, j'espère? For what?

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

5.6.1 The Special Variable this

VIRTUAL FUNCTIONS Chapter 10

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CPS 506 Comparative Programming Languages. Programming Language

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

ob-ject: to feel distaste for something Webster's Dictionary

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Inheritance and object compatibility

Answer1. Features of Java

Transcription:

Session 11: Polymorphism Coding type-dependent logic Virtual functions Pure virtual functions and abstract classes Coding type-dependent logic Suppose we need to do something different depending on what type of object we're processing And suppose those types are all subtypes (derived classes) of a common parent (base) class Here are 4 techniques programmers use: Motor Vehicle 1 IF-THEN-ELSE logic 2 CASE logic Engine Body Electrical System Transmission 3 Table-driven logic COMP 170 Fall, 2013 Mr Weisert Chassis Doors Battery Alternator 4 Polymorphism Comparing the techniques 1 IF-THEN-ELSE logic 2 CASE logic 3 Table-driven logic 4 Polymorphism Let's examine how we'd use each of them in a system to administer a public library Assume that we have to write a separate check_in (or return) routine for each type of item the library circulates 1 IF-THEN-ELSE logic (COBOL code) IF ITEM-TYPE = BOOK THEN PERFORM CHECK-IN-BOOK; ELSE IF ITEM-TYPE = MAGAZINE THEN PERFORM CHECK-IN-MAGAZINE; ELSE IF ITEM-TYPE = AUDIO_RECORDING THEN PERFORM CHECK-IN-RECORDING; ELSE IF ITEM-TYPE = FILM THEN PERFORM CHECK-IN-FILM; ELSE CALL ERROR USING "ILLEGAL ITEM TYPE"; Horribly repetitive and error prone Does anyone actually do this? COMP 170 1-4 Fall, 2013

2 Case logic (C code) switch (itemtype) {case book: check_in_book (item); break; case magazine: check_in_magazine (item); break; case recording:check_in_recording(item); break; case film: check_in_film (item); break; default: cerr << "Illegal item type " << itemtype; } A slight improvement, but still requires custom code for each type, not only here but probably also in dozens of other places in the program 3 Table-driven logic (C code) // Initialization void (check_in [9]) (LibraryItem); check_in[book] = check_in_book; check_in[magazine] = check_in_magazine; check_in[recording`] = check_in_recording; array of pointers to functions check_in[film] = check_in_film; // Invoke the appropriate function check_in[itemtype] (item); Big improvement in executable code, but we still have to set up the array of function pointers Explicit type code The 3 techniques examined so far all depend on reifying the type The type code is an actual data item in the LibraryItem record (object) enum LibraryType {book, recording, magazine, film, video}; struct LibraryItem { LibraryType type_code; char* title; Date datepurchased; }; Hiding the type code (C++) With polymorphism, on the other hand, we don't need a type_code data item The run-time environment will keep track of it for us The detailed syntax is somewhat different in C++, Java, and C#, but the principle is exactly the same a The Microsoft Programming Guide article in today's reading list provides C# details b The handout demonstration shows a similar example in Java We shall examine it in detail COMP 170 5-8 Fall, 2013

C++ special operator for invoking polymorphic functions The arrow operator itemptr->check_in(); is just shorthand for (*itemptr)check_in(); Why do we need it? 4 Polymorphic invocation itemptr->check_in(); We still have to (a) Define Book, etc as derived classes from LibraryItem (b) write a separate check_in routine for each type (at least for any type where we want a non-default action), (c) declare check_in in LibraryItem to be a virtual function but now the code to invoke such routines doesn't have to be aware of the type hierarchy What's happening here? What benefits can we expect? Virtual functions Similar to overloading: If a derived class provides no custom version, the base class's function is called The differences are: 1 that the selection is made not by the compiler, but during execution when the the function is invoked 2 that a base-class's member function can invoke a derived-class's member function, even if the base class doesn't know about the derived class What do we need to change when we add another derived class to the hierarchy? Hiding the type code in Java Java supports the same capability, but does so by default Every function is virtual (potentially polymorphic) unless it's declared final Example: itemcheckin() will invoke the version of checkin() defined in whichever subclass of LibraryItem the object reference item belongs to We don't need a pointer or explicit reference to invoke it because all Java objects are stored as references COMP 170 9-12 Fall, 2013

Run-time method selection In c++ requires a pointer (or a reference): Book item1; LibraryItem* item_ptr; item_ptr = &item1; item_ptr -> check_in(); Invokes the Book version of check_in() What about this? (*item_ptr)check_in(); How would we do this in Java? Reference assignment A reference data item declared as an object of the base class may take on a value of a reference to an object of a derived class But the opposite is prohibited (unless you explicitly cast the reference): Why is this a reasonable rule? LibraryItem item1; Book item2; (code to initialize items) item1 = item2; // OK item2 = item1; // Illegal How does it know? Obviously, some behind-the-scenes control information is present in memory for both virtual member functions, and pointers to objects in the class hierarchy Specifically: for each virtual function C++ or Java may either: construct a table of pointers to the different class-dependent versions, or construct for a class a table of pointers to its virtual functions (the vtbl) an efficient integer type code accompanies the pointer Thus, there's some overhead cost, but not much Arrays of objects It's common to declare an array of references to the base class: LibraryItem catalog = new LibraryItem[size]; and then set elements to references to objects of vaqrious derived classes: catalog[k] = new Book( ); And finally process those elements in a high-level loop that doesn't know the individual types Let's look at the handout example COMP 170 13-16 Fall, 2013

Polymorphism and I-O The absence of the run-time control information in files or databases is the main obstacle to handling persistent objects in both C++, C#, and Java You can write objects to a file, and you can read them back, but you can't then invoke virtual methods on them with run-time selection Why not? For some, this is a serious obstacle to adopting those languages for a major application Can we exploit OOP and databases together? Partial interim solutions include: Object-oriented data-base products (eg Object Store, Poet, Objectivity) Language independent standards (eg CORBA) Re-reifying the type code, and putting class selection logic into your input functions Popular quasi-object-oriented products don't solve (or even address) this problem Object-oriented paradigm: We've now seen all of it Data objects as instances of classes Encapsulation functions (methods, processes, behavior, services) are bound to the class component data items (internal representation) are hidden from client programs Inheritance of properties by specific classes from more general classes Run-time selection among versions of a function depending on the class to which an object belongs (polymorphism) COMP 170 17-20 Fall, 2013