Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.

Similar presentations


Presentation on theme: "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping

2 24 September 2002CS 201J Fall 20022 Menu Subtyping Inheritance What is Object-Oriented Programming

3 24 September 2002CS 201J Fall 20023 Subtyping Cell ConwayLifeCell ConwayLifeCell is a subtype of Cell Cell is a supertype of ConwayLifeCell ConwayLifeCell ≤ Cell

4 24 September 2002CS 201J Fall 20024 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 ();

5 24 September 2002CS 201J Fall 20025 Subtype Examples java.util.Vector: public void addElement (Object obj); public class SpeciesSet { Vector elements; public void insert (Species s) { elements.addElement (s); } Why we can use a Species where an Object is expected?

6 24 September 2002CS 201J Fall 20026 Java’s Type Hierarchy Object Species Cell ConwayLifeCell Object is the ultimate supertype of every object type.

7 24 September 2002CS 201J Fall 20027 Java 3D Class Hierarchy Diagram http://java.sun.com/products/java-media/3D/collateral/j3dclass.html RotationPathInterpolator PathInterpolator Interpolator Selector Node Leaf SceneGraphObject Not at all uncommon to have class hierarchies like this!

8 24 September 2002CS 201J Fall 20028 Subtype Examples java.util.Vector: public void addElement (Object obj); public class IntSet { Vector elements; public void insert (int x) { elements.addElement (x); } Primitive types are not subtypes of Object. elements.addElement (new Integer (x)); But Integer is…

9 24 September 2002CS 201J Fall 20029 Inheritance To implement a subtype, it is often useful to use the implementation of its supertype This is also called “subclassing” In Java: 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

10 24 September 2002CS 201J Fall 200210 Inheritance and Subtyping public class ExtremeLifeCell extends Cell { public CellState getNextState () // EFFECTS: Returns the next state for this cell. // The next state will be alive if this cell or any of its neighbors // is currently alive. { if (countAliveNeighbors () > 0) { return CellState.createAlive (); } else { return getState (); } ExtremeLifeCell is a subtype of Cell - anywhere a Cell is expected, we can use an ExtremeLifeCell ExtremeLifeCell inherits from Cell - the rep of an ExtremeLifeCell includes the rep of Cell - all public methods and constructors of Cell are also available for ExtremeLifeCell

11 24 September 2002CS 201J Fall 200211 Method Dispatch B is a subtype of A If both A and B have a method display which method should be called? A a = new A (); B b = new B (); a.display (); b.display (); a = b; a.display () Calls class A’s display method Calls class B’s display method

12 24 September 2002CS 201J Fall 200212 Dynamic Dispatch Search for the method up the type hierarchy, starting from the actual (dynamic) type of the object A B A a = new A (); B b = new B (); a.display (); b.display (); a apparent type actual type b apparent type actual type

13 24 September 2002CS 201J Fall 200213 Dynamic Dispatch Search for the method up the type hierarchy, starting from the actual (dynamic) type of the object A B A a = new A (); B b = new B (); a.display (); b.display (); a = b; a apparent type actual type b apparent type actual type Now: apparent type of a is A, actual type of a is B

14 24 September 2002CS 201J Fall 200214 public class Grid { /*@non_null@*/ Cell [][] cells; … public void step () // MODIFIES: this // EFFECTS: Executes one step for each cell in the grid. { CellState [][] nextStates = new CellState [rows][columns]; // Since we need to update all cells synchronously, we first calculate the next // state for each cell, and store it in a temporary array. Then, we update all cells. for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { nextStates [i][j] = cells[i][j].getNextState (); } } for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cells[i][j].setState (nextStates[i][j]); } } } } } apparent type: Cell actual type: any subtype of Cell (could be ConwayLifeCell )

15 24 September 2002CS 201J Fall 200215 Apparent and Actual Types Apparent types are associated with declarations: they never change Actual types are associated with object: they are always a subtype of the apparent type Compiler does type checking using apparent type Virtual Machine does method dispatch using actual type

16 24 September 2002CS 201J Fall 200216 Downcasting java.util.Vector: public Object elementAt (int i); public class StringSet { Vector elements; public String choose () { String s = elements.elementAt (0); return s; } String s = (String) elements.elementAt (0); Casting changes the apparent type. The VM must check that the actual type is a subtype of the cast type.

17 24 September 2002CS 201J Fall 200217 A Type Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle What are the subtypes of Parallelogram? What are the supertypes of Square?

18 24 September 2002CS 201J Fall 200218 A Class Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle

19 24 September 2002CS 201J Fall 200219 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

20 24 September 2002CS 201J Fall 200220 Add isEquilateral method class Shape { public bool isEquilateral () { return false; } } class Equilateral { public bool isEquilateral () { return true; } }

21 24 September 2002CS 201J Fall 200221 Is a Rhombus equilateral? Shape Quadrangle Parallelogram Rhombus Equilateral isEquilateral? isEquilateral () { return false; } isEquilateral () { return true; } Inheritance can be tricky!

22 24 September 2002CS 201J Fall 200222 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)

23 24 September 2002CS 201J Fall 200223 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

24 24 September 2002CS 201J Fall 200224 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. }

25 24 September 2002CS 201J Fall 200225 Object- Oriented Programming

26 24 September 2002CS 201J Fall 200226 What is an Object? Packaging state and procedures –state: the rep What a thing is –procedures: methods and constructors What you can do with it

27 24 September 2002CS 201J Fall 200227 What is Object-Oriented Programming?

28 24 September 2002CS 201J Fall 200228 “Object-oriented programming is programming with inheritance. Data abstraction is programming using user-defined types. With few exceptions, object-oriented programming can and ought to be a superset of data abstraction. These techniques need proper support to be effective. Data abstraction primarily needs support in the form of language features and object-oriented programming needs further support from a programming environment. To be general purpose, a language supporting data abstraction or object-oriented programming must enable effective use of traditional hardware.” Bjarne Stroustrup’s Answer

29 24 September 2002CS 201J Fall 200229 “I invented the term Object-Oriented and I can tell you I did not have C++ in mind.” Alan Kay

30 24 September 2002CS 201J Fall 200230 Programming Language History Before 1954: twidling knobs, machine code, assembly code FORTRAN (John Backus, UVa dropout, 1954) – Formula Translation Algol (Peter Naur, Alan Perlis, et. al., 1958-1960) –Most influential programming language –Many of the things Algol did first (types, while, blocks) are in Java

31 24 September 2002CS 201J Fall 200231 Programming Language History Simula (Dahl and Nygaard, 1962-7) –First language with subtyping and inheritance CLU (Liskov et. al., 1970s) –First language with good support for data abstraction (but no subtyping or inheritance) Smalltalk (Kay et. al., 1970s) –First successful language and programming system to support subtyping and inheritance

32 24 September 2002CS 201J Fall 200232 Object-Oriented Programming Object-Oriented Programming is a state of mind where you program by thinking about objects It is difficult to reach that state of mind if your language doesn’t have: –Mechanisms for packaging state and procedures Java has class –Subtyping Java has extends (subtype and subclass) and implements (subtype) Other things can help: dynamic dispatch, implementation inheritance, automatic memory management, mixins, good Indian food, Krispy Kremes, etc.

33 24 September 2002CS 201J Fall 200233 Who was the first object-oriented programmer?

34 24 September 2002CS 201J Fall 200234 By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent. Ada, Countess of Lovelace, around 1830

35 24 September 2002CS 201J Fall 200235 Charge Exam 1: out Thursday 10 Oct, due Tuesday 15 Oct Bring your questions to Thursday’s class Week after: –When is it safe to say B is a subtype of A? Enjoy your reading vacation!


Download ppt "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping."

Similar presentations


Ads by Google