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

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

Conformance. Object-Oriented Programming Spring 2015

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

CS558 Programming Languages

CS153: Compilers Lecture 11: Compiling Objects

Object typing and subtypes

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

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

CPS 506 Comparative Programming Languages. Programming Language

CS558 Programming Languages Winter 2013 Lecture 8

Functional Programming. Introduction To Cool

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

G Programming Languages - Fall 2012

Konzepte von Programmiersprachen

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

Type Analysis. Type Checking vs. Type Inference

Programming Languages and Techniques (CIS120)

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

Subtyping (Dynamic Polymorphism)

C++ Yanyan SHEN. slide 1

Programming Languages and Techniques (CIS120)

Object Model. Object Oriented Programming Spring 2015

Programming Languages and Techniques (CIS120)

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

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

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

CS 32. Lecture 1: oops

CSE 307: Principles of Programming Languages

Data Abstraction. Hwansoo Han

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

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

Objects as Session-Typed Processes

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

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Topic 9: Type Checking

Topic 9: Type Checking

Object-oriented Programming. Object-oriented Programming

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CS263: Runtime Systems Lecture: High-level language virtual machines

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Java Generics

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

- couldn t be instantiated dynamically! - no type or other method of organizing, despite similarity to

OOPLs - Inheritance. Inheritance

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

Concepts of Programming Languages

CS558 Programming Languages

Principles of Software Construction: Objects, Design, and Concurrency

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Classes, subclasses, subtyping

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

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

CS 520 Theory and Practice of Software Engineering Fall 2017

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

Lecture 7: Data Abstractions

Type Checking and Type Inference

Top Down Design vs. Modularization

Inheritance and object compatibility

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

CSE 431S Type Checking. Washington University Spring 2013

Java Object Oriented Design. CSC207 Fall 2014

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

Polymorphism. Arizona State University 1

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Objects, Encapsulation, Inheritance (2)

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

Compiler construction 2009

An Introduction to Subtyping

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

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Overloading, Type Classes, and Algebraic Datatypes

CS 520 Theory and Practice of Software Engineering Fall 2018

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CSE Programming Languages Final exam - Spring Answer Key

Software Construction

22c:111 Programming Language Concepts. Fall Types I

COURSE 2 DESIGN PATTERNS

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

MIT AITI Lecture 18 Collections - Part 1

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

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

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

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Object Model. Object Oriented Programming Winter

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Basic Object-Oriented Concepts. 5-Oct-17

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Exercise 8 Parametric polymorphism November 18, 2016

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

1. Software Systems Complexity, OO Paradigm, UML

Derived and abstract data types. TDT4205 Lecture 15

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

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Transcription:

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

Administration Out of town this afternoon thru Monday HW1 due next Thursday 9/9

Types Last time: strongly typed if no type errors occur at run time statically typed languages rule out type errors at compile time requires programmers to write type declarations dynamically typed languages rule out type errors at run time dynamic checks before operations

Records A record is a value consisting of a sequence of named fields Declaring a record: C: struct { int a; float b; } Pascal: record a: integer; b: real; end Support access operations via field names: x.a, x.b

Records as ADTs struct IntList { int head; struct IntList *tail; // must be a pointer! }

Records as ADTs struct IntList { int head; struct IntList *tail; // must be a pointer! } int get(intlist *xs, int i) { if (i == 0) return xs->head; else get(xs->tail, i-1); } void append(intlist *xs, IntList *ys) { if (xs->tail == NULL) xs->tail = ys; else append(xs->tail, ys); }

Problems Implementation exposed. Only one implementation supported. Clients of the ADT must all use same implementation.

Objects Focus is on data rather than on processes. Programs composed of self-contained, interacting objects. vs. as a list of tasks to perform Bundle data with the operations on that data Encapsulate (hide) implementation from clients. Can only access data through public interface.

Objects Source code for class defines concrete type (implementation) Interface defined by public variables and methods of a class class IntList { private int head; private IntList tail; public int get(int i) { if (i == 0) return this.head; else return get(this.tail, i-1); } public void append(intlist ys) { if (this.tail == null) this.tail = ys; else this.tail.append(ys); } }

OO languages Not an exhaustive survey Simula 67 C++ Java C# C Objective C Scala Smalltalk Self JavaScript Ruby LISP CLOS ML OCaml

OO languages Two kinds of OO languages class-based Simula, Smalltalk, C++, Java, C#, Scala prototype-based Self, Cecil, JavaScript

Class-based languages A class defines a template for creating objects defines what members an object of class C ( instance ) has fields (aka member variables, instance variables) methods (aka member functions) All instances have the same structure same methods, same fields We ll ignore static members for now

Inheritance of classes A subclass (derived class) inherits from (extends) one or more superclasses (base class) As if members of the superclass were copied down into the subclass declaration Instance of subclass contains all members of an instance of the superclass + new members defined by the subclass

Java class Point { int x, y; void move(int dx, int dy) { x += dx; y += dy; } } x y class ColorPoint extends Point { Color c; void redden() { c = Color.RED; } } x y c

Prototype-based languages No notion of class Inheritance is by delegation To create a new object: create an object from nothing (ex nihilo), or clone another object (the prototype) modify the new object maintain a reference (delegate) to the original

JavaScript // create two objects var p = {x: 1, y: 2}; var cp = {color: red }; // make cp extend p cp. proto = p; cp.x // 1 cp.y // 2 cp.color // red

Delegation If object does not contain a given field, check its prototype Behaves similarly to inheritance

Multiple implementations OO languages let you have multiple implementations of the same specification: class List { int length(); int get(int i); List append(list x); } class ArrayList extends List {... } class LinkedList extends List {... } class ConcList extends List {... }

Dispatching problem Problem: don t know what code to run at compile time List a =...; a.length(); ArrayList.length or LinkedList.length? Objects must know their implementation at run time

Compiling objects Add to each object an extra pointer to a dispatch vector (aka virtual table, vtable) with pointers to method code head tail len array LinkedList.length LinkedList.get LinkedList.append ArrayList.length ArrayList. get ArrayList. append Code receiving x : List only knows x has an initial dispatch vector pointer?.length?.get?.append

Polymorphism Code can use values with more than one type Object oriented languages support subtype polymorphism Good for heterogeneous data structures containing different implementations of the same interface Can mix different Animal implementations in the same list (Cat, Duck, Cow, Moose, TRex, Human, Sponge)

Type relationships Classes and their superclasses are related by a subtype relationship ArrayList <: List List LinkedList <: List LinkedList ArrayList

Subtypes One type extends another by allowing more operations class Point { Point } int x(); int y(); ColorPoint class ColorPoint { int x(); int y(); Color color(); ColorPoint <: Point is a subtype of (also:!) }

Subtyping Predicate view of types A type is a predicate on values T1 is a subtype of T2 if T1 s predicate implies T2 s Barney is a Dinosaur => Barney is an Animal Set-theoretic view of types A type is a set of values T1 is a subtype of T2 if T1 s set of values is a subset of T2 s The set of Dinosaurs is a subset of the set of Animals Note: it s a subset of values not a subset of operations

Substitution principle Can always substitute an instance of a subtype for an instance of a supertype and the program will have no type errors Bird ---> Penguin Liskov Substitution Principle: if P(x) is true about objects x of type T, then P(y) is true for objects y of type S, a subtype of T behavioral subtyping usually too strong to be enforceable

Subtype relation Subtyping is a binary relation on types Notation: T1 <: T2 T1 is a subtype of T2 <: is: reflexive: T <: T transitive: if T1 <: T2 and T2 <: T3, then T1 <: T3 antisymmetric: if T1 <: T2 and T2 <: T1, then T1 = T2

Type-checking assignment if x has type T1 e has type T2 then require T2 <: T1 x = e has type T1 Let T <: S!"#"$"%%%& '"("$"%%%& #"$"(&""""""""))"*++,-./ ("$"#&""""""""))"0,("*++,-./

Type-checking assignment x has type T1 if e has type T2 then x = e has type T1 T2 <: T1 Let T <: S 1!"#"$"%%%& 1 '"("$"%%%& 1 #"$"(&""""""""))"*++,-./ 1 ("$"#&""""""""))"0,("*++,-./

Subtyping in Java Classes: class C C <: Object class C extends D C <: D Interfaces: interface I I <: Object class C implements I C <: I Primitives: <: is = int <: int Why not int <: long? Arrays: int[] <: Object C[] <: Object if T1 <: T2 then T1[] <: T2[] Note: this rule is broken Why?

Primitives Why are primitive types different? What is the representation of a value of type int? Type long? Type Object?

Subtyping in Java Classes: class C C <: Object class C extends D C <: D Interfaces: interface I I <: Object class C implements I C <: I Primitives: <: is = int <: int Why not int <: long? Arrays: int[] <: Object C[] <: Object if T1 <: T2 then T1[] <: T2[] Note: this rule is broken Why?

Covariant array subtyping In Java: 1 20(.3.456"78"9:;.<(56 1 20(.3.456"="$"0.-"20(.3.45>?6& 1 9:;.<(56"@"$"=& 1 @5?6"$"A(BC#"C#"*"#(4C03D"0,("*0"C0(.3.4E& Will cause an ArrayStoreException at run-time