Download presentation
Presentation is loading. Please wait.
Published byMildred Dulcie Sharp Modified over 6 years ago
1
Subtyping Rules David Evans cs205: engineering software BlackBear
university of virginia fall 2006 Subtyping Rules Killer Bear Climber KillingBear BlackBear GrizzlyBear David Evans
2
Recap If B is a subtype of A, everywhere an A is expected, a B can be used 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
3
Behavioral Subtyping? Killer Bear What’s the difference between a Black Bear and a Grizzly Bear? Climber 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
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);
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
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
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
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, but can use static methods to create new objects)
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.
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); }
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
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
14
Signature Examples public class Object {
public boolean equals (Object o) ; } public class String extends Object { } public boolean equals (Object o) ;
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
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 must throw the same or fewer exceptions than supertype method.
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); }
18
Substitution Principle
Continues in class 15...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.