Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Similar documents
Object Oriented Issues in VDM++

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

Programmiersprachen (Programming Languages)

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Comparing procedure specifications

Assertions. Assertions - Example

Advanced JML Erik Poll Radboud University Nijmegen

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

Assertions, pre/postconditions

6.170 Lecture 6 Procedure specifications MIT EECS

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Type Hierarchy. Lecture 6: OOP, autumn 2003

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Safety SPL/2010 SPL/20 1

Lecture 4: Procedure Specifications

Classes, interfaces, & documentation. Review of basic building blocks

Basic Principles of analysis and testing software

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Specification tips and pitfalls

Sharing Objects Ch. 3

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Agenda. More on the Unified Modeling Language. UML diagram types. Packages

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

An Annotated Language

CSE331 Spring 2015, Final Examination June 8, 2015

Advances in Programming Languages

The Contract Pattern. Design by contract

Conformance. Object-Oriented Programming Spring 2015

Object-Oriented Design

Design by Contract: An Overview

The Java Memory Model

The Java Modeling Language JML

Equality for Abstract Data Types

Jam: A Smooth Extension With Java Mixins

Procedural Abstraction

Object-oriented Programming. Object-oriented Programming

Specifications. CSE 331 Spring 2010

Universe Type System for Eiffel. Annetta Schaad

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

6.170 Lecture 7 Abstract Data Types MIT EECS

CSC Advanced Object Oriented Programming, Spring Specification

Software Specifications. CMSC 433 Programming Language Technologies and Paradigms Spring Specifications Help You Write Code

Rules and syntax for inheritance. The boring stuff

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Chapter 4 Defining Classes I

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

Lecture 7: Data Abstractions

Contracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

The Java Memory Model

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

Generics with Type Bounds

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

The New Java Technology Memory Model

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CITS5501 Software Testing and Quality Assurance Formal methods

Review of last lecture. Goals of this lecture. DPHPC Overview. Lock-based queue. Lock-based queue

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Semantic Analysis and Type Checking

6.170 Recitation #5: Subtypes and Inheritance

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

Safely Creating Correct Subclasses without Seeing Superclass Code

Formal Methods for Java

Type Checking in COOL (II) Lecture 10

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Symmetry in Type Theory

A Practical Type System and Language for Reference Immutability

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

Outline. iterator review iterator implementation the Java foreach statement testing

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

Principles of Object-Oriented Design

Axiomatic Specification. Al-Said, Apcar, Jerejian

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Runtime assertion checking of multithreaded Java programs

The Java Type System (continued)

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Java Semaphore (Part 1)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Introduction to Object-Oriented Programming

CS 161 Computer Security

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Why testing and analysis. Software Testing. A framework for software testing. Outline. Software Qualities. Dependability Properties

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

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

Contract Programming For C++0x

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Programming Languages Seminar. Lecture in the subject of. Yair Moshe

Java: advanced object-oriented features

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Inheritance and Substitution (Budd chapter 8, 10)

Reasoning About Imperative Programs. COS 441 Slides 10

301AA - Advanced Programming [AP-2017]

Transcription:

2014-03-07 Preface Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming) Coordinates: Lecturer: Web: Studies: Requirements: No. 185.211, VU, 3 ECTS Franz Puntigam http://www.complang.tuwien.ac.at/franz/foop.html Software Engineering & Internet Computing Computational Intelligence Visual Computing experience in object-oriented programming Registration: in TISS until 13.03.2014 group building: 14.03.2014 during lecture

2014-03-07 Substitutability and Assertions Substitutability and Assertions

2014-03-07 Substitutability and Assertions 1 Principle of Substitutability Type S is subtype of a type T iff each instance of S is usable where an instance of T is expected Principle of substitutability holds if types of input parameters are contravariant, types of variables and through-parameters are invariant, types of constants, results, output parameters are covariant, and corresponding methods in S show same behaviour as those in T

2014-03-07 Substitutability and Assertions 2 Substitutability and Behaviour Methods in subtype S show same behaviour as those in supertype T if assertions to be fulfilled by clients (= preconditions) in S not stronger than those in T assertions to be fulfilled by servers (= postconditions and invariants) in S not weaker than those in T assertions to be fulfilled by clients and servers (only useful for invariants in some cases) equivalent in U and T methods in U do not throw more exceptions than those in T (in corresponding situations)

2014-03-07 Substitutability and Assertions 3 Expressing Assertions In theory, all assertions formally expressible (logics, algebra), relationships between assertions statically checkable In practice, many assertions not expressible and relationships not checkable because programming language has insufficient support (informal comments, usually ambiguous) clients have not enough information about object state (data hiding in conflict with Design-by-Contract) object state changes in non-predictable way (concurrency, aliasing)

2014-03-07 Substitutability and Assertions 4 Example of Using Assertions class IntSet { public boolean find(int x) {... } // true iff x is in set public void insert(int x) {... } // immediately afer insertion x is in set... }... IntSet set = new IntSet(); set.insert(1); // now 1 is in set boolean a = set.find(1); do_something_not_using_set(); boolean b = set.find(1);

2014-03-07 Substitutability and Assertions 5 Possible Problems in Example Immediately after is ambiguous Usual interpretation: Element remains in set as long as no delete is invoked (that could be defined in a subtype) Constructor creating set could have introduced alias (different from set ) that causes do something not using set to change the set b possibly false Concurrent thread (spawned in the constructor) could have deleted element a and b possibly false

2014-03-07 Substitutability and Assertions 6 How to Solve the Problems Invoke find to check if the element is in the set often bad solution because nothing useful to do if check fails Prevent aliasing altogether bad solution because extremely expensive In case of concurrency always ensure atomic actions often a good idea, but sometimes very expensive avoid unexpected side-effects (e.g., in constructor) only useful alternative however, expectations hardly ever formally expressible

2014-03-07 Substitutability and Assertions 7 History Constraints Advice: Use all available information, no matter how easily expressible as usual assertions (pre-, post-conditions, invariants) Example: History constraints constrain the evolution of objects in a way hardly expressible as usual assertions like value of counter can only increase one by one (server responsible), or unlock invokable only after lock (client responsible). History constraints to be fulfilled by servers resemble invariants; they can be more restrictive in subtypes. History constraints to be fulfilled by clients restrict invocation sequences; subtypes can support more invocation sequences than supertypes.

2014-03-07 Substitutability and Assertions 8 Suggestions about Assertions Considering all possibilities would be too expensive; don t try to do so. Be predictable, avoid tricks, rely on usual behaviour of programmers. Use design rules (specific to company or project). Use all available information as assertions (including history constraints). First rule: Avoid unnecessary dependences! no avoidable assertions, no unneeded invocations and parameters, no unnecessary visibility of variables and methods, only well-considered parameter types code only deep in class hierarchy (= avoid inheritance)

2014-03-07 Names Names

2014-03-07 Names 1 Significance of Names Names are abstractions of object behaviour, method behaviour and variable properties, thereby resembling informal assertions. Names support intuition; comments necessary only if intuition insufficient good programs readable even without comments Intuitive names cause programmers to be predictable (bad names no trust unnecessary code) Names of types provide semantic information (not only for programmers, also for machines)

2014-03-07 Names 2 Examples of Structural Types Two structural (= anonymous) types in subtyping relation: { String name; String address() } { String name; String address(); int regnr } Members names used as type names: { String name; String address(); void isperson() } { String name; String address(); void isperson(); int regnr; void isstudent() } In theory we mainly use structural types, in practice we mainly use nominal (= named) types. Structural types can simulate nominal types, but accidental coincidence possible (can be faked)

2014-03-07 Names 3 Structural versus Nominal Types structural nominal type equivalence same structure same name subtyping implicit explicit (inheritance) usability simple more difficult plug & play easier not that easy name conflicts possible easily possible accidental relations possible unlikely readability not so good good abstraction of behaviour no yes

2014-03-07 Names 4 Nominal Types Ensure Properties Example: class SortedList<A extends Comparable<A>> {... } Property sorted hardly directly checkable, except by class membership Each instance of SortedList<T> initialized by a constructor in SortedList and its subclasses (faking almost impossible) If SortedList and its subclasses ensure the property sorted, we can rely on it

2014-03-07 Names 5 Structural Types have Benefits as bounds for bounded genericity when using plug & play on software components when introducing supertypes of already existing subtypes

2014-03-07 Names 6 Genericity with Anonymous Bounds Java example: class SortedList<A extends Comparable<A>> Why has A to inherit from Comparable<A>? Only requirement: A provides methods of Comparable<A> because Comparable<A> does not imply further properties Example in Ada showing the use of anonymous bounds: generic type T is private; with function compare(x,y: T) returns Boolean package SortedList...

2014-03-07 Names 7 Kinds of Bounded Genericity F-bounded Genericity: S T S can deal with binary methods, but restricted to one inheritance level Java example: Integer Comparable<Integer> (subtype relation = relation on bounds) Higher Order Subtyping: S<#T if U :S U T U also called matching (on functions over types) supports binary methods directly, but does not provide substitutability statically type-safe as bound in bounded genericity (subtype relation relation on bounds)