You are on page 1of 8

Undergraduate Topics in Computer Science

Undergraduate Topics in Computer Science (UTiCS) delivers high-quality instructional content for
undergraduates studying in all areas of computing and information science. From core foundational
and theoretical material to nal-year topics and applications, UTiCS books take a fresh, concise, and
modern approach and are ideal for self-study or for a one- or two-semester course. The texts are
all authored by established experts in their elds, reviewed by an international advisory board, and
contain numerous examples and problems. Many include fully worked solutions.

Also in this series


Max Bramer
Principles of Data Mining
978-1-84628-765-7
Hanne Riis Nielson and Flemming Nielson
Semantics with Applications: An Appetizer
978-1-84628-691-9

Iain D. Craig

Object-Oriented
Programming
Languages:
Interpretation

Iain D. Craig, MA, PhD, FBCF, CITP


Series editor
Ian Mackie
cole Polytechnique and Kings College London, UK
Advisory board
Samson Abramsky, University of Oxford, UK
Chris Hankin, Imperial College London, UK
Dexter Kozen, Cornell University, USA
Andrew Pitts, University of Cambridge, UK
Hanne Riis Nielson, Technical University of Denmark, Denmark
Steven Skiena, Stony Brook University, USA
Iain Stewart, University of Durham, UK
David Zhang, The Hong Kong Polytechnic University, Hong Kong

British Library Cataloguing in Publication Data


A catalogue record for this book is available from the British Library
Library of Congress Control Number: 2007921522
Undergraduate Topics in Computer Science ISSN 1863-7310
ISBN-10: 1-84628-773-1
e-ISBN-10: 1-84628-774-X
ISBN-13: 978-1-84628-773-2
e-ISBN-13: 978-1-84628-774-9
Printed on acid-free paper
Springer-Verlag London Limited 2007
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted
under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or
transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in
the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright
Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers.
The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a
specic statement, that such names are exempt from the relevant laws and regulations and therefore free
for general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the information
contained in this book and cannot accept any legal responsibility or liability for any errors or omissions
that may be made.
9 8 7 6 5 4 3 2 1
Springer Science+Business Media
springer.com

Contents

1.

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Essential Properties of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Objects and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 Pure and Impure Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5 Mixed-Paradigm Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6 Organisation of this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1
1
3
6
7
9
9

2.

Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4 Slots and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.5 Slot Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6 Visibility and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7 Instance Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.8.2 Denition of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.9 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.10 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.11 Part Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13
13
16
20
22
23
25
31
34
34
35
40
44
49

3.

Prototype and Actor Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57


3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.2 Prototype Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

vi

Object-Oriented Programming Languages: Interpretation

3.3 The Concept of the Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


3.3.1 Slots and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.2 Message Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.3 Creating New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.4 Delegation and Shared Structure . . . . . . . . . . . . . . . . . . . . .
3.4 Methods in Prototype Languages . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5 Actor Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.2 Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.3 Extensions to the Actor Concept . . . . . . . . . . . . . . . . . . . . .

58
64
65
65
67
72
73
73
73
78

4.

Inheritance and Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83


4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.2 Interpretations of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.3 Inheritance as Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.4 Inheritance as Code Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.5 Single Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.6 Calling More Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.7 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.8 Multiple Inheritance Graph Shape . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.9 Approaches to Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.10 Tree Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.11 Graph Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
4.12 Linearised Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.13 Implemented Multiple Inheritance Techniques . . . . . . . . . . . . . . . . 112
4.13.1 The CLOS Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
4.13.2 Multiple Inheritance in C++ . . . . . . . . . . . . . . . . . . . . . . . . 114
4.13.3 Multiple Inheritance in Eiel . . . . . . . . . . . . . . . . . . . . . . . . 115
4.14 Mixin Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.15 Alternatives to Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 120
4.15.1 Perspectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
4.15.2 Interfaces in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4.15.3 Delegation and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . 122
4.16 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

5.

Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
5.2 Methods and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.3 Object Constructors and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5.4 Environments and Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.4.2 Environments: A More Formal Denition . . . . . . . . . . . . . . 136

Contents

5.4.3
5.4.4
5.4.5
5.4.6
5.5 Static

vii

Blocks in Smalltalk and SELF . . . . . . . . . . . . . . . . . . . . . . . 139


Block Structure in Beta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Higher-Order Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Methods and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
and Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

6.

Types I: Types and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.2 Inheritance and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.2.1 Telling What the Type Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.2.2 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
6.2.3 Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
6.3 Generic Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
6.4 Overloading and Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
6.5 Languages with Root Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
6.6 Polyadicity and Default Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 174
6.6.1 Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
6.7 Downcasting and Subtypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
6.8 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

7.

Types II: Types and ObjectsAlternatives . . . . . . . . . . . . . . . . . . 185


7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.2 Types and Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
7.3 Hiding Implementation Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
7.4 Classes and Type Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
7.5 Containers and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

8.

C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
8.2 Classes and Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
8.2.1 Class and Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . 203
8.2.2 Access Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
8.2.3 Data and Method Access Modiers . . . . . . . . . . . . . . . . . . . 204
8.2.4 Instance Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
8.2.5 Static Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
8.2.6 Finalization and Destruction . . . . . . . . . . . . . . . . . . . . . . . . . 207
8.2.7 Dot Notation and Member Access . . . . . . . . . . . . . . . . . . . . 208
8.2.8 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
8.2.9 Indexers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
8.2.10 Self Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
8.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
8.3.1 Calling Base-Class Constructors . . . . . . . . . . . . . . . . . . . . . . 211

viii

Object-Oriented Programming Languages: Interpretation

8.3.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213


8.4 Methods and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
8.4.1 Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
8.4.2 The Base Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
8.4.3 Parameter Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
8.4.4 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
8.4.5 Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
8.4.6 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
8.5 Polymorphism and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
8.5.1 Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
8.5.2 Type Unication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
8.6 Base Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
9.

BeCecil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
9.2 Programming Standard OO Mechanisms . . . . . . . . . . . . . . . . . . . . 232
9.3 Syntactic Sugar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
9.4 A Small Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
9.5 Concluding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

You might also like