Formal Specification of Java Interfaces Jason Hallstrom and Murali Sitaraman Clemson University
Basics An interface Describes what classes or components do Does not describe how they should do it Is a contract between component users (clients) and developers (implementers) If the users satisfy the requirements for using the component, the component will provide guarantees
Principles of Interface Design Information hiding Hide details unnecessary to use the component Abstraction Provide a “cover story” or explanation in user-oriented terms so they can understand the interface
Contract specifications Requirements and guarantees Requires clauses are preconditions Ensures clauses are postconditions Who is responsible for requires clauses? Client (i.e., caller) Implementer Neither Both Discussion of consequences
Contract specifications Requirements and guarantees Requires clauses are preconditions Ensures clauses are postconditions Who is responsible for requires clauses? Client (i.e., caller) Implementer Neither Both Consequences
Specification of Stacks Mathematical modeling What can we think of stacks as “mathematically”?
Mathematical Strings Unlike sets, strings have order Notations Example: Str(Z) for String of integers Notations Empty string (written empty_string or L) Concatenation ( alpha o beta ) Length ( |alpha| ) String containing one entry ( <5> )
Specification of IntStack Interface Suppose IntStack is an interface uses Integer_Theory, String_Theory; Think of stacks of Integers as “math strings” of integers this: Str(Z); Specification of Constructor Initialization ensures this = empty_string; Exercises: Specification of other Stack operations
Specification of IntStack Interface Operation push (int x); updates this; restores x; ensures this = <x> o #this; int Operation pop (); updates this; requires this /= empty_string; ensures #this = <result of pop()> o this; bool Operation is_empty(); preserves this; ensures result of is_empty = (this = empty_string);
Specification of IntStack Interface Operation push (int x); updates this; restores x; ensures this = <x> o #this; int Operation pop (); updates this; requires this /= empty_string; ensures #this = <pop()> o this; bool Operation is_empty(); preserves this; ensures is_empty = (this = empty_string);
Java Specification Questions What is the specification of “=“ to assign one IntStack object to another? If you defined a “clone” method, what is its specification? What are the advantages of using “=“ over “clone”? What are the advantages of using “clone” over equal?