Download presentation
Presentation is loading. Please wait.
Published byAron Burns Modified over 9 years ago
1
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)
2
2 cs205: engineering software Subtyping Cell ConwayLifeCell ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell ≤ Cell In Java API documentation: java.lang.Object Cell ConwayLifeCell
3
3 cs205: engineering software Subtype Substitution If B is a subtype of A, everywhere the code expects an A, a B can be used instead Examples: Cell c = c1; c1 must be a subtype of Cell (note A is a subtype of A) Cell c = new ConwayLifeCell (); ConwayLifeCell c = new Cell ();
4
4 cs205: engineering software Java’s Type Hierarchy Object String Cell ConwayLifeCell Object is the ultimate supertype of every object type.
5
5 cs205: engineering software Java Exception Hierarchy Object Throwable Exception RuntimeException IndexOutOfBoundsException IOException FileNotFoundException throws Throwable Objects thrown must be subtypes of Throwable Subtypes of RuntimeException are unchecked
6
6 cs205: engineering software Inheritance To implement a subtype, it is often useful to use the implementation of its supertype This is also called “subclassing” class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F both subtyping and inheritance just subtyping No way to get inheritance without subtyping in Java
7
7 cs205: engineering software A Type Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle What are the subtypes of Parallelogram? What are the supertypes of Square?
8
8 cs205: engineering software A Class Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle
9
9 cs205: engineering software 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
10
10 cs205: engineering software Add isEquilateral method class Shape { public bool isEquilateral () { return false; } } class Equilateral { public bool isEquilateral () { return true; } }
11
11 cs205: engineering software Is a Rhombus equilateral? Shape Quadrangle Parallelogram Rhombus Equilateral isEquilateral? isEquilateral () { return false; } isEquilateral () { return true; } Inheritance can be tricky!
12
12 cs205: engineering software 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)
13
13 cs205: engineering software 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
14
14 cs205: engineering software 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. }
15
15 cs205: engineering software 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).
16
16 cs205: engineering software 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.