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

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

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

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

Some instance messages and methods

Object-oriented Programming. Object-oriented Programming

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

Types. What is a type?

Conformance. Object-Oriented Programming Spring 2015

Java Object Oriented Design. CSC207 Fall 2014

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

Fundamentals of Programming Languages

Subtyping (Dynamic Polymorphism)

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

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

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

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

Structuring the Data. Structuring the Data

C++ Yanyan SHEN. slide 1

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

Programming Languages

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

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

G Programming Languages - Fall 2012

Object Oriented Paradigm

Programming Languages

Chapter 5 Object-Oriented Programming

Type Analysis. Type Checking vs. Type Inference

Derived and abstract data types. TDT4205 Lecture 15

The Java Programming Language

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

Types and Type Inference

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

Types, Type Inference and Unification

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

C++ Important Questions with Answers

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

Types and Type Inference

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

Inheritance and object compatibility

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

Introduction to Programming Using Java (98-388)

CSCI312 Principles of Programming Languages!

Data Abstraction. Hwansoo Han

CPS 506 Comparative Programming Languages. Programming Language

Subtyping for behaviour?

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

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

OBJECT ORIENTED PROGRAMMING USING C++

CS-202 Introduction to Object Oriented Programming

Subtyping for behaviour?

G Programming Languages - Fall 2012

VALLIAMMAI ENGINEERING COLLEGE

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

CSE 307: Principles of Programming Languages

What about Object-Oriented Languages?

301AA - Advanced Programming [AP-2017]

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

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

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

Lecture 12: Data Types (and Some Leftover ML)

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

Instantiation of Template class

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Short Notes of CS201

User-Defined Algebraic Data Types

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

CSC 533: Organization of Programming Languages. Spring 2005

22c:111 Programming Language Concepts. Fall Types I

CS201 - Introduction to Programming Glossary By

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

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

Properties of an identifier (and the object it represents) may be set at

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

Inheritance and Polymorphism

CSE 431S Type Checking. Washington University Spring 2013

PROGRAMMING IN C++ COURSE CONTENT

2. The object-oriented paradigm

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

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Names, Scopes, and Bindings II. Hwansoo Han

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

CS153: Compilers Lecture 11: Compiling Objects

Programming Languages: OO Paradigm, Polymorhism and Class Members

Practice for Chapter 11

What are the characteristics of Object Oriented programming language?

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

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

Inheritance, Polymorphism and the Object Memory Model

HANDLING NONLOCAL REFERENCES

CS 320 Introduction to Software Engineering Spring March 06, 2017

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

CSE 307: Principles of Programming Languages

Transcription:

Types and Classes I II From predefined (simple) and user-defined (composite) types via Abstract data types to Classes Type compatibility Subtyping <> subclassing Polymorphism revisited Subtype polymorphism Class compatibility Scandinavian Object Oriented programming Advanced oo concepts Multiple inheritance - alternatives Specialization of behaviour? Inner classes Covariance/contravariance Types of parameters to redefined methods Modularity Packages Interface-implementation Generics Object oriented or functional programming 11/1/2005 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, objects Evolution from simple types, via predefined composite types to userdefined types that reflect parts of the application domain. 11/1/2005 2

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

Special pre-defined type: String/Text/... Sequence of characters Properties Primitive type or just an array of characters? Static or dynamic length? Typical operations Assignment Comparison Concatenation Extraction of sub-string Pattern matching 11/1/2005 4

Different languages different string concepts Pascal packed arrays, Only assignment and comparison Ada, FORTRAN 77, FORTRAN 90 and BASIC Primitive type Assignment, comparison, concatenation, extraction of sub-string C and C++ not primitive type, character array Simula Text object, both by value and by reference Java String class (not array of char) 11/1/2005 5

Properties of composite types Cartesian product (record, struct) (m1,m2,...mn) in M1xM2x...xMn Assignment, comparison Composite values {3, 3.4 Hiding underlying representation? Arrays (mappings in terms of data) domain -> range digits: 0..9 -> char Possible domains, index bound checking, bound part of type definition, static/dynamic? typedef struct { int nedges; float edgesize; RegularPolygon; RegularPolygon rp = {3, 3.4 rp.nedges = 4; char digits[10] array [5..95] of integer array[weekday] of T, where type WeekDays = enum{monday, Tuesday,... 11/1/2005 6

Composite types Union Run-time type check union address { short int offset; long int absolute; 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; union bad_idea { int int_v; int* int_ref; typedef struct { address location; descriptor kind; safe_address; enum descriptor {abs, rel 11/1/2005 7

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; ; 11/1/2005 8

type t1 = array [0..9] of integer type t2 = array [0..9] of integer x,y: array [0..9] of integer z: array [0..9] of integer 11/1/2005 9

Type Completeness Principle No operation should be arbitrarily restricted in the types of its operands. Values of all types allowed as parameters? Values of all types allowed as function result? Records as result of functions? (Not all languages allow that) Functions as parameters, but can they be assigned? 11/1/2005 10

Subtyping (for non-object oriented languages) 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 11/1/2005 11

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 11/1/2005 12

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);... 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);... With abstract data types: operation (operands) => meaning of operation is always the same With classes object.operation (arguments) code depends on object and operation (dynamic lookup, method dispatch) 11/1/2005 13

From abstract data types to classes Traditional approach to encapsulation is through abstract data types Advantage Separate interface from implementation Disadvantage Not extensible in the way that OOP is Possible to do add(c1,c2) with classes? 11/1/2005 14

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 11/1/2005 15

Abstract data types argument of Mitchell abstype q with mk_queue: unit -> q is_empty: q -> bool insert: q * elem -> q remove: q -> elem is in program end abstype pq with mk_queue: unit -> pq is_empty: pq -> bool insert: pq * elem -> pq remove: pq -> elem is in program end But cannot intermix pq s and q s 11/1/2005 16

Abstract Data Types Guarantee invariants of data structure only functions of the data type have access to the internal representation of data Limited reuse Cannot apply queue code to pqueue, except by explicit parameterization, even though signatures identical Cannot form list of points, colored points Data abstraction is important part of OOP, innovation is that it occurs in an extensible form 11/1/2005 17

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) 11/1/2005 18

Simula I 11/1/2005 19

Simula II 11/1/2005 20

Subclassing in Simula 11/1/2005 21

11/1/2005 22

11/1/2005 23

Smalltalk I 11/1/2005 24

Smalltalk II 11/1/2005 25

Classification of polymorphism polymorphism overloading ad-hoc implisitt (conversion) Inclusion/ subtype universal parametric 11/1/2005 26

Inclusion/subtype polymorphism Point p1; ColorPoint c1; ; p1.equal(cp1); equal 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 11/1/2005 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 11/1/2005 28

Overriding vs Overloading - II class C {... bool equals(c pc) {... // equals 1 class SC extends C {... bool equals(c pc) {... // equals 1 bool equals(sc psc) {... // equals 2 C c SC sc C c = new C(); = new SC(); = 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 11/1/2005 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 novariance Some languages provide covariance on both: most intuitive Statically type-safe: Contravariance on parameter types Covariant on result type 11/1/2005 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 11/1/2005 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 class ColorPoint extends Point { Color c; bool equal(colorpoint cp) { return super.equal(cp) and c=cp.c 1. p2.equal(p1) 2. c2.equal(c1) 3. p1.equal(c1) 4. c1.equal(p1) OK OK OK NOK run-time run-time run-time run-time 11/1/2005 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) 11/1/2005 33

Example: Point and ColorPoint class Point { int x,y; virtual class ThisClass < Point; Towards a solution Virtual classes with constraints (OOPSLA 89) bool equal(thisclass p) { return x=p.x and y=p.y class ColorPoint extends Point { Color c; ThisClass:: ColorPoint; bool equal(thisclass p) { return super.equal(p) and c=p.c 11/1/2005 34

Subclassing Two approaches So-called Scandinavian/modeling approach Classes represent concepts from the domain Subclasses represent specialized concepts Overriding is specialization 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 Java: somewhere in between 11/1/2005 35

Example: Shapes Interface of every shape must include center, move, rotate, print Alt. 1 General interface only in Shape Different kinds of shapes are implemented differently Square: four points, representing corners Circle: center point and radius Alt. 2 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 Shape In Simula, C++, a method specified as virtual may be overridden. Circle Rectangle In Java, a method specified as final may not be overridden. 11/1/2005 36

Subclass compatibility Name compatibility: method with the same name in subclass overrides method in superclass (Smalltalk) Structure compatibility: number and types of method parameters must be compatible (C++, Java) Behavioral compatible: the effect of the subclass method must a specialization of the effect of the superclass method 11/1/2005 37

Types versus classes Type as the set of operations on an object Objects of two different classes may have the same type Structural typing Type name (e.g. interface) Type = class (or interface) 11/1/2005 38

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 11/1/2005 39

Two questions 1. How is the type of an entity (variable, funtion, ) specified? 2. When is the type determined? 2 Static (compile-time) Dynamic (run-time) 1 Explicit declaration Java, C++, Algol, Simula ML Simula, BETA Implicit declaration ML, Perl Smalltalk 11/1/2005 40

Example Structural (sub) typing Smalltalk in C-syntax class GraphicalObject { move(dx,dy int) { draw() { ;......; r.draw();.........; r.draw(); 11/1/2005 41

Example Two classes with the same structural type class GraphicalObject { move(dx,dy int) { draw() { ; class Cowboy { move(dx,dy int) { draw() { ;......; r.draw();.........; r.draw(); 11/1/2005 42