Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Similar documents
Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

An Introduction to Object Orientation

Subtyping. Lecture 13 CS 565 3/27/06

CS 617 Object Oriented Systems Lecture 3 Object Abstractions, Encapsulation, Abstract Data Types 3:30-5:00pm Thu, Jan 10

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

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Type Analysis. Type Checking vs. Type Inference

CS 617 Object Oriented Systems Lecture 5 Classes, Classless World:Prototypes, Instance Variables, Class Variables, This/Self 3:30-5:00 pm Thu, Jan 17

C++ Important Questions with Answers

CS-202 Introduction to Object Oriented Programming

Inheritance and object compatibility

C++ Yanyan SHEN. slide 1

Fundamentals of Programming Languages

Subtyping (Dynamic Polymorphism)

Lecture 13: Subtyping

Practice for Chapter 11

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

Part III. Chapter 15: Subtyping

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Type Hierarchy. Lecture 6: OOP, autumn 2003

Types, Type Inference and Unification

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Subtyping (cont) Lecture 15 CS 565 4/3/08

Object Oriented Programming. Java-Lecture 11 Polymorphism

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Method Resolution Approaches. Dynamic Dispatch

301AA - Advanced Programming [AP-2017]

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Object typing and subtypes

Part III Chapter 15: Subtyping

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

22. Subtyping, Inheritance and Polymorphism

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Programming in C++: Assignment Week 6

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Types and Type Inference

Object Oriented Programming: Based on slides from Skrien Chapter 2

OBJECT ORIENTED PROGRAMMING USING C++

Subtyping and Objects

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Types and Type Inference

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

COMP322 - Introduction to C++

Generic programming in OO Languages

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Week 8: Operator overloading

An introduction to C++ template programming

Object-oriented Programming. Object-oriented Programming

Topic 7: Algebraic Data Types

CS Programming I: Inheritance

G Programming Languages - Fall 2012

Programming Languages: OO Paradigm, Polymorhism and Class Members

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Conformance. Object-Oriented Programming Spring 2015

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

What is an algorithm?

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Chapter 11 Object and Object- Relational Databases

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

Programming Languages 2nd edition Tucker and Noonan"

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

Software Engineering

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

Inheritance. Transitivity

Operator overloading

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

CSE 307: Principles of Programming Languages

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Imperative Programming 2: Inheritance 1

Object Oriented Issues in VDM++

Before we dive in. Preprocessing Compilation Linkage

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Object-Oriented Principles and Practice / C++

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

CS 320 Introduction to Software Engineering Spring March 06, 2017

SPAREPARTSCATALOG: CONNECTORS SPARE CONNECTORS KTM ART.-NR.: 3CM EN

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

Some instance messages and methods

Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

CSCI-GA Final Exam

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

Lecture #23: Conversion and Type Inference

Object Oriented Design

Java Object Oriented Design. CSC207 Fall 2014

Data Abstraction. Hwansoo Han

Strict Inheritance. Object-Oriented Programming Spring 2008

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Object Oriented Programming in C++

Chapter 1: Object-Oriented Programming Using C++

Transcription:

CS 617 Object Oriented Systems Lecture 9 Polymorphism: Mere Syntactic Vs. Dynamic Binding,, Subsumption Covariance, Contravariance 3:30-5:00 pm Thu, Jan 31 Rushikesh K Joshi Department of Computer Science and Engineering Indian Institute of Technology Bombay

Outline 1 Dynamic Binding and Polymorphism 2 3 4

Outline 1 Dynamic Binding and Polymorphism 2 3 4

Dynamic Binding and Polymorphism I class A { public: virtual void f () { cout «"A.f "; }; virtual void g () { cout «"A.g "; }; virtual void h () { cout «"A.h "; }; virtual void k () { cout «"A.k "; }; }; class B : public A { public: virtual void g () { cout «"B.g "; }; virtual void h () { cout «"B.h "; }; }; class C : public B { public: virtual void h () { cout «"C.h "; }; virtual void k () { cout «"C.k "; }; };

Dynamic Binding and Polymorphism II main () { C *cp = new C; B* bp = cp; A* a1 = cp; A* a2 = bp; A* a3 = new B; cp->f(); cp->g(); cp->h(); cp->k(); bp->f(); bp->g(); bp->h(); bp->k(); a1->f(); a1->g(); a1->h(); a1->k(); a2->f(); a2->g(); a2->h(); a2->k(); a3->f(); a3->g(); a3->h(); a3->k(); }

Outline 1 Dynamic Binding and Polymorphism 2 3 4

Member Function Overloading I class Complex { int i ; // real component int j ; // imaginary component public: Complex (int x, int y) { i=x; j=y; } Complex add (Complex a) { i += a.i ; j += a.j; Complex tmp (i,j); return (tmp); } Complex add (int i) { Complex tmp (i,0); add (tmp); } void printstate () {... print c1 and c2... }; };

Member Function Overloading II int main () { Complex c1 (2,3), c2(4,6); c1.printstate(); c2.printstate(); c1.add (c2); c1.add (100); c1.printstate(); c2.printstate(); }

Operator Overloading I class Complex { private: int i ; // real component int j ; // imaginary component public: Complex (int x, int y) { i=x; j=y; } Complex operator + (Complex a) { Complex tmp (i+a.i,j+a.j); return (tmp); } Complex operator + (int x) { Complex tmp (i+x,j); return (tmp); } void printstate () {... } };

Operator Overloading II int main () { Complex c1 (2,3), c2(4,6);..print c1 and c2 before the addition.. c1 = c1+c2; c1 = c1+100; c1+c2; c1+100;..print c1 and c2 after c1=c1+c2; c1=c1+100; c1+c2; c1+100;.. }

Templates I template <class T> class Node { public: T element; Node<T> *next; Node<T> *previous; Node (T e) { element = e; next=previous=null;} };

Templates II template <class T> class List { protected: int len; // cardinality Node<T> *head; Node<T> *tail; public: List (); List <T>& in (T element); // attach given elem at beginning

Templates III T out (); // take away front elem and return it. // receiver list is the pruned one List <T>& push (T element); // attach given elem at end T pop (); // take away last node and return it. //receiver list is the pruned one List <T>& operator << (T element); // same as in ; receiver list returned

Templates IV List <T>& operator + (T element); // same as push ; receiver list returned T operator - () ; // same as out; element returned: unary prefix T operator ~ (); // same as pop; element returned: unary prefix void read_visit (ListVisitor<T> *visitor); // visitor object gets to reads all elements

Templates V }; void rw_visit (ListVisitor<T> *visitor); //visitor object gets to read/write // transformed elements are to be returned int length () {return len;} List <T> & operator = (List <T> inputlist); // copy constructor void nullify (); // nullifies the given list by terminating it

Templates VI int main (void) { } List <char> l,m,n; List <Account> la; //...

Syntactic Polymorphism Polymorphism Merely syntactic Compiler can remove polymorphism during compile time through a type analysis For example: all calls to overloaded functions are resolved Same type list is used to hold elements of different types, but the compiler generates two different implementations for two different types No dynamic binding in syntactic polymorphism

Polymorphism at Runtime Can we use a value of one type where a value of another type is expected?

Outline 1 Dynamic Binding and Polymorphism 2 3 4

Relating Two Different Types Principle of Safe Substitution: A value of one type can safely used where a value of another type is expected When can you say a value of type T 1 can be used where a value of type T 2 is expected? A a; B b; C c; R f(b val) {.. use val here..}.... f(a); when is this permitted? f(c); when is this permitted?

Consider Some Types which are Finite Sets of Integers we know something about type int: int = {-MAXINT.. 0... +MAXINT } Now Let s define types A, B, C as below Type A = {1,2,3,4,5} Type B = {1,2,3} Type C = {1,2} What can we say about type safety of the above program?

What about acceptability of returned parameter? A a; B b; C c; R f(b val) {.. use val here..}.... a = f(x); when is this permitted? b = f(x); when is this permitted? c = f(x); when is this permitted?

The Subtype Relation S <: T (Meaning: S is a subtype of T) It s safe to use a value of a subtype where a value of a supertype is expected. i.e. s:s,s<:t s:t (called The Rule of Subsumption: The latter subsumes (includes) the former) Formulate Rules for for simple types, structures, functions, and now Object Types

Outline 1 Dynamic Binding and Polymorphism 2 3 4

Set Types Subsets are Subtypes

Record Types One Rule (depth rule): for each i 1..n S i <:T i, s:s 1..n s:t 1..n S 1..n, T 1..n are two records Formulate a rule based on width of records?

Function Types f : T 1 T 2 g : S 1 S 2 When is g <: f?

Now to induced by Class Inheritance class A { public T2 f(t1); public T4 g(t3); } class B inherits A { public S2 f(s1); public S4 g(s3); } main () { A a = new B X x = new X Y y y = a.f(x) when will this statement work safely? }

Covariance and Contravariance Which one is type-safe? At what point of time do you guarantee type safety?

and Subsumption put to Use Code written in terms of supertype works on all its subtypes Code written in terms of an interface will work on all classes implementing the interface code written in terms of a superclass will work on all its subclasses Provided that subtyping is established between the base and the derived entities