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

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

References: Chapters 10 and 11 Chapters 8, and 12( 2 and 3)

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Object typing and subtypes

Subtyping (Dynamic Polymorphism)

Some instance messages and methods

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

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

Lecture 16: Object Programming Languages

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

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

The Java Programming Language

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

Data Abstraction. Hwansoo Han

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

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Object Oriented Paradigm

Class 22: Inheritance

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

CS558 Programming Languages Winter 2013 Lecture 8

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

Object-oriented Programming. Object-oriented Programming

G Programming Languages - Fall 2012

CPS 506 Comparative Programming Languages. Programming Language

Lecture Notes on Programming Languages

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

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

C++ Yanyan SHEN. slide 1

Objects, Encapsulation, Inheritance (2)

What about Object-Oriented Languages?

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

Chapter 10 Object-Oriented Programming

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

Object Oriented Paradigm Languages

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

Lecture 15: Object-Oriented Programming

CS558 Programming Languages

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Chapter 6 Introduction to Defining Classes

Chapter 5 Object-Oriented Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CMSC 132: Object-Oriented Programming II

Informatica 3 Syntax and Semantics

G Programming Languages - Fall 2012

2. The object-oriented paradigm!

Chapter 9 :: Data Abstraction and Object Orientation

Chapter 10 :: Data Abstraction and Object Orientation

Chapter 13. Object Oriented Programming

CSE 341, Autumn 2015, Ruby Introduction Summary

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

Advanced Database Applications. Object Oriented Database Management Chapter 13 10/29/2016. Object DBMSs

CS 403/503 Exam 4 Spring 2015 Solution

Java Object Oriented Design. CSC207 Fall 2014

What are the characteristics of Object Oriented programming language?

CS-202 Introduction to Object Oriented Programming

C++ Important Questions with Answers

Lecturer: William W.Y. Hsu. Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Oriented Programming: Based on slides from Skrien Chapter 2

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

LATENT METHODS. Richard CS

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

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

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

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

Instantiation of Template class

Get Unique study materials from

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Object Oriented Programming. Solved MCQs - Part 2

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

C++ (classes) Hwansoo Han

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Inheritance and Polymorphism

Smalltalk. Topics. History of Smalltalk. OOP and GUI. Steve Jobs, PARC, Dec Smalltalk 1. The best way to predict the future is to invent it.

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

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

Conformance. Object-Oriented Programming Spring 2015

Chapter 11 Object and Object- Relational Databases

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

Admin. CS 112 Introduction to Programming. Exercise: Gene Finding. Language Organizing Structure. Gene Finding. Foundational Programming Concepts

CSE 307: Principles of Programming Languages

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

CS 314 Principles of Programming Languages

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Object Oriented Programming in C++ Basics of OOP

STRUCTURING OF PROGRAM

ITI Introduction to Computing II

Types and Type Inference

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

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

ITI Introduction to Computing II

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Konzepte von Programmiersprachen

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Transcription:

CS 242 Simula 67 Simula and Smalltalk John Mitchell First object oriented language Designed for simulation Later recognized as general purpose prog language Extension of Algol 60 Standardized as Simula (no 67 ) in 1977 Inspiration to many later designers Smalltalk C++ Brief history Norwegian Computing Center Designers: Dahl, Myhrhaug, Nygaard Simula 1 in 1966 (strictly a simulation language) General language ideas Influenced by Hoare s ideas on data types Added classes and prefixing (subtyping) to Algol 60 Nygaard Operations Research specialist and political activist Wanted language to describe social and industrial systems Allow ordinary people to understand political (?) changes Dahl and Myhrhaug Maintained concern for general programming Comparison to Algol 60 Added features class concept reference variables (pointers to objects) pass by reference char, text, I/O coroutines Removed Changed default par passing from pass by name some var initialization requirements own (=C static) variables string type (in favor of text type) Objects in Simula Example: Circles and lines Class A procedure that returns a pointer to its activation record Object Activation record produced by call to a class Object access Access any local variable or procedures using dot notation: object. Memory management Objects are garbage collected user destructors considered undesirable Problem Find the center and radius of the circle passing through three distinct points, p, q, and r Solution Draw intersecting circles Cp, Cq around p,q and circles Cq, Cr around q, r (Picture assumes Cq = Cq ) Draw lines through circle intersections The intersection of the lines is the center of the desired circle. Error if the points are colinear. p q r 1

Approach in Simula Methodology Represent points, lines, and circles as objects. Equip objects with necessary operations. Operations Point equality(anotherpoint) : boolean distance(anotherpoint) : real (needed to construct circles) Line parallelto(anotherline) : boolean (to see if lines intersect) meets(anotherline) : REF(Point) Circle intersects(anothercircle) : REF(Line) Simula Point Class class Point(x,y); real x,y; formal p is pointer to Point begin boolean procedure equals(p); ref(point) p; if p =/= none then equals := abs(x p.x) + abs(y p.y) < 0.00001 real procedure distance(p); ref(point) p; if p == none then error else distance := sqrt(( x p.x )**2 + (y p.y) ** 2); end ***Point*** p : new Point(1.0, 2.5); q : new Point(2.0,3.5); if p.distance(q) > 2 then uninitialized ptr has value none pointer assignment p Representation of objects access link real x 1.0 real y 2.5 proc equals proc distance code for equals code for distance Object is represented by activation record with access link to find global variables according to static scoping Simula line class class Line(a,b,c); real a,b,c; begin boolean procedure parallelto(l); ref(line) l; if l =/= none then parallelto := ref(point) procedure meets(l); ref(line) l; begin real t; if l =/= none and ~parallelto(l) lt (l)then end; real d; d := sqrt(a**2 + b**2); if d = 0.0 then error else begin d := 1/d; a := a*d; b := b*d; c := c*d; end; end *** Line*** Local variables line determined by ax+by+c=0 Procedures Initialization: normalize a,b,c Derived classes in Simula Subtyping A class decl may be prefixed by a class name class A A class B A class C B class D An object of a prefixed class is the concatenation of objects of each class in prefix d : new D( ) d A part B part D part The type of an object is its class The type associated with a subclass is treated as a subtype of the type assoc with superclass Example: class A( ); A class B( ); ref (A) a : new A( ) ref (B) b : new B( ) a := b /* legal since B is subclass of A */ b := a /* also legal, but run time test */ 2

Main object oriented features Classes Objects Inheritance ( class prefixing ) Subtyping Virtual methods A function can be redefined in subclass Inner Combines code of superclass with code of subclass Inspect/Qua run time class/type tests Features absent from Simula 67 Encapsulation All data and functions accessible; no private, protected Self/Super mechanism of Smalltalk But has an expression this class to refer to object itself, regarded as object of type class. Not clear how powerful this is Class variables But can have global variables Exceptions Not fundamentally an OO feature Simula Summary Class procedure" that returns ptr to activation record initialization code always run as procedure body Objects: closure created by a class Encapsulation protected and private not recognized in 1967 added later and used as basis for C++ Subtyping: determined by class hierarchy Inheritance: provided by class prefixing Smalltalk Major language that popularized objects Developed at Xerox PARC Smalltalk 76, Smalltalk 80 were important versions Object metaphor extended and refined Used some ideas from Simula, but very different lang Everything is an object, even a class All operations are messages to objects Very flexible and powerful language Similar to everything is a list in Lisp, but more so Example: object can detect that it has received a message it does not understand, can try to figure out how to respond. Motivating application: Dynabook Concept developed by Alan Kay Small portable computer Revolutionary idea in early 1970 s At the time, a minicomputer was shared by 10 people, stored in a machine room. What would you compute on an airplane? Influence on Smalltalk Language intended to be programming language and operating system interface Intended for non programmer Syntax presented by language specific editor Smalltalk language terminology Object Instance of some class Class Defines behavior of its objects Selector Name of a message Message Sl Selector together th with parameter values Method Instance variable Data stored in object Subclass Class defined by giving incremental Code used by a class to respond to message modifications to some superclass 3

Example: Point class Class messages and methods Class definition written in tabular form class name Point super class Object class var pi instance var x y class messages and methods names and code for methods instance messages and methods names and code for methods Three class methods newx:xvalue Y:yvalue ^ self new x: xvalue y: yvalue neworigin ^ self new x: 0 y: 0 initialize pi < 3.14159 Explanation - selector is mix-fix newx:y: e.g, Point newx:3 Y:2 - symbol ^ marks return value - new is method in all classes, inherited from Object - marks scope for local decl - initialize method sets pi, called automatically - <- is syntax for assignment Instance messages and methods Run time representation of point Five instance methods x: xcoord y: ycoord x < xcoord y < ycoord movedx: dx Dy: dy x < dx + x y < dy + y x ^x y ^y draw code to draw point Explanation set x,y coordinates, e.g, pt x:5 y:3 move point by given amount return hidden inst var x return hidden inst var y draw point on screen Point object class x 3 y 2 Detail: class method shown in dictionary, but lookup procedure distinguishes class and instance methods Point class newx:y: move to superclass Object Method dictionary Template x y code code Inheritance Define colored points from points class name ColorPoint super class Point class var instance var color class messages and methods newx:xv Y:yv C:cv code instance messages and methods color ^color draw code new instance variable new method override Point method Run time representation Point object Point class Template x Method dictionary 2 y newx:y: 3 draw move ColorPoint object ColorPoint class Template x Method dictionary 4 y newx:y:c: 5 red color color draw This is a schematic diagram meant to illustrate the main idea. Actual implementations may differ. 4

Encapsulation in Smalltalk Methods are public Instance variables are hidden Not visible to other objects pt x is not allowed unless x is a method But may be manipulated by subclass methods This limits ability to establish invariants Example: Superclass maintains sorted list of messages with some selector, say insert Subclass may access this list directly, rearrange order Object types Each object has interface Set of instance methods declared in class Example: Point { x:y:, movedx:dy:, x, y, draw} ColorPoint { x:y:, movedx:dy:, x, y, color, draw} This is a form of type Names of methods, does not include type/protocol of arguments Object expression and type Send message to object p draw p x:3 y:4 Subtyping Relation between interfaces Suppose expression makes sense p msg:pars OK if msg is in interface of p Replace p by q if interface of q contains interface of p Subtyping If interface is superset, then a subtype Example: ColorPoint subtype of Point Sometimes called conformance Subtyping and Inheritance Subtyping is implicit Not a part of the programming language Important aspect of how systems are built Inheritance is explicit it Used to implement systems No forced relationship to subtyping Collection Hierarchy Smalltalk Flexibility at: Indexed Updatable Array replacefrom:to:with: Collection at:put: Dictionary associationat: isempty, size, includes:, Set Sorted collection Subtyping Inheritance add: remove: sortblock: Measure of PL expressiveness: Can constructs of the language be defined in the language itself? Examples: Lisp cond: Lisp allows user defined special forms ML datatype: sufficient to define polymorphic lists, equivalent to built in list type ML overloading: limitation, since not available to programmer C/C++:??? Smalltalk is expressive in this sense M h ld b i i i h 5

Smalltalk booleans and blocks Boolean value is object with iftrue:iffalse: Class boolean with subclasses True and False True iftrue:b1 iffalse:b2 executes B1 False iftrue:b1 iffalse:b2 executes B2 Example expression i < j iftrue: [i add 1] iffalse: [j subtract 1] i < j is boolean expression, produces boolean object arg s are blocks, objects with execute methods Since booleans and blocks are very common Self and Super Integer SmallInt Factorial il self <= 1 iftrue: [^1] iffalse: [^(self 1) factorial * self ] LargeInt This method can be implemented in Integer, and works even if SmallInt and LargeInt are represented differently. C++ and Java type systems can t really cope with this. Ingalls test Dan Ingalls: principal designer Smalltalk system Grace Murray Hopper award for Smalltalk and Bitmap graphics work at Xerox PARC 1987 ACM SoftwareSystemsSystems Awardwith with Kay, Goldberg Proposed test for object oriented Can you define a new kind of integer, put your new integers into rectangles (which are already part of the window system), ask the system to blacken a rectangle, and have everything work? Smalltalk passes, C++ fails this test Smalltalk integer operations Integer expression x plus: 1 times: 3 plus: (y plus: 1) print Properties All operations are executed by sending messages If x is from some new kind of integer, expression makes sense as long as x has plus, times, print methods. Actually, compiler does some optimization. But will revert to this if x is not built in integer. Costs and benefits of true OO Why is property of Ingalls test useful? Everything is an object All objects are accessed only through interface Makesprograms extensible What is implementation cost? Every integer operation involves method call Unless optimizing compiler can recognize many cases Is this worth it? One application where it seems useful? One application where it seems too costly? Are there other issues? Security? (wait for Java final Smalltalk Summary Class creates objects that share methods pointers to template, dictionary, parent class Obj t d b l bl Objects: created by a class, contains instance variables Encapsulation methods public, instance variables hidden Subtyping: implicit, no static type system Inheritance: subclasses, self, super Single inheritance in Smalltalk 76, Smalltalk 80 6

Ruby www.ruby lang.org Example Ruby class Ruby is a complete, full, pure object oriented language All data in Ruby are objects, in the sense of Smalltalk Example: the number 1 is an instance of class Fixnum Very flexible Can add methods to a class, or even to instance during runtime Closures Automatic small integer (Fixnum) large integer (Bignum) conversion Single inheritance only Ruby features single inheritance only, *on purpose* Modules are collections of methods A class can import a module and gets all its methods class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 "This song: #@plays plays. Total #@@plays plays." end end see http://www.rubycentral.com/book/ 7