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

Slides:



Advertisements
Similar presentations
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Advertisements

Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
1 COMP313A Programming Languages Object Oriented Progamming Languages (1)
Principles of Object-Oriented Software Development The language Eiffel.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 22: Object-Oriented Programming COMP 144 Programming Language Concepts Spring 2002.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
C# F 1 CSC 298 Object Oriented Programming (Part 1)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Chapter 12 Support for Object oriented Programming.
Types in programming languages1 What are types, and why do we need them?
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
1 COMP313A Programming Languages Object Oriented Progamming Languages (2)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
ISBN Object-Oriented Programming Chapter Chapter
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Peyman Dodangeh Sharif University of Technology Fall 2014.
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSE 341 Section 10 Subtyping, Review, and The Future.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 David Evans Object-Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Modern Programming Tools And Techniques-I
Polymorphism.
Types of Programming Languages
Lecture 8: SmallTalking about Objects
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 10: Using Object-Oriented Languages
Lecture 13: Subtyping Rules Killer Bear Climber
Lecture 6: Polymorphism
Presentation transcript:

David 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.

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

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.

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

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?

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

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.

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

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

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?

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?

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);

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)

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

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)

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

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?

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

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

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