David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.

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.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
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 16: Concurrent Programming.
5/17/2015 OO Design: Liskov Substitution Principle 1.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 18: Behavioral Subtyping and.
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.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
1 cs205: engineering software university of virginia fall 2006 Avoiding Software Disasters.
Types in programming languages1 What are types, and why do we need them?
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Subtype Polymorphism, Subtyping vs
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.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
CSCI-383 Object-Oriented Programming & Design Lecture 24.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
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.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
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 by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Type Abstraction SWE Spring 2009.
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurring Concurrently
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Lecture 9: Exceptions in Java CS201j: Engineering Software
Type Abstraction Liskov, Chapter 7.
Data Abstraction David Evans cs205: engineering software
Lecture 4: Data Abstraction CS201j: Engineering Software
Lecture 10: Using Object-Oriented Languages
CS2013 Lecture 7 John Hurley Cal State LA.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Lecture 9: When is S  T safe? Killer Bear
Type Abstraction SWE Spring 2013.
Presentation transcript:

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the difference between a Black Bear and a Grizzly Bear? When you climb up the tree, the Black Bear climbs up after you. The Grizzly Bear knocks down the tree. (Which is the behavioral subtype?) Killer BlackBearGrizzlyBear Climber Bear KillingBear

15 October 2002CS 201J Fall Recap If B is a subtype of A, everywhere the code expects an A, a B can be used instead To implement a subtype, it is often useful to use the implementation of its supertype class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F

15 October 2002CS 201J Fall Subtyping Example public class Bear { public Bear () { } abstract public boolean chaseHuman (Human h); public void eatHuman (Human h) { if (chaseHuman (h)) say (“Yum Yum!”); } public void say (String s) { System.err.println (“Bear says: ” + s); } public class BlackBear extends Bear { public boolean chaseHuman (Human h) { // follow h and climb the tree return true; } public void say (String s) { System.err.println (“BlackBear says: ” + s); } BlackBear b = new BlackBear (); b.eatHuman (h);

How do we know if saying B is a subtype of A is safe?

15 October 2002CS 201J Fall Substitution Principle If B is a subtype of A, everywhere the code expects an A, a B can be used instead For a function f (A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B.

15 October 2002CS 201J Fall What needs to be true? For a function f (a A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B. public int f (a A, x X) { return a.m (x); }

15 October 2002CS 201J Fall 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

15 October 2002CS 201J Fall 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

15 October 2002CS 201J Fall Signature Examples public class Object { public boolean equals (Object o) ; } public class String extends Object { } public boolean equals (Object o) ;

15 October 2002CS 201J Fall Signature Examples public class CellSet { // A set of Cell’s. public Cell choose () throws NoSuchElementException ; } public class ConwayCellSet extends CellSet { // A set of ConwayCell (<= Cell) objects. public ConwayCell choose () } public Cell choose () ; Return type must be a supertype.

15 October 2002CS 201J Fall Java’s Rule Java compiler is stricter than this: doesn’t allow any variation in types (“novariant”): –Overriding method must have same return and parameter types –Overriding method can throw fewer exceptions.

15 October 2002CS 201J Fall What needs to be true? For a function f (a A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B. public int f (a A, x X) { // REQUIRES: a is initialized // EFFECTS: returns a.value * x.value return a.m (x); }

15 October 2002CS 201J Fall 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 2: Methods Rule

15 October 2002CS 201J Fall Methods Rule Example public int f (a A, x X) { // REQUIRES: a is initialized // EFFECTS: returns a.value * x.value return a.m (x); } public class A { // An A may be initialized or uninitialized. // An initialized A has an associated int value. public int m (x X) { // REQUIRES: this is initialized } public class B extends A { // A B may be initialized or uninitialized. // A B may be awake or asleep. // An initialized B has an associated int value. public int m (x X) { // REQUIRES: this is initialized and awake } Can’t make the precondition stronger! The callsite might not satisfy it.

15 October 2002CS 201J Fall Methods Rule Example public int f (a A, x X) { // REQUIRES: a is initialized // EFFECTS: returns a.value * x.value return a.m (x); } public class A { // An A may be initialized or uninitialized. // An initialized A has an associated int value. public int m (x X) { // REQUIRES: this is initialized } public class B extends A { // A B may be initialized or uninitialized. // A B may be awake or asleep. // An initialized B has an associated int value. public int m (x X) { // REQUIRES: nothing }

15 October 2002CS 201J Fall Subtypes must preserve all properties described in the overview specification of the supertype. Subtype Condition 3: Properties

15 October 2002CS 201J Fall Substitution Principle Is this the only way?

15 October 2002CS 201J Fall 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

15 October 2002CS 201J Fall Eiffel’s Rules (Described in Bertrand Meyer paper out today)

15 October 2002CS 201J Fall Eiffel Rules Skier set_roommate (Skier) Boy Girl The types of the parameters in the subtype method may be subtypes of the supertype parameters. How can Girl override set_roomate? set_roommate (Girl g) set_roommate (Boy b) Opposite of substitution principle!

15 October 2002CS 201J Fall Can’t Get Up? s: skier; g: girl; b: boy; s := g;... s.set_roommate (b); Skier set_roommate (Skier) Boy Girl set_roomate (Girl)

15 October 2002CS 201J Fall Meyer’s Excuse Strangely enough, some workers in the field have been advocating a contravariant policy. Here it would mean that if we go for example to class RANKED_GIRL, where the result of roommate is naturally redefined to be of type RANKED_GIRL, we may for the argument of routine share use type GIRL, or rather scaringly, SKIER of the most general kind. One type that is never permitted in this case is RANKED_GIRL! Here is what, under various mathematical excuses, some professors have been promoting. No wonder teenage pregnancies are on the rise.

15 October 2002CS 201J Fall PS5 First part (due Thursday Oct 24): –Questions about subtyping rules Which rules make most sense? Which rules are most useful for real programs? –Exercises to learn about concurrency (Thursday) –Design a distributed simulation of something interesting Second part (due Halloween): –Implement your distributed simulation

15 October 2002CS 201J Fall Charge Must it be assumed that because we are engineers beauty is not our concern, and that while we make our constructions robust and durable we do not also strive to make them elegant? Is it not true that the genuine conditions of strength always comply with the secret conditions of harmony? Gustav Eiffel