Languages and Compilers (SProg og Oversættere)

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Chapter 12: Support for Object-Oriented Programming
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Object Oriented Programming Daniel Albu Based on a lecture by John Mitchell Reading: Chapter 10.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Concepts in Object-Oriented Programming Languages Slides Today Taken From John Mitchell.
C++ fundamentals.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Chapter 12 Support for Object oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Languages and Compilers (SProg og Oversættere) Abstract Data Types and Object Oriented Features Bent Thomsen Department of Computer Science Aalborg University.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Principles of programming languages 10: Object oriented languages
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Abstract Data Types and Encapsulation Concepts
Object-Oriented Analysis and Design
Object-Oriented Modeling with UML
Multi-Methods in Cecil
The Object-Oriented Thought Process Chapter 1
CS 326 Programming Languages, Concepts and Implementation
Sequences and Iterators
Software Engineering Fall 2005
OOP What is problem? Solution? OOP
Review: Two Programming Paradigms
11.1 The Concept of Abstraction
Object Oriented Programming in Java
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Types of Programming Languages
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
Lecture 9 Concepts of Programming Languages
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Inheritance Basics Programming with Inheritance
Abstract Data Types and Encapsulation Concepts
Week 6 Object-Oriented Programming (2): Polymorphism
The Procedure Abstraction Part V: Run-time Structures for OOLs
Chapter 9 Objects and Classes
More Object-Oriented Programming
Dr. Bhargavi Dept of CS CHRIST
Parameter Passing Actual vs formal parameters
Computer Programming with JAVA
Support for Object-Oriented Programming
Introduction to Data Structure
Bent Thomsen Department of Computer Science Aalborg University
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Lecture 3 – Data collection List ADT
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell who’s slides this lecture is based on.

Varieties of OO languages class-based languages behavior of object determined by its class object-based objects defined directly multi-methods operation depends on all operands

History Simula 1960’s Smalltalk 1970’s C++ 1980’s Java 1990’s Object concept used in simulation Smalltalk 1970’s Object-oriented design, systems C++ 1980’s Adapted Simula ideas to C Java 1990’s Distributed programming, internet

Objects An object consists of Object-oriented program: hidden data instance variables, also called member data hidden functions also possible public operations methods or member functions can also have public variables in some languages Object-oriented program: Send messages to objects hidden data method1 msg1 . . . methodn msgn

What’s interesting about this? Universal encapsulation construct Data structure File system Database Window Integer Metaphor usefully ambiguous sequential or concurrent computation distributed, sync. or async. communication

Object-oriented programming Programming methodology organize concepts into objects and classes build extensible systems Language concepts encapsulate data and functions into objects subtyping allows extensions of data types inheritance allows reuse of implementation dynamic lookup

Dynamic Lookup In object-oriented programming, object  message (arguments) object.method(arguments) code depends on object and message In conventional programming, operation (operands) meaning of operation is always the same

Example Add two numbers x  add (y) different add if x is integer, complex Conventional programming add (x, y) function add has fixed meaning Important distinction: Overloading is resolved at compile time, Dynamic lookup at run time.

Encapsulation Builder of a concept has detailed view User of a concept has “abstract” view Encapsulation is the mechanism for separating these two views message Object

Comparison Traditional approach to encapsulation is through abstract data types Advantage Separate interface from implementation Disadvantage Not extensible in the way that OOP is We will look at ADT’s example to see what problem is

Abstract data types with abstype q mk_Queue : unit -> q is_empty : q -> bool insert : q * elem -> q remove : q -> elem is … in program end

Priority Q, similar to Queue 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

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

Subtyping and Inheritance Interface The external view of an object Subtyping Relation between interfaces Implementation The internal representation of an object Inheritance Relation between implementations

Object Interfaces Interface Example: point The messages understood by an object Example: point x-coord : returns x-coordinate of a point y-coord : returns y-coordinate of a point move : method for changing location The interface of an object is its type.

Subtyping If interface A contains all of interface B, then A objects can also be used B objects. Point x-coord y-coord move Colored_point x-coord y-coord color move change_color Colored_point interface contains Point Colored_point is a subtype of Point

Inheritance Implementation mechanism New objects may be defined by reusing implementations of other objects

Example Subtyping Inheritance class Point class Colored_point private float x, y public point move (float dx, float dy); class Colored_point float x, y; color c point move(float dx, float dy); point change_color(color newc); Subtyping Colored points can be used in place of points Property used by client program Inheritance Colored points can be implemented by resuing point implementation Propetry used by implementor of classes

OO Program Structure Group data and functions Class Subtyping Defines behavior of all objects that are instances of the class Subtyping Place similar data in related classes Inheritance Avoid reimplementing functions that are already defined

Example: Geometry Library Define general concept shape Implement two shapes: circle, rectangle Functions on implemented shapes center, move, rotate, print Anticipate additions to library

Shapes Interface of every shape must include center, move, rotate, print Different kinds of shapes are implemented differently Square: four points, representing corners Circle: center point and radius

Subtype hierarchy General interface defined in the shape class Circle Rectangle General interface defined in the shape class Implementations defined in circle, rectangle Extend hierarchy with additional shapes

Code placed in classes Dynamic lookup Conventional organization center move rotate print Circle c_center c_move c_rotate c_print Rectangle r_center r_move r_rotate r_print Dynamic lookup circle  move(x,y) calls function c_move Conventional organization Place c_move, r_move in move function

Example use: Processing Loop Remove shape from work queue Perform action Control loop does not know the type of each shape

Subtyping differs from inheritance Collection Indexed Set Array Dictionary Sorted Set String Subtyping Inheritance

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

Run-time representation of point to superclass Object Point class Template Point object x class y x 3 y 2 Method dictionary code newX:Y: ... Detail: class method shown in dictionary, but lookup procedure distinguishes class and instance methods ... move code

Access Control In many OOPLs it is possible to declare attributes (and methods) private or public or protected etc. This has no effect on the running program, but simply means that the compiler will reject programs which violate the access-rules specified The control is done as part of static semantic analysis

Dynamic (late) Binding Consider the method call: x.f(a,b) where is f defined? in the class (type)of x? Or in a predecessor? If multiple inheritance is supported then the entire predecessor graph must be searched: This costs a large overhead in dynamic typed languages like Smalltalk (normally these languages don’t support multiple inheritance) In static typed languages like Java, Eiffel, C++ the compiler is able to analyse the class-hierarchy (or more precise: the graph) for x and create a display-array containing addresses for all methods of an object (including inherited methods) According to Meyer the overhead of this compared to static binding is at most 30%, and overhead decreases with complexity of the method

fig14.2

fig14.3

Multiple Inheritance In the case of simple inheritance, each class may have one direct predecessor; multiple inheritance allows a class to have several direct predecessors. In this case the simple ways of accessing attributes and binding method-calls (shown previously) don’t work. The problem: if class C inherits class A and class B the objects of class C cannot begin with attributes inherited from A and at the same time begin with attributes inherited from B. In addition to these implementation problems multiple inheritance also introduces problems at the language (conceptual) level. Person - Lærer - Student - Instructor - Problemet

Attribute Offset with Multiple Inheritance The problem is actually a graph colouring problem: let each name be represented by a vertex if two names are in the same scope (perhaps through inheritance) then they are connected by an edge all vertices which are adjacent must have different offsets this approach may cause empty slots in an object (fig. 14.4) the offsets (and the waste of space) may be moved to the class-descriptor (and there much more objects than classes) costing an overhead of two instructions per fetch or store (fig. 14.5) this analysis can be done compile-time a similar technique may be used for dynamic binding of methods

Implementation of Object Oriented Languages Implementation of Object Oriented Languages differs only slightly from implementations of block structured imperative languages Some additional work to do for the contextual analysis Access control, e.g. private, public, procted directives Subtyping can be tricky to implement correctly The main difference is that methods usually have to be looked up dynamically, thus adding a bit of run-time overhead Only multiple inheritance poses a bigger problem