Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13: Subtyping Rules Killer Bear Climber

Similar presentations


Presentation on theme: "Lecture 13: Subtyping Rules Killer Bear Climber"— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans
Lecture 13: Subtyping Rules Killer Bear Climber What’s the difference between a Black Bear and a Grizzly Bear? KillingBear 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?) BlackBear GrizzlyBear CS201j: Engineering Software University of Virginia Computer Science David Evans

2 Announcements Exam 1 out at end of class today
Open book, open notes Work alone, don’t use Java compiler If I have any reasons to suspect the honor policy is not followed on this exam, you will have a closed book final during Exam week No section meetings tomorrow 16 October 2003 CS 201J Fall 2003

3 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 16 October 2003 CS 201J Fall 2003

4 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); 16 October 2003 CS 201J Fall 2003

5 Overloading and Overriding
Overriding: replacing a supertype’s method in a subtype Dynamic dispatch finds method of actual type Overloading: providing two methods with the same name but different parameter types Statically select most specific matching method of apparent type 16 October 2003 CS 201J Fall 2003

6 Overloading Example public class Overloaded extends Object {
public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; public boolean equals (String s) { return true; public boolean equals (Object) is inherited from Object 16 October 2003 CS 201J Fall 2003

7 Overloading 17 23 true false static public void main (String args[]) {
public class Overloaded { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; public boolean equals (String s) { return true; Overloading static public void main (String args[]) { Overloaded over = new Overloaded (); System.err.println (over.tryMe (over)); System.err.println (over.tryMe (new String ("test"))); Object obj = new String ("test"); System.err.println (over.tryMe (obj)); System.err.println (over.equals (new String ("test"))); System.err.println (over.equals (obj)); Object obj2 = over; System.err.println (obj2.equals (new String ("test"))); } 17 23 true false 16 October 2003 CS 201J Fall 2003

8 Overkill Overloading and overriding together can be overwhelming!
Avoid overloading whenever possible: names are cheap and plentiful One place you can’t easily avoid it: constructors (they all have to have the same name) 16 October 2003 CS 201J Fall 2003

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

10 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. 16 October 2003 CS 201J Fall 2003

11 What needs to be true? public int f (a A, x X) { return a.m (x); }
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); } 16 October 2003 CS 201J Fall 2003

12 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 16 October 2003 CS 201J Fall 2003

13 Signature Rule class A { public RA m (PA p) ; } class B extends A {
public RB m (PB p) ; } RB must be a subtype of RA: RB <= RA PB must be a supertype of PA: PB >= PA covariant for results, contravariant for parameters 16 October 2003 CS 201J Fall 2003

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

15 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 subtype of Cell. 16 October 2003 CS 201J Fall 2003

16 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. 16 October 2003 CS 201J Fall 2003

17 What needs to be true? public int f (a A, x X) {
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); } 16 October 2003 CS 201J Fall 2003

18 Subtype Condition 2: Methods Rule
Precondition of the subtype method must be weaker than the precondition of the supertype method. mA.pre  mB.pre Postcondition of the subtype method must be stronger than the postcondition of the supertype method. mB.post  mA.post 16 October 2003 CS 201J Fall 2003

19 Methods Rule Example public class A {
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. 16 October 2003 CS 201J Fall 2003

20 Methods Rule Example public class A {
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 } 16 October 2003 CS 201J Fall 2003

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

22 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 16 October 2003 CS 201J Fall 2003

23 Substitution Principle
… (in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … (anything could be here) mt1 = mt2.m (mt3); If the Java compiler is happy with this code, which of these are guaranteed to be true: The apparent type of mt2 is MysteryType2 At the last statement, the actual type of mt2 is MysteryType2 MysteryType2 has a method named m The MysteryType2.m method takes a parameter of type MysteryType3 The MysteryType2.m method returns a subtype of MysteryType1 After the last statement, the actual type of mt1 is MysteryType1 16 October 2003 CS 201J Fall 2003

24 … (anything could be here) mt1 = mt2.m (mt3);
… (in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … (anything could be here) mt1 = mt2.m (mt3); If the Java compiler is happy with this code, which of these are guaranteed to be true: The apparent type of mt2 is MysteryType2 At the last statement, the actual type of mt2 is MysteryType2 MysteryType2 has a method named m The MysteryType2.m method takes a parameter of type MysteryType3 The MysteryType2.m method returns a subtype of MysteryType1 After the last statement, the actual type of mt1 is MysteryType1 TRUE: the apparent type is obvious from the declaration. FALSE: we only know the actual type <= MysteryType2 TRUE FALSE: we only know it takes a parameter >= MysteryType3 TRUE: the assignment type checking depends on this FALSE: we only know that the actual type <= MysteryType1 16 October 2003 CS 201J Fall 2003

25 Subtyping Rules class A { public RA m (PA p) ; }
… (in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; mt1 = mt2.m (mt3); class A { public RA m (PA p) ; } If A is MysteryType2, what do we know about RA and PA? RA must be a subtype of MysteryType1: RA <= MysteryType1 MysteryType3 must be a subtype of PA: PA >= MysteryType3 16 October 2003 CS 201J Fall 2003

26 Subtyping Rules class A { public RA m (PA p) ; } class B extends A {
public RB m (PB a); … (in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; mt1 = mt2.m (mt3); If B <= A, what do we know about RB and PB? RB must be a subtype of RA: RB <= RA PA must be a subtype of PB: PB >= PA 16 October 2003 CS 201J Fall 2003

27 Substitution Principle
class A { public RA m (PA p) ; } class B extends A { public RB m (PB a); … (in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; mt1 = mt2.m (mt3); Substitution Principle: Parameters PB >= PA Preconditions pre_A  pre_B Result RB <= RA Postconditions post_B  post_A 16 October 2003 CS 201J Fall 2003

28 Substitution Principle
Is this the only way? No…Tuesday we’ll look at an alternative. 16 October 2003 CS 201J Fall 2003


Download ppt "Lecture 13: Subtyping Rules Killer Bear Climber"

Similar presentations


Ads by Google