Konzepte von Programmiersprachen

Similar documents
CS422 - Programming Language Design

9 Objects and Classes

Lecture 16: Object Programming Languages

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Java Object Oriented Design. CSC207 Fall 2014

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

Object typing and subtypes

G Programming Languages - Fall 2012

Subtyping (Dynamic Polymorphism)

ITI Introduction to Computing II

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

ITI Introduction to Computing II

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

CS558 Programming Languages

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

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Principles of Programming Languages, 2

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CS558 Programming Languages Winter 2013 Lecture 8

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

2. Classes & Inheritance. Classes. Classes. Éric Tanter Objects and Design in Smalltalk. How do you create objects?

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Data Abstraction. Hwansoo Han

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

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

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

CS 32. Lecture 1: oops

Lecture Notes on Programming Languages

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

Objects, Encapsulation, Inheritance (2)

Object-Oriented Design

Inheritance. Transitivity

What about Object-Oriented Languages?

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

Some instance messages and methods

Programming Languages and Techniques (CIS120)

QUESTIONS FOR AVERAGE BLOOMERS

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

Interfaces, Mixins, & Multiple Inheritance

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

Object-oriented Programming. Object-oriented Programming

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

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

C++ Important Questions with Answers

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

CS250 Final Review Questions

An Introduction to Object Orientation

C++ Yanyan SHEN. slide 1

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

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

Reusing Classes. Hendrik Speleers

Object-Oriented Concepts and Design Principles

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

2. The object-oriented paradigm

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

CS153: Compilers Lecture 11: Compiling Objects

Overview. Elements of Programming Languages. Objects. Self-Reference

Programming Languages & Paradigms PROP HT Abstraction & Modularity. Inheritance vs. delegation, method vs. message. Modularity, cont d.

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

New Programming Paradigms

Programming Languages and Techniques (CIS120)

JAVA MOCK TEST JAVA MOCK TEST II

2. The object-oriented paradigm!

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

Lecture 23: Object Lifetime and Garbage Collection

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

Programming Languages and Techniques (CIS120)

Chapter 5 Object-Oriented Programming

Object Model. Object Oriented Programming Spring 2015

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

Conformance. Object-Oriented Programming Spring 2015

Chapter 5. Inheritance

The Java Programming Language

CS-202 Introduction to Object Oriented Programming

PROGRAMMING LANGUAGE 2

ECE 122. Engineering Problem Solving with Java

Functional Programming. Introduction To Cool

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

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

Overview. Elements of Programming Languages. Objects. Self-Reference

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

OOPLs - Inheritance. Inheritance

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance -- Introduction

Type Hierarchy. Lecture 6: OOP, autumn 2003

COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan

Outline. More optimizations for our interpreter. Types for objects

CPS 506 Comparative Programming Languages. Programming Language

Have classes that describe the format of objects. Create objects by stating the class of the object to be created.

CIS 110: Introduction to computer programming

Programming Languages

Object-Oriented Design

Transcription:

Konzepte von Programmiersprachen Chapter 9: Objects and Classes Peter Thiemann Universität Freiburg 9. Juli 2009 Konzepte von Programmiersprachen 1 / 22

Objects state encapsulation (instance vars, fields) package behavior with state (methods) Konzepte von Programmiersprachen 2 / 22

OO View of the World everything is an object (Smalltalk) computation proceeds by message passing: one object send method name and arguments to receiver object synchronous mp: procedure call asynchronous mp: blends with concurrent programming Konzepte von Programmiersprachen 3 / 22

Special Case: Procedures Procedure (closure) object with one kind of behavior Free variables fields (possible implementation of objects) in general: different behaviors wanted Konzepte von Programmiersprachen 4 / 22

Class-based OO Languages Class specifies fields and methods inheritance allows sharing specification and code between similar classes derived class may add fields and methods may reuse and/or override methods of the superclass Examples: Smalltalk, Eiffel, Java, C++,... Konzepte von Programmiersprachen 5 / 22

Prototype-based OO Languages Objects are constructed as needed Some objects serve as templates / prototypes Either cloned or otherwise reused Examples: Self, JavaScript Konzepte von Programmiersprachen 6 / 22

Objects vs. Modules Commonality: mechanism for defining opaque types Differences Object data structure with behavior Module just bindings, but with sophisticated control Konzepte von Programmiersprachen 7 / 22

CLASSES Konzepte von Programmiersprachen 8 / 22

The Language CLASSES class c1 extends object field i field j method initialize(x) begin set i = x; set j = -(0,x) end method countup(d) begin set i = -(i,-(0,d)); set j = -(j,d) end method getstate()list(i,j) let o1 = new c1(3) t1 = 0 t2 = 0 in begin set t1 = send o1 getstate(); send o1 countup(2); set t2 = send o1 getstate(); list(t1,t2) end Konzepte von Programmiersprachen 9 / 22

Dynamic Dispatch class interior_node extends object field left field right method initialize(l,r) begin set left = l; set right = r end method sum() -(send left sum(), -(0, send right sum())) class leaf_node extends object field value method initialize(v)set value = v method sum()value let o1 = new interior_node ( new interior_node ( new leaf_node(3), new leaf_node(4)), new leaf_node(5)) in send o1 sum() Konzepte von Programmiersprachen 10 / 22

Mutually Recursive Methods class oddeven extends object method initialize()1 method even(n) if zero?(n) then 1 else send self odd (-(n,1)) method odd(n) if zero?(n) then 0 else send self even (-(n,1)) let o1 = new oddeven() in send o1 odd(13) Konzepte von Programmiersprachen 11 / 22

Inheritance Konzepte von Programmiersprachen 12 / 22

Terminology Inheritance: define new class C by modification of existing ones D 1,..., D n C extends D 1... D n D i is parent or superclass of C C is child, subclass, or derived class of D i Single Inheritance: n 1, tree-shaped class hierarchy Multiple Inheritance: n > 0, class relations form a DAG Substitution principle / subclass polymorphism: an instance of a child class can be used wherever an instance of its parent class is expected Konzepte von Programmiersprachen 13 / 22

Example of Inheritance class point extends object field x field y method initialize (initx, inity) begin set x = initx; set y = inity end method move (dx, dy) begin set x = -(x,-(0,dx)); set y = -(y,-(0,dy)) end method get_location () list(x,y) Konzepte von Programmiersprachen 14 / 22

Example of Inheritance (contd) class colorpoint extends point field color method set_color (c) set color = c method get_color () color let p = new point(3, 4) cp = new colorpoint(10, 20) in begin send p move(3, 4); send cp set_color(87); send cp move(10, 20); list(send p get_location(), % returns (6 8) send cp get_location(), % returns (20 40 send cp get_color()) % returns 87 end Konzepte von Programmiersprachen 15 / 22

Example of Field Shadowing class c1 extends object field x field y method initialize() 1 method setx1 (v) set x = v method sety1 (v) set y = v method getx1 () x method gety1 () y class c2 extends c1 field y method sety2 (v) set y = v method getx2 () x method gety2 () y Konzepte von Programmiersprachen 16 / 22

Example of Field Shadowing (contd) let o = new c2() in begin send o2 setx1 (101); send o2 sety1 (102); send o2 sety2 (999); list(send o2 getx1(), send o2 gety1(), send o2 getx2(), send o2 gety2()) end Konzepte von Programmiersprachen 17 / 22

Method Overriding class c1 extends object method initialize()1 method m1()1 method m2()100 method m3()send self m2() class c2 extends c1 method m2()2 let o1 = new c1() o2 = new c2() in list(send o1 m1(), % returns 1 send o1 m2(), % returns 100 send o1 m3(), % returns 100 send o2 m1(), % returns 1 (from c1) send o2 m2(), % returns 2 (from c2) send o2 m3() % returns 2 (c1 s m3 calls ) Konzepte von Programmiersprachen 18 / 22

Super Calls The Need class point extends object field x field y method initialize (initx, inity) begin set x = initx; set y = inity end method move (dx, dy) begin set x = -(x,-(0,dx)); set y = -(y,-(0,dy)) end method get_location () list(x,y) Konzepte von Programmiersprachen 19 / 22

Super Calls The Need (contd) class colorpoint extends point field color method set_color (c) set color = c method get_color () color let p = new point(3, 4) cp = new colorpoint(10, 20) in begin send p move(3, 4); send cp set_color(87); send cp move(10, 20); list(send p get_location(), % returns (6 8) send cp get_location(), % returns (20 40 send cp get_color()) % returns 87 end Konzepte von Programmiersprachen 20 / 22

Super Calls The Solution class colorpoint extends point field color method set_color (c) set color = c method get_color () color method initialize (x,y,c) begin super initialize (x,y); set color = c end let p = new point(3, 4) cp = new colorpoint(10, 20, 30) in begin send p move(3, 4); send cp set_color(87); send cp move(10, 20); list(send p get_location(), % returns (6 8) send cp get_location(), % returns (20 40 send cp get_color()) % returns 87 end Konzepte von Programmiersprachen 21 / 22

Interaction of Super Calls with Self class c1 extends object method initialize () 1 method m1 () send self m2 () method m2 () 13 class c2 extends c1 method m1 () 22 method m2 () 23 method m3 () super m1 () class c3 extends c2 method m1 () 32 method m2 () 33 let o3 = new c3() in send o3 m3() Konzepte von Programmiersprachen 22 / 22