Transcript subtype

cs205: engineering software
university of virginia
fall 2006
Subtyping
and
Inheritance
David Evans
www.cs.virginia.edu/cs205
Quiz Friday:
classes through today,
Ch 7 (except 7.9)
Subtyping
Cell
ConwayLifeCell
cs205: engineering software
ConwayLifeCell is a subtype of Cell
Cell is a supertype of ConwayLifeCell
ConwayLifeCell ≤ Cell
In Java API documentation:
java.lang.Object
Cell
ConwayLifeCell
2
Subtype Substitution
• If B is a subtype of A, everywhere
the code expects an A, a B can be
used instead
• Examples: c1 must be a subtype of Cell
Cell c = c1; (note A is a subtype of A)
Cell c = new ConwayLifeCell ();
ConwayLifeCell c = new Cell ();
cs205: engineering software
3
Java’s Type Hierarchy
Object
Cell
String
ConwayLifeCell
cs205: engineering software
Object is the ultimate supertype of
every object type.
4
Java Exception Hierarchy
Object
throws Throwable
Objects thrown
Throwable
must be subtypes
of Throwable
Exception
Subtypes of RuntimeException
are unchecked
RuntimeException
IndexOutOfBoundsException
cs205: engineering software
IOException
FileNotFoundException
5
Inheritance
• To implement a subtype, it is often
useful to use the implementation of
its supertype
• This is also called “subclassing”
both subtyping and inheritance
class B extends A
B is a subtype of A
B inherits from A
just subtyping
class C implements F
C is a subtype of F
No way to get inheritance without subtyping in Java
cs205: engineering software
6
A Type Hierarchy
Shape
Quadrangle
Triangle
Equilateral
Parallelogram
Rhombus
Rectangle
Square
cs205: engineering software
EquilateralTriangle
What are the supertypes of Square?
What are the subtypes of Parallelogram?
7
A Class Hierarchy
Shape
Quadrangle
Triangle
Equilateral
Parallelogram
Rhombus
Rectangle
EquilateralTriangle
Square
cs205: engineering software
8
Reusing Implementations
• Shapes should have a setColor
method
• Change Shape, Quadrangle,
Parallelogram, Triangle, Equilateral,
EquilateralTriangle, Rhombus, Rectangle,
Square, etc.
• Change Shape others inherit new
attribute and method automatically
cs205: engineering software
9
Add isEquilateral method
class Shape {
public bool isEquilateral () {
return false; }
}
class Equilateral {
public bool isEquilateral () {
return true; }
}
cs205: engineering software
10
Is a Rhombus equilateral?
Shape
Quadrangle
isEquilateral () { return false; }
isEquilateral () {
Equilateral
return true;
}
Parallelogram
Rhombus
Inheritance can be
tricky!
isEquilateral?
cs205: engineering software
11
Solutions
• Java
– Allow multiple supertypes using
interfaces, but only one implementation
– Pro: Safe and Simple, Con: Limits Reuse
• C++
– Allow it, let programmers shoot
themselves if they want
• Eiffel
– Explicit renaming or hiding (error if not
done)
cs205: engineering software
12
Java’s Solution: Interfaces
• Define a type with no implementation
• Classes can implement many
interfaces:
class B extends A implements I1, I2, I3 {
…
}
means B is a subtype of A, I1, I2, and I3
B inherits the implementation of A
cs205: engineering software
13
Example Interface
public interface Comparable {
int compareTo (Object o) {
// EFFECTS: Compares this object with the specified
// object for order. Returns a negative integer, zero,
// or a positive integer as this object is less than,
// equal to, or greater than the specified object.
}
}
cs205: engineering software
14
Java’s Sorting Routines
public class java.util.Arrays {
public static void sort (Object[] a, int fromIndex,
int toIndex)
// REQUIRES: All elements in a between
//
fromIndex and toIndex must
//
implement the Comparable interface.
// EFFECTS: Sorts the elements of a between
//
fromIndex and toIndex into ascending
//
order, according to the natural ordering of
//
its elements (defined by compareTo).
cs205: engineering software
15
Charge
• Subtyping
– Allow one type to be used where
another type is expected
• Inheritance
– Reuse implementation of the
supertype to implement a subtype
• Friday, Monday:
– When is it safe to say B is a subtype
of A?
cs205: engineering software
16