Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.

Similar presentations


Presentation on theme: "David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects Herbert Klaeren: Would you agree that the object-oriented concept has superseded the abstract data type idea. And if you were to redo the CLU development right now, would it become and object-oriented language? Barbara Liskov: Well, I guess I have a lot of answers to that question. One is that I believe the most important thing about object-oriented programming is data abstraction. It happens to be expressed in a slightly different form in object-oriented languages, with the notion that the operations belong to the objects rather than to the type. But, I don't believe that's a very important difference. I believe the important idea, grouping object and operations together, is supported by both approaches. On the other hand, I am now designing an object-oriented language and it does have an inheritance mechanism and it does have a type hierarchy mechanism. And I have to say that even today I am not a hundred percent convinced about the utility of these mechanisms. But I'm thinking about it. Question and answer from History of Programming Languages II, 1993.

2 22 March 2001CS 655: Lecture 162 Menu What is Object-Oriented Programming? –Surveys: 38 years of C++ experience, 9 years of Java experience Language Features that support OOP

3 22 March 2001CS 655: Lecture 163 The problem is that there is no distinction between the general properties of any shape and the properties of a specific shape. Expressing this distinction and taking advantage of it defines object-oriented programming. Stroustrup’s Answer: type hierarchy subtype polymorphism? O-O P is expressing and taking advantage of the distinction between the general properties of any shape and the properties of a specific shape.

4 22 March 2001CS 655: Lecture 164 Can we do this in CLU? Shape = cluster [spfc_shape: type] is create, draw where spfc_shape has draw = proctype (s: spfc_shape, x: int, y: int) rep = record [shape: spfc_shape, locx: int, locy: int] create = proc (s: spfc_shape, x: int, y: int)... end create draw = proc (s: cvt) spfc_shape$draw (s.shape, s.x, s.y) end draw end Shape

5 22 March 2001CS 655: Lecture 165 Using Shape st: Shape[Triange] = Shape[Triangle]$create (Triangle$create (3,4,5), 10, 20) sq : Shape[Quadrangle] = Shape[Quadrangle]$create (Quadrangle$create (2,4, 6, 8), 20, 50) st.draw ()% syntactic sugar assumed sq.draw () s := st s := sq st and sq are different types – can’t assign them to the same variable What’s missing?

6 22 March 2001CS 655: Lecture 166 Subtype Polymorphism shape quadrangletriangle Subtype can be used where supertype is expected. Program should still behave as expected if subtype is implemented correctly. ([Liskov & Wing] paper formalizes what this means.) ShapeTriangle supertypesubtype C++: base class derived class Eiffel:parentdescendant Java:Triangle extends Shape. Triangle is-a Shape. Triangle is a subtype of Shape. Triangle  Shape

7 22 March 2001CS 655: Lecture 167 Subsumption Shape s; … s := new Triangle (...);... s := new Quadrangle (...);... B subtype of type A, you can assign B to a reference of type A.

8 22 March 2001CS 655: Lecture 168 Subsumption A E : S, S  T A E : T [subsumption] A E : Triangle, Triangle  Shape A E : Square

9 22 March 2001CS 655: Lecture 169 T  T[reflexive-  ] T 1  T 2 ; T 2  T 3 [transitive-  ] T 1  T 3 T 1  T 2 [struct-  ] struct [..., f: T 1,...]  struct [..., f: T 2,...] (...’s must match) Structs are monotonic. Struct is a subtype if components are subtypes. (Often called covariant.) Subtyping Rules

10 22 March 2001CS 655: Lecture 1610 Arrays T 1  T 2 [unsound-array-  ] array[T 1 ]  array [T 2 ] as : array[Shape]; at : array[Triangle];... as := at; as[2] := new Quadrangle (2, 3, 2, 6); Triangle t := at[2]; Are we sure about the struct rule’s soundness?

11 22 March 2001CS 655: Lecture 1611 Procedures Triangle  Shape fss = proc (Shape) returns (Shape) fts= proc (Triangle) returns (Shape) fst = proc (Shape) returns (Triangle) ftt = proc (Triangle) returns (Triangle) fts  fss?fst  fss? ftt  fss?fss  fts?

12 22 March 2001CS 655: Lecture 1612 How to decide? s: Shape; t: Triangle s := fss (s);t := fst (s); t := fts (t); s := fts (s);t := fss (s); t := fss (s); s := fst (s);t := ftt (s); s := ftt (s);

13 22 March 2001CS 655: Lecture 1613 Procedures fss = proc (Shape) returns (Shape) fts = proc (Triangle) returns (Shape) fst = proc (Shape) returns (Triangle) ftt = proc (Triangle) returns (Triangle) fts  fssfst  fss ftt  fssfss  fts Result can be more specific (monotonic = covariant) Parameters can be less specific (anti-monotonic = contravariant)

14 22 March 2001CS 655: Lecture 1614 Procedure Calls S S  T T1T1  S1 S1 TnTn  Sn Sn TrTr S r  anti-monotonic on parameters monotonic on results

15 22 March 2001CS 655: Lecture 1615 What is Object-Oriented Programming? Programmer can define subtype relationships Typing rules that allow subtype to be used in place of supertype (subtype polymorphism) Type-directed method dispatch Implementation sharing (inheritance)

16 22 March 2001CS 655: Lecture 1616 Type-Directed Method Dispatch s: Shape := new Triangle (3, 4, 5); s.draw (); Static Dispatch: Calls shape draw method Dynamic Dispatch: Calls triangle draw method

17 22 March 2001CS 655: Lecture 1617 Dispatching Solutions C++ –Supertype declares methods virtual to allow overriding Java –Everything is overridable, unless supertype declared it final Eiffel –Subtype uses explicit redefine clause Which supports reuse best? Which is safest?

18 22 March 2001CS 655: Lecture 1618 Method Binding s : Shape := new Triangle (3, 4, 5); h : int := s.getHypotenuse (); s := new Quadrangle (2, 4, 3, 8); h : int := s.getHypotenuse (); // no method Static Checking: Both are compile-time errors. Dynamic Checking: First is okay, second is run-time error. C++, Java, Eiffel Smalltalk, Dylan

19 22 March 2001CS 655: Lecture 1619 Talk Today at 3:30 Luiga Luca Cavalli-Sforza Languages and Genes Newcomb Hall Theater

20 22 March 2001CS 655: Lecture 1620 Charge Read Wing & Liskov paper: what must be true about S and T to make S  T safe? Either submit a project proposal on Tuesday, or contact me by Monday if you are still struggling to find a good topic


Download ppt "David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects."

Similar presentations


Ads by Google