1 abstraction identify important aspects and ignore the details permeates software development...
TRANSCRIPT
1
Abstraction
Identify important aspects and ignore the details
Permeates software development programming languages are abstractions
built on hardware programs are (often) abstractions of
some real process (e.g., accounting system)
Abstraction
Forget some things i.e. remove some details
Combine some things i.e. many-to-one mapping
2
Abstraction by Parameterization A basic programming technique but
an important concept in software engineering
Exampleh*h+w*w
replaced withsquares(h, w);
5
Abstraction by Specification Abstract from implementation details
to behaviour client can depend on
Precondition: Something assumed to be true on entry to the
method Postcondition:
Something that is supposed to be true at the completion of the method
6
Specification Examplefloat sqrt(float coef) {//PRECONDITION: coef > 0//EFFECTS: Returns approximate square root of coef
float ans = coef/2.0;int i= 1;while (i<7) {
ans = ans – ((ans*ans-coef)/(2.0*ans));i = i + 1;
}return ans;
}}7
Background
• Type• Inheritance• Subtype• Supertype• Polymorphism and Dynamic Dispatch• Boolean logic
– and, or, not, boolean variables (x,y,z)
• Sets– infinite sets, subset
Variables• Definition: A name bound to an object
– Allow programmers to manipulate objects– They are like a “box” where you can store objects
• Variables are not objects!• new statements are not objects!• Objects do not appear in the source code!
while(x > 0) {myString = new String(“string” + x);println(myString);x--;
}
Types as Sets• We can understand certain aspects of
subtyping by considering types as sets• A type can be considered as a set
consisting of all possible objects of that type
• In this way, a subtype is always a subset of a supertype
• We say a subtype is narrower and more specific than a supertype
• We say a supertype is wider and less specific
Declared Type and Runtime Type• Each variable has a declared type
ex. String str; – The declared type is like a label on the side of a
box saying what could be inside the box• Each object has a runtime type
– The runtime type is the actual type of the object
– The runtime type of an object never changes– The runtime type is the type of the new
statement which created the object– Dynamic dispatch: Methods to be invoked are
determined by runtime type of target
Reasoning using Declared Types• You always know the declared type of a variable• You don’t always know the runtime type of an object
bound to a variable• Allows you to write methods which abstract from the
details of all possible subtypes
• Subtypes of List:– AbstractList, ArrayList, LinkedList, Vector
void reverseList(List list) {...
}
Substitution Principle
• Subtypes provide different behavior when they override supertype methods
However!
• Subtypes must not break the interface specified by a supertype/(pre n postcon)– So programmers can write code and reason
just using the supertype specification
Substitution Principle
Signature Rule /done by java Methods Rule /pre n postcon Properties Rule / object states
overtime
Signature Rule• Require that methods overridden by a
subtype are compatible with the supertype method
• Compatibility depends on the language you are using
• Let’s consider Java– Arguments of overridden methods must
have the same type– The return type of an override can be a
subtype of the return type of the overridden method
Signature Rule Exampleclass Object {
...public Object clone() { ... }
}
class String {...public String clone() { ... }
}
Methods Rule• A subtype can weaken the precondition
– A subtype can require less• A subtype can strengthen the
postcondition– A subtype can guarantee more
• Corresponds to the logical operators OR and AND
ex.(x AND y) is a stronger statement than just x(x OR y) is a weaker statement than just x
Methods Rule ExampleString toUpper(String str);//PRE: x != null//POST: returns an upper case version of x
A subtype could provide this specification for toUpper
//PRE: no preconditions//POST: if x != null, return an upper case
version of xand if x == null, return null
Properties Rule
So far we have only considered methods
Sometimes the state of objects needs to follow an invariant
Invariant: A fact which is always true about the state of an object
Object state: A fact which can be concluded based on the current values of the object’s fields
Properties Rule
Example: Set invariant: Sets cannot contain duplicate
elements We should be able to conclude that no
subtypes of Set will contain duplicate elements
Notice that the invariant has consequences on the implementation of several methods
We say the invariant crosscuts the methods
Properties Rule
Invariants must hold after execution of the constructor
Invariants must hold after the execution of each method
An invariant may be violated during the execution of a method, so long as the object is not executable by another thread
Example: on the board