David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Software Engineering and Design Principles Chapter 1.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Object-Oriented Analysis and Design Lecture 8 State Space, Behavior, and Type Conformance.
Chapter 10 Classes Continued
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Comparison of OO Programming Languages © Jason Voegele, 2003.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 18: Behavioral Subtyping and.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
OO as a language for acm l OO phrase l Mental model of key concepts.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Reusing Classes composition The trick is to use the classes without soiling the existing code. In this chapter you’ll see two ways to accomplish.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Cs205: engineering software university of virginia fall 2006 David Evans Object-Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Modern Programming Tools And Techniques-I
Component Based Software Engineering
Lecture 8: SmallTalking about Objects
Subtyping Rules David Evans cs205: engineering software BlackBear
Type Abstraction Liskov, Chapter 7.
Verified Subtyping with Traits and Mixins
Lecture 10: Using Object-Oriented Languages
Lecture 13: Subtyping Rules Killer Bear Climber
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Lecture 9: When is S  T safe? Killer Bear
Presentation transcript:

David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral Subtyping (when is S  T safe?) What’s the difference between a Black Bear and a Grizzly Bear? When you climb up the tree, the Grizzly climbs up after you. The Black Bear knocks down the tree. (Which is the behavioral subtype?) Killer BlackBearGrizzlyBear Climber Bear KillingBear

27 March 2001CS 655: Lecture 172 Menu Wrap-up “What is Object-Oriented Programming?” Behavioral Notion of Subtyping

27 March 2001CS 655: Lecture 173 Last time Defined subtyping as subsumption Showed typing judgments that support subtype polymorphism Some language features that support subtype polymorphism: –Dynamic type-directed method dispatch –Subclassing (Implementation inheritance)

27 March 2001CS 655: Lecture 174 Implementation Reuse: Subclassing, Inheritance Use implementation of one type to implement another type Often use implementation of supertype to implement subtype Commonly used OO languages confuse issue by combining subtyping and inheritance: –Eiffel – cannot separate –Java – cannot separate, can use interfaces for subtyping only –C++ - can use implementation inheritance without subtyping ( private, protected inheritance)

27 March 2001CS 655: Lecture 175 Language Principle: Getting Defaults Right Matters Shouldn’t require extra work to hide things, should require extra work to expose them (forgetting something should be safer) Possible Examples: –Algol60: call-by-value requires extra work (should have been call-by-name) –Java: preventing overriding requires extra work ( final ) / opposite of C++ –C++: preventing subtyping requires extra work ( public inheritance is default, need private to reuse implementation without subtyping) –Java access: default is package protected, need private to hide variables and methods

27 March 2001CS 655: Lecture 176 A Type and Class Hierarchy Shape Quadrangle Triangle Rectangle Parallelogram Rhombus Square Equilateral EquilateralTriangle

27 March 2001CS 655: Lecture 177 Add an attribute Shapes should have a color and set_color method Change Shape, Quadrangle, Parallelogram, Triangle, Equilateral, EquilateralTriangle, Rhombus, Rectangle, Square, etc. Change Shape, others inherit new attribute and method automatically

27 March 2001CS 655: Lecture 178 Add is_equilateral bool Shape::is_equilateral () { return false; } bool Equilateral::is_equilateral () { return true; }

27 March 2001CS 655: Lecture 179 Is a Rhombus equilateral? Shape Quadrangle Parallelogram Rhombus Equilateral is_equilateral? is_equilateral () { return false; } is_equilateral () { return true; } Multiple inheritance can be tricky!

27 March 2001CS 655: Lecture 1710 Solutions Java, Ada95 –Don’t allow it (Java: interfaces for multiple supertypes, not implementation sharing) –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)

27 March 2001CS 655: Lecture 1711 Smalltalk Design Principles Personal Mastery: If a system is to serve the creative spirit, it must be entirely comprehensible to a single individual. Storage Management: To be truly "object- oriented", a computer system must provide automatic storage management. Uniform Metaphor: A language should be designed around a powerful metaphor that can be uniformly applied in all areas.

27 March 2001CS 655: Lecture 1712 Smalltalk Design Principles 2 Operating System: An operating system is a collection of things that don't fit into a language. There shouldn't be one. Natural Selection: Languages and systems that are of sound design will persist, to be supplanted only by better ones.

27 March 2001CS 655: Lecture 1713 “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.” Stroustrup’s Conclusions

27 March 2001CS 655: Lecture 1714 My Conclusions Object-Oriented Programming is a state of mind. It is difficult to reach that state of mind if your language doesn’t have a way to declare S  T and the type judgment: Other language features can help, but we aren’t yet sure what the right ones are: dynamic dispatch, implementation inheritance, mixins, automated delegation, etc. A E : S, S  T A E : T [subsumption]

27 March 2001CS 655: Lecture 1715 Analogies Structured Programming is a state of mind. It is difficult to reach that state of mind if your language doesn’t have structured control statements (e.g., while, for, if, blocks, procedures) Data Abstraction is a state of mind. It is difficult to reach that state of mind if your language doesn’t have type checking and mechanisms for restricting access

How do we know if S  T is safe?

27 March 2001CS 655: Lecture 1717 What does it mean for S  T to be safe? Liskov & Wing: “objects of the subtype ought to behave the same as those of the supertype as far as anyone or any program using supertype objects can tell.” For all functions f, if f behaves correctly when passed a T, f behaves correctly when passed an S. Too Strong For all programs f, if f can be shown to satisfy its specification using the specification of T, then f can be shown to satisfy its specification using the specification of S.

27 March 2001CS 655: Lecture 1718 L & W’s Subtype Requirement Let  (x) be a property provable about objects x of type T. Then  (y) should be true for objects y of type S where S is a subtype of T. Same meaning? For all programs P, if P can be shown to satisfy its specification using the specification of T, then P can be shown to satisfy its specification using the specification of S.

27 March 2001CS 655: Lecture 1719 Type Specification Description of type’s value space Type invariant and history properties (constraint) –How different from rep invariant? For each method: –Behavior in terms of pre-conditions and post- conditions No creators – allows subtypes to provide different creators –Need to prove creators establish invariant and constraint

27 March 2001CS 655: Lecture 1720 Two-Tiered Specification Separate interface-level specification from sort specification Specs in paper are interface-level specifications only: bag = type uses BBag (bag for B)... get = proc () returns (int) requires b pre.elems  { } What does this mean?

27 March 2001CS 655: Lecture 1721 LSL Specification Bag (E, C) : trait introduces { } :  C; insert : E, C  C; count : E, C  Int asserts C generated by {}, insert C partitioned by count  b: C, e, e1, e2: E count (e, {}) == 0; count (e1, insert (e2, b)) == count (e1, b) + (if e1 = e2 then 1 else 0) BBag (B) tuple of bound: Int, elems: Bag (Int, B for C)

27 March 2001CS 655: Lecture 1722 Subtype Definition ( S  T) 1.Subtype methods preserve the supertype methods’ behavior: Signatures have contravariant arguments, covariant results Pre-conditions of T imply preconditions of S; post-conditions of S imply post- conditions of T. 2.Subtypes preserve supertype properties Invariant of S implies invariant of T. Constraint of S implies constraint of T.

27 March 2001CS 655: Lecture 1723 Subtype Condition 1: Signature Rule Subtype methods preserve the supertype methods’ behavior: –Signature: Contravariance of arguments, covariance of result (typing rule we saw last time) Exceptions by m s are contained in set of exceptions signed by m T

27 March 2001CS 655: Lecture 1724 Methods rule: –Pre-condition  x : s m T.pre [ A (x pre ) / x pre ]  m S.pre Replace every x pre in m T.pre with A (x pre ). Abstraction function, A : s  t. –Post-condition m S.post  m T.post [A (x pre ) / x pre, A (x post ) / x post ] Subtype Condition 1: Methods Rule “contravariance – subtype is weaker” “covariance – subtype is stronger”

27 March 2001CS 655: Lecture Subtypes preserve supertype properties For all states p and q such that p precedes q, for all x: S: Invariant Rule I S  I T [ A (x p ) / x p ] Constraint Rule C S  C T [A (x p ) / x p, A (x q ) / x q ] “covariance – subtype is stronger” Subtype Relation 2: Preserves supertype Properties

27 March 2001CS 655: Lecture 1726 Charge Don’t stop working on your projects just because you turned in your proposal... Next time: pragmatic aspects of OO languages - comparison of Sather, Eiffel, Java and C++