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

Similar documents
Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

INF3110 Programming Languages Object orientation and types, part I

INF3110 Programming Languages Types, Subtyping and Object Orientation

Subtyping for behaviour?

Object typing and subtypes

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

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

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

Conformance. Object-Oriented Programming Spring 2015

Java Object Oriented Design. CSC207 Fall 2014

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

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

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

Subtyping (Dynamic Polymorphism)

Some instance messages and methods

Object-oriented Programming. Object-oriented Programming

C++ Yanyan SHEN. slide 1

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

Chapter 5 Object-Oriented Programming

Fundamentals of Programming Languages

Types. What is a type?

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

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

The Java Programming Language

G Programming Languages - Fall 2012

C++ Important Questions with Answers

Type Analysis. Type Checking vs. Type Inference

Inheritance and object compatibility

Structuring the Data. Structuring the Data

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

Subtyping for behaviour?

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

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

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Object Oriented Paradigm

Data Abstraction. Hwansoo Han

Programming Languages

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

Subtyping for behaviour?

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

301AA - Advanced Programming [AP-2017]

Introduction to Programming Using Java (98-388)

CS-202 Introduction to Object Oriented Programming

Types, Type Inference and Unification

Derived and abstract data types. TDT4205 Lecture 15

Programming Languages

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

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

User-Defined Algebraic Data Types

What about Object-Oriented Languages?

Types and Type Inference

CPS 506 Comparative Programming Languages. Programming Language

Types and Type Inference

G Programming Languages - Fall 2012

CSE 431S Type Checking. Washington University Spring 2013

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CSE 307: Principles of Programming Languages

OBJECT ORIENTED PROGRAMMING USING C++

Practice for Chapter 11

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

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

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

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

CS558 Programming Languages Winter 2013 Lecture 8

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CSCI312 Principles of Programming Languages!

CMSC 330: Organization of Programming Languages

Type Hierarchy. Lecture 6: OOP, autumn 2003

C++ (Non for C Programmer) (BT307) 40 Hours

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

Lecture 12: Data Types (and Some Leftover ML)

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CS 320 Introduction to Software Engineering Spring March 06, 2017

Subtyping. Lecture 13 CS 565 3/27/06

Inheritance, Polymorphism and the Object Memory Model

Instantiation of Template class

Type Checking and Type Inference

VALLIAMMAI ENGINEERING COLLEGE

2. The object-oriented paradigm

Programming Languages: OO Paradigm, Polymorhism and Class Members

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

C++ (classes) Hwansoo Han

Principles of Programming Languages

Tokens, Expressions and Control Structures

Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

What are the characteristics of Object Oriented programming language?

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

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

CSC324 Principles of Programming Languages

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CS 520 Theory and Practice of Software Engineering Fall 2017

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Transcription:

Types and Classes I II From predefined (simple) and user-defined (composite) types via Abstract data types Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to Inner classes Classes Modularity Type compatibility Packages Subtyping <> subclassing Interface-implementation Class compatibility Covariance/contravariance Types of parameters of redefined methods Generics Scandinavian approch to object oriented programming 10/22/10 1

Classification of types Predefined, simple types (not built from other types) boolean, integer, real,... pointers, pointers to procedures string User-defined types enumerations Predefined composite types arrays User-defined, composite types Records/structs, unions, abstract data types, classes Evolution from simple types, via predefined composite types to userdefined types that reflect parts of the application domain. 10/22/10 2

Properties of primitive types Classifying data of the program Well-defined operations on values Protecting data from un-intended operations Hiding underlying representation 10/22/10 3

Properties of composite types Records, structs (Cartesian products) (m 1,m 2,...m n ) in M 1 xm 2 x...xm n Assignment, comparison Composite values {3, 3.4 Hiding underlying representation? typedef struct { int nedges; float edgesize; RegularPolygon; RegularPolygon rp = {3, 3.4 rp.nedges = 4; Arrays (mappings) domain range Possible domains, index bound checking, bound part of type definition, static/dynamic? char digits[10] array [5..95] of integer array[weekday] of T, where type WeekDays = enum{monday, Tuesday,... 10/22/10 4

Composite types Union Run-time type check union address { short int offset; long int absolute; union bad_idea { int int_v; int* int_ref; Discriminated union Run-time type check address_type = (absolute, offset); safe_address = record case kind:address_type of absolute: (abs_addr: lnteger); offset: (off:addr: short integer) end; typedef struct { address location; descriptor kind; safe_address; enum descriptor {abs, rel 10/22/10 5

Type compatibility (equivalence) Name compatible Values of types with the same name are compatible Structural compatible Types T1 and T2 are structural compatible If T1 is name compatible with T2, or T1 and T2 are defined by applying the same type constructor to structurally compatible corresponding type components struct Position { int x,y,z; ; struct Position pos; struct Date { int m,d,y; ; struct Date today; void show(struct Date d);...; show(today);......; show(pos);... struct Complex { real x,y; ; struct Point {real x,y; ; 10/22/10 6

Abstract datatypes abstype Complex = C of real * real with fun complex(x,y: real) = C(x,y) fun add(c(x1,y1,c(x2,y2)) = C(x1+x2,y1+y2) end...; add(c1,c2);... Signature Constructor Operations 10/22/10 7

Abstract datatypes versus classes abstype Complex = C of real * real with fun complex(x,y:real) = C(x,y) fun add(c(x1,y1,c(x2,y2)) = C(x1+x2,y1+y2) end...; add(c1,c2);... With abstract data types: operation (operands) class Complex { real x,y; Complex(real v1,v2) {x=v1; y=v2 add(complex c) {x=x+c.x; y=y+c.y...; c1.add(c2);... Possible to do add(c1,c2) with classes? meaning of operation is always the same With classes object.operation (arguments) code depends on object and operation (dynamic lookup, method dispatch) 10/22/10 8

From abstract data types to classes Encapsulation through abstract data types Advantage Separate interface from implementation Guarantee invariants of data structure only functions of the data type have access to the internal representation of data Disadvantage Not extensible in the way that OO is 10/22/10 9

Subtyping (in general) Subtype as a subset of the set of values e.g. subrange Compatibility rules between subtype and supertype Substitutability principle: a value of a subtype can appear wherever a value of the supertype is expected 10/22/10 10

Abstract data types argument of Mitchell abstype queue with is in end mk_queue: unit -> queue is_empty: queue -> bool insert: queue * elem -> queue remove: queue -> elem program abstype pqueue with is in end mk_queue: unit -> pqueue is_empty: pqueue -> bool insert: pqueue * elem -> pqueue remove: pqueue -> elem program Cannot apply queue code to pqueue, even though signatures are identical 10/22/10 11

Point and ColorPoint class Point { int x,y; move(int dx,dy) { x=x+dx; y=y+dy class ColorPoint extends Point { Color c; changecolor(color nc) {c= nc Point x y move ColorPoint x y c move changecolor ColorPoint interface contains Point ColorPoint is a subtype of Point Could not form list of points and colored points if done by abstract data types 10/22/10 12

Object Interfaces - Subtyping Interface The operations provided by objects of a certain class Example: Point x : returns x-coordinate of a point y : returns y-coordinate of a point move : method for changing location The interface of an object is its type. If interface B contains all of interface A, then B objects can also be used as A objects (substitutability) Subclassing <> subtyping 10/22/10 13

Subclassing Two approaches So-called Scandinavian/Modeling Approach Classes represent concepts from the domain Subclasses represent specialized concepts Overriding is specialization/extension Subclass is subtype Reluctant to multiple inheritance (unless it can be understood as multiple specialization) So-called American/Programming Approach Classes represent implementations of types Subclasses inherit code Overriding is overriding Subclassing not necessarily the same as subyping Multiple inheritance as longs as it works 10/22/10 14

Simula I 10/22/10 15

Simula II 10/22/10 16

Subclassing in Simula 10/22/10 17

10/22/10 18

10/22/10 19

Smalltalk I 10/22/10 20

10/22/10 21

Example: Shapes Interface of every shape must include center, move, rotate, print Programming Approach Modeling Approach General interface only in Shape Different kinds of shapes are implemented differently Square: four points, representing corners Circle: center point and radius General interface and general implementation in shape Shape has center point Move moves by changing the position of the center point To be or not be virtual e.g. Move should not be redefined in subclasses Circle Shape Rectangle In Simula, C++, a method specified as virtual may be overridden. In Java, a method specified as final may not be overridden. 10/22/10 22

Subclass compatibility Name compatible: method with the same name in subclass overrides method in superclass (Smalltalk) Structure compatible: number and types of method parameters must be compatible (C++, Java) Behavior compatible: the effect of the subclass method must a specialization of the effect of the superclass method Types versus classes Type as the set of operations on an object Objects of two different classes may have the same type Structural type compatibility Type = class (or interface) Type name (e.g. class or interface name) 10/22/10 23

Example Structural (sub) typing Smalltalk in C-syntax class GraphicalObject { move(dx,dy int) { draw() { ;......; r.draw();...; r.draw(); 10/22/10 24

Example Two classes with the same structural type class GraphicalObject { move(dx,dy int) { draw() { ; class Cowboy { move(dx,dy int) { draw() { ;... 10/22/10...; r.draw();... ; r.draw(); 25

Classification of polymorphism polymorphism ad-hoc universal overloading implisitt (conversion) Inclusion/ subtype parametric 10/22/10 26

Inclusion/subtype polymorphism Point p1; ColorPoint cp1; ; p1.equals(cp1); equals works for cp1 because ColorPoint is a subtype of type Point class Shape { void draw() { ; class Circle extends Shape { void draw() { ;...; ashape.draw(); will draw a Circle if ashape is a Circle 10/22/10 27

Overriding vs Overloading I class Shape {... bool contains(point pt) {...... ; class Rectangle extends Shape {... bool contains(int y,y) {...... Overloading within the same scope {..., crossing superclass boundaries 10/22/10 28

Overriding vs Overloading - II class C {... bool equals(c pc) {... // C_equals 1 class SC extends C {... bool equals(c pc) {... // SC_equals 1 bool equals(sc psc) {... // equals 2 C c = new C(); SC sc = new SC(); C c = new SC(); c.equals(c) //1 c.equals(c ) //2 c.equals(sc) //3 c.equals(c) //4 c.equals(c ) //5 c.equals(sc) //6 sc.equals(c) //7 sc.equals(c ) //8 sc.equals(sc) //9 10/22/10 29

Covariance/contravariance/novariance class C { T1 v; T2 m(t3 p) {... class SC extends C { T1 v; T2 m(t3 p){... Covariance: T1 subtype of T1 T2 subtype of T2 T3 subtype of T3 Contravariance: The opposite Novariance: same types Most languages have no-variance Some languages provide covariance on both: most intuitive Statically type-safe: Contravariance on parameter types Covariant on result type 10/22/10 30

Example: Point and ColorPoint I: no variance class Point { int x,y; move(int dx,dy) { x=x+dx; y=y+dy Point p1, p2; ColorPoint c1,c2; bool equal(point p) { return x=p.x and y=p.y class ColorPoint extends Point { Color c; bool equal(point p) { return x=p.x and y=p.y and c=p.c 1. p2.equal(p1) 2. c2.equal(c1) 3. p1.equal(c1) 4. c1.equal(p1) return super.equal(p) and c=p.c 10/22/10 31

Example: Point and ColorPoint II: covariance class Point { int x,y; move(int dx,dy) { x=x+dx; y=y+dy Point p1, p2; ColorPoint c1,c2; bool equal(point p) { return x=p.x and y=p.y 1. p2.equal(p1) 2. c2.equal(c1) 3. p1.equal(c1) OK OK OK run-time run-time run-time class ColorPoint extends Point { Color c; bool equal(colorpoint cp) { return super.equal(cp) and c=cp.c 4. c1.equal(p1) NOK run-time 10/22/10 32

Example: Point and ColorPoint III: casting class Point { int x,y; move(int dx,dy) { x=x+dx; y=y+dy Point p1, p2; ColorPoint c1,c2; bool equal(point p) { return x=p.x and y=p.y class ColorPoint extends Point { Color c; bool equal(point p) { return super.equal(p) and c=(colorpoint)p.c 1. p2.equal(p1) 2. c2.equal(c1) 3. p1.equal(c1) 4. c1.equal(p1) 10/22/10 33

Example: Point and ColorPoint class Point { int x,y; virtual class ThisClass < Point; bool equal(thisclass p) { return x=p.x and y=p.y Alternative to casting: Virtual classes with constraints (OOPSLA 89) Still run time type checking class ColorPoint extends Point { Color c; ThisClass:: ColorPoint; bool equal(thisclass p) { return super.equal(p) and c=p.c 10/22/10 34

Compile-time vs run-time type checking f(x) if f : A B then x : A, but when to check? Compile-time (static) Must ensure that x will never have another type then A Run-time (dynamic) type checking When calling f(x) determine the type of x and check if it is A Basic tradeoff Both prevent type errors Run-time checking slows down execution Compile-time checking restricts program flexibility Combined compile - and run-time checking 10/22/10 35

Two questions 1. How is the type of an entity (variable, function, ) specified? 2. When is the type determined? 2 1 Explicit declaration Implicit declaration Static (compile-time) Java, C++, Algol, Simula ML, BETA ML, Perl Dynamic (run-time) Simula, BETA, Java (casting) Python, Ruby, Prolog Smalltalk Python, Ruby 10/22/10 36