Download presentation
Presentation is loading. Please wait.
Published byHarry Gallagher Modified over 9 years ago
1
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)
2
Abstraction Forget some things i.e. remove some details Combine some things i.e. many-to-one mapping 2
3
Forget some things 3 hiring date
4
Many-to-One Mapping 4 bonus hiring date
5
Abstraction by Parameterization A basic programming technique but an important concept in software engineering Example h*h+w*w replaced with squares(h, w); 5
6
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
7
Specification Example float 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
8
Background Type Inheritance Subtype Supertype Polymorphism and Dynamic Dispatch Boolean logic – and, or, not, boolean variables (x,y,z) Sets – infinite sets, subset
9
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--; }
10
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
11
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
12
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 AbstractListArrayListLinkedListVector void reverseList(List list) {... }
13
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
14
Substitution Principle Signature Rule /done by java Methods Rule /pre n postcon Properties Rule / object states overtime
15
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
16
Signature Rule Example class Object {... public Object clone() {... } } class String {... public String clone() {... } }
17
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
18
Methods Rule Example String 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 x and if x == null, return null
19
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
20
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
21
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
22
Summary Type hierarchies provide extensibility Code can be written in terms of a supertype, yet still work many all subtypes But only when the subtypes follow the substitution principle
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.