Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Advertisements

Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Generic programming in Java
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
Utilities (Part 3) Implementing static features 1.
5/17/2015 OO Design: Liskov Substitution Principle 1.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Class 14: Object-Oriented Programming Fall 2010 University of Virginia David Evans cs2220: Engineering Software.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Fall 2010 UVa David Evans cs2220: Engineering Software Class 27: Exam 2.
Effective Java, Chapter 7: Methods Items Last modified Fall 2012 Paul Ammann.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Design David Evans cs205: engineering software
Component Based Software Engineering
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
EECE 310: Software Engineering
Type Abstraction SWE Spring 2009.
ATS Application Programming: Java Programming
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Type Abstraction Liskov, Chapter 7.
Generic programming in Java
Data Abstraction David Evans cs205: engineering software
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
EECE 310: Software Engineering
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
Lecture 13: Subtyping Rules Killer Bear Climber
1.
Type Abstraction SWE Spring 2013.
Presentation transcript:

cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans

static public boolean moreElements(ArrayList a, ArrayList b) // REQUIRES: a and b are not null // MODIFIES: nothing // EFFECTS: Returns true iff a has more elements than b. { return a.size() > b.size(); } Is this correct?

static public boolean moreElements(ArrayList a, ArrayList b) // REQUIRES: a and b are not null // MODIFIES: nothing // EFFECTS: Returns true iff a has more elements than b. { return a.size() > b.size(); } public static void main(String[] args) { // TODO Auto-generated method stub ArrayList a; ArrayList b; … a.add("Hello"); b.add("Ciao"); b.add("Goodbye"); System.out.println("More elements: " + moreElements(a, b)); }

Dangers of Subtyping public class SillyList extends ArrayList public int size() // REQUIRES: The alert level has reached DEFCON 5, all the missiles have // been targeted, and the President has issued a verified launch // command. // MODIFIES: Everything // EFFECTS: Launches the missiles. Returns the expected number of // elements in the object after all the computer’s memory has been // destroyed by radiation. { launchMissiles(); return 0; } public static void main(String[] args) { ArrayList a = new SillyList (); ArrayList b = new SillyList (); a.add("Hello"); b.add("Ciao"); b.add("Goodbye"); System.out.println("More elements: " + moreElements(a, b)); }

Reasoning about programs that can use unfettered subtyping is hopeless!

How can we solve this? static public String pasteTogether(String a, String b) // REQUIRES: a and b are not null // EFFECTS: Returns a String that is a followed by b. { return a.concat(b); } Could pasteTogether launch the missiles? public final class String extends Object implements Serializable, Comparable, CharSequence { … } public final class String extends Object implements Serializable, Comparable, CharSequence { … }

Reasoning with Subtyping Easy approach #1: don’t allow subtyping! Make all classes final (like java.lang.String) Easy approach #2: give up on reasoning Reason based on the apparent type specification and don’t make any claims about what happens with subtypes. Hard approach: impose constraints on subtypes to allow reasoning Substitution principle

How do we know if saying B is a subtype of A is safe? Substitution Principle: If B is a subtype of A, everywhere the code expects an A, a B can be used instead and the program still satisfies its specification

Subtype Condition 1: Signature Rule We can use a subtype method where a supertype methods is expected: – Subtype must implement all of the supertype methods – Argument types must not be more restrictive – Result type must be at least as restrictive – Subtype method must not throw exceptions that are not subtypes of exceptions thrown by supertype

Signature Rule class A { public R A m (P A p) ; } class B extends A { public R B m (P B p) ; } R B must be a subtype of R A : R B <= R A P B must be a supertype of P A : P B >= P A covariant for results, contravariant for parameters

Subtype Condition 2: Methods Rule Precondition of the subtype method must be weaker than the precondition of the supertype method. m A.pre  m B.pre Postcondition of the subtype method must be stronger than the postcondition of the supertype method. m B.post  m A.post

Subtype Condition 3: Properties Subtypes must preserve all properties described in the overview specification of the supertype.

Properties Example public class StringSet // Overview: An immutable set of Strings. public class MutStringSet extends StringSet // Overview: A mutable set of Strings. MutStringSet cannot be a subtype of StringSet, since it does not satisfy property that once a StringSet object is created its value never changes. Would it be okay for a subtype of a mutable type to be immutable?

Properties Example public class ImmutableStringSet extends MutStringSet // Overview: An immutable set of Strings. public class MutStringSet // Overview: A mutable set of Strings. ImmtableStringSet could be a subtype of MutStringSet according to the properties rule....but would be very difficult to satisfy the methods rule!

Substitution Principle Summary Signatures: subtype methods must be type correct in supertype callsites: result is a subtype (covariant), parameters are supertypes (contravariant) Methods: subtype preconditions must be weaker than supertype preconditions (covariant); subtype postconditions must be stronger than supertype postconditions (contravariant) Properties: subtype must preserve all properties specified in supertype overview

Substitution Principle Summary Param Types Psub  Psuper Preconditions pre_sub  pre_super Result TypeRsub  Rsuper Postconditions post_sub  post_super Propertiesproperties_sub  properties_super contravariant for inputs covariant for outputs These properties ensure code that is correct using an object of supertype is correct using an object of subtype.

Substitution Mystery …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; …(anything could be here) mt1 = mt2.m (mt3); If the Java compiler accepts this code, which of these are guaranteed to be true: a.The apparent type of mt2 is MysteryType2 b.At the last statement, the actual type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the actual type of mt1 is MysteryType1 If the Java compiler accepts this code, which of these are guaranteed to be true: a.The apparent type of mt2 is MysteryType2 b.At the last statement, the actual type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the actual type of mt1 is MysteryType1