OOP Design by Contract. Carsten Schuermann Kasper Østerbye IT University Copenhagen

Similar documents
Introduction to JML David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

JML tool-supported specification for Java Erik Poll Radboud University Nijmegen

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

Advanced JML Erik Poll Radboud University Nijmegen

Program Verification (6EC version only)

Advanced JML. and more tips and pitfalls. David Cok, Joe Kiniry, and Erik Poll

Programming with Contracts. Juan Pablo Galeotti, Alessandra Gorla Saarland University, Germany

JML. Outline. Métodos Formais em Engenharia de Software. MI, Braga these slides were prepared by adopting/adapting teaching material

The Java Modeling Language JML

Lecture 10 Design by Contract

Assertions, pre/postconditions

Integrating verification in programming languages

The Java Modeling Language (Part 2)

Formal Specification and Verification

JML Class Specifications The Java Modeling Language (Part 2) A Java Class

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

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

Specification tips and pitfalls

Testing, Debugging, and Verification

CS 161 Computer Security

Assertions. Assertions - Example

JML. Java Modeling Language

Java: advanced object-oriented features

Formale Entwicklung objektorientierter Software

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

a correct statement? You need to know what the statement is supposed to do.

Overview The Java Modeling Language (Part 1) Related Work

CS 3 Introduction to Software Engineering. 5: Iterators

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

MSO Lecture Design by Contract"

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

ESC/Java2 Warnings David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

Design by Contract and JML: concepts and tools

6.001 Notes: Section 8.1

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Inheritance (Part 5) Odds and ends

VIRTUAL FUNCTIONS Chapter 10

Chapter 4 Defining Classes I

Java Collection Framework

What This Course Is About Design-by-Contract (DbC)

JAVA BASICS II. Example: FIFO

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

Computer Science 1 Ah

Outline. iterator review iterator implementation the Java foreach statement testing

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Formal Specification and Verification

Java Review: Objects

Lecture 4: Procedure Specifications

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

CSE 331 Midterm Exam Sample Solution 2/18/15

Advances in Programming Languages

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

The JML Tool. Faculty of Engineering Pontificia Universidad Javeriana. The JML Tool p.1/23

CSE 341, Autumn 2015, Ruby Introduction Summary

Java Modelling Language (JML) References

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

6.005 Elements of Software Construction Fall 2008

Advances in Programming Languages

Collections and Iterators. Collections

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

Design by Contract in Eiffel

Java Fundamentals (II)

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Design-by-Contract (Dbc) Test-Driven Development (TDD)

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Formal Methods for Software Development

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Software Construction

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Formal Methods for Software Development

Java Modelling Language (JML) References

ESC/Java2 Use and Features

Java Object Oriented Design. CSC207 Fall 2014

ESC/Java2 Use and Features

Advances in Programming Languages

An introduction to formal specifications and JML. Invariant properties

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CIS 120 Final Exam 15 December 2017

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

Arrays. Chapter Arrays What is an Array?

Verification Condition Generation

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

JAVA OBJECT-ORIENTED PROGRAMMING

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Classes Classes 2 / 36

Lecture Notes on Contracts

6.001 Notes: Section 6.1

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

The Java Modeling Language (Part 1)

Definition of DJ (Diminished Java)

Transcription:

OOP Design by Contract Carsten Schuermann Kasper Østerbye IT University Copenhagen 1

Today's schedule Design by Contract why the term contract what design issue is captured, and why bother what is a pre-condition what is a post-condition what is a class invariant How to do this in Java. you cannot, only in documentation experimental java s exist Assert in Java loop invariants design by contract 2

Design by contract Consider the service offered by the Danish postal service: For 4.50 dkr, they will deliver a letter to the address printed on the front of the envelope, if the letter is posted in Denmark, if it is less than 23x17x0.5 cm. If it is handed in before a given time of the day, there are a next day delivery guarantee. The advantage for me is that I know how much postage to put on a standard letter, I know when to post it, and when I should expect it to arrive. class DanishMailBox { /*pre: l is within size restrictions & current time is before postingdeadline post: letter l is delivered at address next day */ public void sendstandardletter(letter l){ public Time postingdeadline(){ The advantage for the postal service is to receive 4.50, not to have obligations for late delivery, and not to have obligations for odd size letters. The pre condition is what must be true before we start the method, the post condition is what must be true after the method has been executed. 3 It is the responsibility of the caller of the method to make sure the pre-condition is met, it is the responsibility of the called object to make sure that the post condition is met.

A Crash Course in Propositional Logic Boolean values Boolean expressions Boolean algebra Truth assignements Interpretations Models Tautology Proof rules 4

Iterator contract I An iterator is an object which will provide sequential access to a collection. The java.util.iterator<e> interface has the following methods : boolean hasnext() E next() void remove() hasnext returns true if the iterator is not empty. next returns the head, and moves to tail. remove removes the head from the underlying collection or throws an UnsupportedOperationException head tail An iterator is empty if it will not be able to produce any more elements. The next element which it will produce is called its head, and all the remaining is its tail. Note, given an iterator itr, itr.tail is an iterator which will return the same elements as itr, except for itr.head. The Iterator in java.util is one of the many examples of bad design in Java (there are more examples of good design though). It is not considered good design to throw the UnsupportedOperationException. Instead, the designers should have defined two interfaces. One named SimpleIterator, which has only the methods hasnext and next. And an other RemovableIterator, which extends SimpleIterator with the remove method: interface SimpleIterator{ boolean hasnext() E next() interface RemoveableIterator extends SimpleIterator{ void remove() This way a (collection) class can decide which kind of Iterator it wants to implement. 5

Iterator contract II An empty pre-condition means that the method can always be called. Notice, it is the responsibility of the caller to make sure that next() and peek() is not called when the list is empty. Notice, we will assume that the iterator does not change as a result of a call to cloneme(), since nothing is stated to indicate such behaviour public interface Iterator { /* pre: none * post: return true if the iterator is not empty. */ boolean hasnext(); /* pre: hasnext() * post: return head of old.iterator & this is old.tail. */ Object next(); head tail /* pre: next has been called & remove has not already been called & remove is supported by this iterator * post: removes the last element returned by next*/ void remove(); In the paper by Bertrand Meyer, Meyer uses the programming language Eiffel, which he has designed himself. The Eiffel language has constructs in the language itself to deal with pre and post conditions. In Java there is no such constructs and we can only write them in the comment of the methods. 6 There is often a need to refer in the post condition to the state of the class as it was before the operation. In the next() method, we say that this is the old.tail, which means that the iterator now is the tail of what it was before the next() call. There exists several experimental tools for Java which extends the Java language with support for design by contract. Try design by contract java on Google if you want to try the real thing.

An array iterator This iterator will iterate over the elements in an array which is given as argument to its constructor. The iterator can be used as: String[] names ={ Bill, Ben, Jack ; ArrayIterator itr = new ArrayIterator(names); while ( itr.hasnext() ) System.out.println( itr.next() ); But, will this actually print out the right elements? To examine this, we need to compare the implementation of each method to the contract to see if the ArrayIterator satisfies the Iterator contract, and not just defines methods with the right signature public class ArrayIterator implements Iterator{ private final Object[] thearray; private int index; // index of next element public ArrayIterator(Object[] objects){ thearray = objects; index = 0; public boolean hasnext(){ return index < thearray.length; public Object next(){ return thearray[index++]; public void remove(){ throw new UnsupportedOperationException(); The point of this, and the next slide is to introduce the notion of a class invariant. The reason we need this concept is that we cannot reason about the individual methods in isolation from the objects the methods works on. 7 The invariant helps to state something about the relationship between the fields of the object, and the purpose of the object, so we can argue in that each method does indeed do the right thing.

Class invariants But, we cannot see if the body does the right thing in isolation from the class itself. What is the the value of index, and what is thearray? A class invariant is a postulate on the fields of a class. The postulate is supposed to be true after each method has finished, and after the constructor has finished. It is the programmers responsibility to make certain that the invariant is maintained. /* pre: none * post: return true if the iterator is not empty. */ public boolean hasnext(){ return index < thearray.length; thearray index if index<thearray.length then index refers to the head of the iterator. else the iterator is empty If we assume that the invariant is as in the drawing, then hasnext() is correct. The rule of class invariants is that it is a postulate must be true after each method call and after the constructor has finished. Therefore it must also be true at the beginning of each method call. The invariant cannot be assumed to be true before the constructor has finished. 8 If we assume the invariant to be true before we call the hasnext() method, we know that either index<thearray.length, in which case index points to the head of the iterator or index >=thearray.length, in which case the iterator is empty. Thus, we test to see which of the two cases we are in, and if it is not empty, we return true.

Array Iterator, next and constructor Reg. next. We assume the invariant is true, and hasnext() is true, that is the iterator is not empty. Thus, we know that index refers to head, which is what we return. But we also increments index by one, which is what is needed to make sure that the iterator now is its tail. Reg. the constructor The precondition ensures that we will not attempt to iterate null. if objects has elements in it, then index=0 is the head of the iterator if objects it empty, then length is 0, and index is not less than 0, hence the iterator is empty. In both situations the invariant is true. /* pre: hasnext() * post: return head of iterator; this is old.tail. */ public Object next(){ return thearray[index++]; /* pre: objects!= null * post: invariant */ public ArrayIterator(Object[] objects){ thearray = objects; index = 0; thearray index if index<thearray.length then index refers to the head of the iterator. else the iterator is empty I use a dirty trick in the next method. Normally we use the increment operator x++ as a statement to add one to a variable x. But really, x++ is an expression, which returns the value of x, and then increments x. If you look at this code fragment: int x=8; System.out.println(x++); System.out.println(++x); It will print 8, 10. After the first print, x will have the value 9, but will print the value it had before it was incremented. In the second print, we first increment x, then print its new value. x++ is called post-increment, and ++x is called pre-increment, see Java Precisely at page 30, section 11.2. 9 So, return thearray[index++] corresponds to the longer Object o = thearray[index]; index++; return 0; It is often much easier to reason about the invariant being true after the constructor is executed if one does not use initializers.

Invariants and encapsulation Please observe the wording used when arguing for the pre/post conditions. They all say, If the invariant is true, then so and so and so and so, therefore the invariant is still true. But what if the invariant is not true? This is why it in general is a good idea to make fields private. If the index field was made public, then we could not be sure the invariant was valid, as someone might had changed it from the outside. Also notice that one should be very careful with setter methods for variables mentioned in the invariant. A setter for the index would be as bad as making it public public class ArrayIterator implements Iterator{ private final Object[] thearray; private int index; // index of next element public ArrayIterator(Object[] objects){ thearray = objects; index = 0; public boolean hasnext(){ return index < thearray.length; public Object next(){ return thearray[index++]; public void remove(){ throw new UnsupportedOperationException(); At a very high level of abstraction, one can say that one purpose of the encapsulation is to ensure that the invariant of the class can be ensured. There MUST NOT be public methods which break the invariant, as it is then not possible to know what the program does. 10

An invariant on Persons and Cars Consider the exercise on Persons and Car from lecture 2. There was three rules: 1. Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person. 2. If a person owns a car, that car is owned by that person, and vice versa. 3. A Person can own at most one car :Car owner: Person :Person mycar: Car public class Car { /* inv: owner!= null, & owner.mycar == this private Person owner; /* pre: p.mycar == null post: p.mycar = this. */ public Car(Person p){ owner = p; owner.setcar(this); /* pre: p.mycar == null post: p.mycar == this */ public void setowner(person p){ owner.setcar(null); owner = p owner.setcar(this);. The invariant states that the owner is not null, and that the mycar field of the owner refers back to this. That reflects the picture. 11 Constructor: A Person cannot own more than one car. We must assume (precondition) that the person given as parameter to the constructor is one who does not own a car. post condition states that the person p owns the car afterwards. owner = p, makes the reference from the car to the person p. owner.setcar( p ) makes the reference back again (See next slide). setowner: We must again assume that the new owner p does not already own a car (precondition). We start by detaching the old owner of the car from this car, so the old owner does not own the car nay more. We then set the reference from car to person, and the reference from person to car. The class Person is simpler, it has the following form class Person{ /* inv: if mycar!= null, mycar.owner = this. private Car mycar; Person(){; /* pre: mycar == null, c.owner = this post: inv

An invariant on Persons and Cars II The class Person is simpler. class Person{ /* inv: if mycar!= null then mycar.owner = this. private Car mycar; Person(){; :Car owner: Person :Person mycar: Car /* pre: mycar == null, c.owner = this post: inv */ public void setcar(car c){ mycar = c; The invariant of Person has to take into account two situations, one where the mycar reference is null, and an other where it is not. 12 Notice the precondition of setcar. It assumes that this person does not already own a car, and it assumes that the car c already refers to this person. These two conditions are exactly matched in the call from the method setowner in class Car. In the C++ programming language there is a special encapsulation mechanism called friends. Rather than letting setcar be a public method, we could have declared it to be a friend of Car. This way the method could only be called from Car objects. The friend mechanism is very useful for invariant encapsulation which go across several objects as is the case here.

Pre conditions and exception handling An important property of pre-conditions is to establish under what conditions a method will work. void sort(int[] a) Is it acceptable to all with a null reference? An empty array? An array of length larger than 5.000.000 elements? It is the responsibility of the client to ensure that the method s pre-condition is true. The contract avoids that both the client and the object performs a test if the argument is null. /* pre: a not null, a not empty post: a sorted */ void sort(int[] a){ try{ a extraordinary good sorting algorith goes here catch(exception ex){ if (a == null a.length = 0) throw new PreConditionVoilation(); On this topic a resign for a better explanation. Please read from page 49 and the rest of Bertrand Meyer s paper, the use of exceptions is very nicely described. 13

Java assertions Java has a mechanism called assertions. assert boolean-condition; This statement will throw an exception if the boolean condition is not true. However, you must tickle java a bit to do so. javac -source 1.4 myfile.java to compile it java -enableassertions myfile to execute and actually test the assertions Assert statements can be inserted in the beginning of a method to check preconditions Assert statements can be inserted just before return, to check post conditions Assert statements can be inserted at the end (just before each return) to check that the class invariant is true. Design by contract is supported in Eiffel. Assertions lack: Systematic support for class invariants Support for old variables Support for combination of pre and post conditions when using inheritance Assertions are described in Java precisely. 14 Assertions are much better than nothing! So while I am not impressed with the assert mechanism, it is clearly better than not using it. The three points of critique remains however.

Interfaces An interface I declares a set of methods which a class must implement in order to be of type I. This will make your code more robust to changes in the other code You cannot depend on any specific class You cannot depend on any fields Your code interface Other code Important: The advantage of using interfaces is not for the programmer of Other code, but in your code. Important: It is the programmer of Other code which implement interfaces for you to use. It is typically the programmer of Other code who have designed the interface to be used in Your code. 15

Abstract classes An abstract class is a class which has been designed to be used as a superclass. Often an abstract class implements an interface, but leaves out a few details for you to fill in. Your code still use the interface, rather than your class. The abstract class provides useful implementation of interface your class Your code interface Abstract class Other code As before, the programmer of other code has designed the interface, and also the abstract class. 16

The for loop of Java As of java 5.o is it possible to write loops of the form: for( T v: i) do stuff with v Normally, the for loop is used to go through all elements in a collection, but it is far more general. What the for loop really unfolds to is: Iterator<T> it = i.iterator(); while(it.hasnext()){ v = it.next(); do stuff with v This means that the type of i must have an iterator method. This is ensured by i implementing the Iterable interface. The iterator method returns an iterator which returns elements of type T. To let your own classes work with the for loop, several things need to be in place: You must define an iterator You must create an iterator method that returns an iterator You must impement the Iterable interfrace 17

Reading a file using the for loop for( String s: new IterableReader( areader ) ) System.out.println( s ); class IterableReader implements Iterable<String>{ private Reader reader; IterableReader(Reader r){ reader = r; public Iterator<String> iterator(){ return new MyIterator( reader ); class MyIterator implements Iterator<String>{ // Inv: br is tail & nextlinetobereturned is head private BufferedReader br; private String nextlinetobereturned; private MyIterator(Reader r){ br = new BufferedReader( r ); readaline(); The for loop in the beginning will print out all the lines in areader. private void readaline(){ try{ nextlinetobereturned = br.readline(); catch(ioexception uups){ nextlinetobereturned = null; public boolean hasnext(){ return nextlinetobereturned!= null; public String next(){ String ret = nextlinetobereturned; readaline(); return ret; public void remove(){ throw new UnsupportedOperationException(); 18 The major part of the slide is the class MyIterator, which will iterate each line in the reader it gets as argument to its constructor. The implementation strategy of MyIterator is very common. The problem is the following: With an iterator, you first ask if there are any more elements (using hasnext()), and if there is, you go get it (using next()). With a Reader, you just try to read, and if there were no elements you get a null or -1 response. So, to turn a Reader into an iterator, we have to try to read before we answer if there are any elements. Hence, we need to keep the read line waiting for a call to next(). The private method readaline() is doing the bridging from Reader to Iterator style. It try to read a line, and if successful, stores the line so that it later can be returned by next(). If there is no line to be read, the buffered reader will return null, which in hasnext() is used as the test to see if there is a line to be returned. Notice in particular the next() method. The line to be returned has already been read (using readaline()). Next need to return nextlinetobereturned, and to move to the next line. An auxiliary variable ret is used to hold the return value while we move to the next line.

JML [Slides by Erik Poll, Nijmegen] Formal specification language for Java to specify behaviour of Java classes to record design/implementation decisions by adding annotations (aka assertions) to Java source code, eg preconditions postconditions class invariants as in programming language Eiffel, but more expressive 19

To make JML easy to use: JML (cont d) JML annotations are added as comments in.java file, between /*@... @*/, or after //@. Properties are specified as Java boolean expressions, extended with a few operators \result, \forall, \old, ==>,... and a few keywords requires, ensures, invariant,... Using JML we specify and check properties of the Java program itself, not of some model of our Java program. Ie. the Java program itself is our formal model. 20

Pre and Post Conditions Pre- and post-conditions for methods, eg. /*@ requires amount >= 0; @*/ ensures balance == \old(balance)-amount && \result == balance; public int debit(int amount) {... Here \old(balance) refers to the value of balance before execution of the method. 21

Pre and Post Conditions (cont d) JML specs can be as strong or as weak as you want. /*@ requires amount >= 0; @*/ ensures true; public int debit(int amount) {... This default postcondition ensures true can be omitted. 22

Pre and Post Conditions (cont d) Pre- and postconditions define a contract between a class and its clients: Client must ensure precondition and may assume postcondition Method may assume precondition and must ensure postcondition Eg, in the example spec for debit, it is the obligation of the client to ensure that amount is positive. The requires clause makes this explicit. 23

Signals signals clauses specify when exceptions may be thrown /*@ requires amount >= 0; @*/ ensures true; signals (ISOException e) amount > balance && balance == \old(balance) && e.getreason()==amount_too_big; public int debit(int amount) throws ISOExceptio... 24

Signals (cont d) Again, specs can be as strong or weak as you want. /*@ requires amount >= 0; ensures true; signals (ISOException) true; @*/ public int debit(int amount) throws ISOExceptio NB this specifies that an ISOException is the only exception that can be thrown by debit 25

Signals (cont d) Exceptions mentioned in throws clause are allowed by default, i.e. the default signals clause is signals (Exception) true; To rule them out, add an explicit signals (Exception) false; or use the keyword normal_behavior /*@ normal behavior @*/ requires... ensures... 26

Signals (cont d) There is often a trade-off between precondition and exceptional postcondition /*@ requires amount >= 0 && amount <= balance; @*/ ensures signals true; (ISOException e) false; public int debit(int amount) throws ISOExceptio... Maybe throws ISOException should now be omitted. 27

Invariants Invariants (aka class invariants) are properties that must be maintained by all methods, eg public class Wallet { public static final short MAX_BAL = 1000; private short balance; /*@ invariant 0 <= balance && balance <= MAX_BAL; @*/... 28

Invariants (con td) Invariants document design decisions. private final Object[] objs; /*@ invariant objs!= null && objs.length == CURRENT_OBJS_SIZE && (\forall int i; 0 <= i && i <= CURRENT_OBJS_SIZE ; objs[i]!= null); @*/ Making these design decisions explicit helps in understanding the code. 29

Null Objects Many invariants, pre- and postconditions are about references not being null. non_null is a convenient short-hand for these. public class Directory { private /*@ non null @*/ File[] files; void createsubdir(/*@ non null @*/ String name){... Directory /*@ non null @*/ getparent(){... 30

Assertions An assert clause specifies a property that should hold at some point in the code, eg. if (i <= 0 j < 0) {... else if (j < 5) {... else { //@ assert i > 0 && 0 < j && j < 5; //@ assert i > 0 && j > 5;... 31

Assertions (cont d) JML keyword assert now also in Java (since Java 1.4). Still, assert in JML is more expressive, for example in... for (n = 0; n < a.length; n++) if (a[n]==null) break; /*@ assert (\forall int i; 0 <= i && i < n; a[i]!= null); @*/ 32

Mutable Store Frame properties limit possible side-effects of methods. /*@ requires amount >= 0; @*/ assignable balance; ensures balance == \old(balance)-amount; public int debit(int amount) {... E.g., debit can only assign to the field balance. NB this does not follow from the post-condition. Default assignable clause: assignable \everything. 33

Side Effects A method without side-effects is called pure. public /*@ pure @*/ int getbalance(){... Directory /*@ pure non null @*/ getparent(){... Pure method are implicitly assignable \nothing. Pure methods, and only pure methods, can be used in specifications, eg. //@ invariant 0<=getBalance() && getbalance()<=max_balance 34

JML Summary This covers all you need to know to start using JML! The JML keywords discussed so far: requires ensures signals invariant non null normal behavior assignable pure and JML operators \old, \forall, \exists, \result There are many more features in JML, but these depend on which tool for JML you use. 35

The moral of the story Interfaces and Abstract classes are necessary for the division of labor between A) the programmers of frameworks B) the programmers who use the frameworks The B) programmers need to understand this to use frameworks efficiently Interfaces are the part of contracts that the compiler understands. Contracts are the part you as programmer has to understand. Application programmers will rarely need to define interfaces and abstract classes, nor to define contracts. Application programmers will often need to implement interfaces or subclass abstract classes to integrate their code into the framework. Application programmers need to understand what pre and post conditions are for the methods called. Most programmers are application programmers. 36