Foundations of Object-Oriented Programming

Foundations of Object-Oriented Programming

Foundations of Object-Oriented Programming RALF HINZE Background Foundations of Object-Oriented Programming FVOP FEOP FOOP Introduction Objects RALF HINZE Classes Open Recursion Subtyping Institut f¨urInformatik III, Universit¨at Bonn Delegation R¨omerstraße 164, 53117 Bonn Inheritance Email: [email protected] Conclusion Homepage: http://www.informatik.uni-bonn.de/~ralf Appendix February 2007 (Pick up the slides at . /~ralf/talks.html#54.) 1 / 28 Foundations of Vision Object-Oriented Programming RALF HINZE Background Possible future course “Foundations of Programming”: FVOP I FVOP: Foundations of Value-Oriented Programming; FEOP I FEOP: Foundations of Effect-Oriented Programming; FOOP Introduction I FOOP: Foundations of Object-Oriented Programming. Objects Classes Open Recursion Subtyping Delegation Inheritance Conclusion Appendix A large part of the material is classroom-tested: I advanced course on “Principles of Programming Languages”; I introductory course on “Programming”. 2 / 28 Foundations of Concept Object-Oriented Programming RALF HINZE You can never understand one language until you understand at least two. Background Ronald Searle (1920–) FVOP FEOP Make everything as simple as possible, FOOP but not simpler. Introduction Objects Albert Einstein (1859–1955) Classes Open Recursion Subtyping Delegation I Idea: explain programming language concepts by growing a “teaching Inheritance language”: Conclusion I empty language, Appendix I functional language, I imperative language, I object-oriented language; I define everything precisely: syntax and semantics; I concentrate on essential concepts and ideas; I guiding principle: a concept should be as simple and pure as possible; I the concepts should be orthogonal; I use the teaching language to explain features of “real programming languages”. 3 / 28 Foundations of Foundations of Value-Oriented Programming Object-Oriented Programming RALF HINZE Background local FVOP val base = 8 FEOP FOOP in Introduction function digits (n : Nat): List hNati = Objects Classes if n 0 then Nil Open Recursion else Cons (n % base, digits (n ÷ base)) Subtyping Delegation Inheritance function nat (ds : List hNati): Nat = Conclusion case ds of Appendix Nil ⇒ 0 | Cons (d, ds0) ⇒ d + base ∗ nat (ds0) end end Introduces expressions, declarations, the concept of scope etc. Advanced topics: type abstraction, polymorphism, contracts etc. 4 / 28 Foundations of Foundations of Effect-Oriented Programming Object-Oriented Programming RALF HINZE Background FVOP FEOP local FOOP val bal = ref 0 Introduction Objects in Classes function deposit (amount : Nat): () = Open Recursion Subtyping bal := !bal + amount Delegation Inheritance function withdraw (amount : Nat): () = Conclusion bal := !bal − amount Appendix function balance () : Nat = !bal end Introduces IO, state, exceptions, the concept of extent etc. 5 / 28 Foundations of Table of contents Object-Oriented Programming RALF HINZE 1 Background Background FVOP 2 Foundations of Value-Oriented Programming FEOP FOOP Introduction 3 Foundations of Effect-Oriented Programming Objects Classes Open Recursion 4 Foundations of Object-Oriented Programming Subtyping Delegation Introduction Inheritance Objects Conclusion Appendix Classes Open Recursion Subtyping Delegation Inheritance Conclusion 5 Appendix 6 / 28 Foundations of Introduction — Jargon Object-Oriented Programming RALF HINZE Background FVOP Every subject has its jargon, object-oriented programming is no exception: FEOP abstract class, anonymous class, behaviour, class hierarchy, class method, FOOP Introduction class variable, class, constructor, delegation, dispatch table, down cast, Objects dynamic binding, dynamic dispatch, encapsulation, extension, field, final, Classes Open Recursion friend, generic class, implementation, inclusion polymorphism, inheritance, Subtyping Delegation inner class, instance variable, instance, interface inheritance, interface, late Inheritance binding, message passing, method invocation, method, multiple inheritance, Conclusion name subtyping, new, object creation, object, object-oriented, open Appendix recursion, overriding, package, private, protected, public, redefinition, self, structural subtyping, subclass, subtype polymorphism, subtyping, super, superclass, this, up cast, (virtual) method table, visibility. Object-orientation seems to be complex and loaded. To help de-mystify the subject, we shall identify the essential principles or characteristics. 7 / 28 Foundations of Characteristics — What is object-oriented programming? Object-Oriented Programming I Dynamic dispatch: RALF HINZE When an operation is invoked on an object, the object itself Background determines what code gets executed. Two objects with the same FVOP interface may be implemented quite differently. FEOP I Encapsulation: FOOP Introduction The implementation of an object is hidden from view. Changes to the Objects implementation can only affect the object itself. Classes Open Recursion I Open recursion: Subtyping Delegation One method can invoke another method via a special identifier called Inheritance self , which is late-bound — dynamic dispatch for recursive invocations. Conclusion Appendix I Subtyping: An object that supports more operations can be used as an object that supports less operations. The ability to ignore parts of an interface allows us to write general code that manipulates different sorts of objects in a uniform way. I Inheritance: The behaviour of an object can be reused in another object so that common behaviour must be implemented just once. This reuse of behaviour can be achieved I via objects and delegation or I via classes and subclassing. 8 / 28 Foundations of Recapitulation: encapsulation in an effect-oriented language Object-Oriented Programming RALF HINZE Encapsulation is achieved by delimiting the scope of internals — only Background entities that have a name can be accessed and reused. FVOP FEOP local FOOP val bal = ref 0 Introduction in Objects Classes function deposit (amount : Nat): () = Open Recursion Subtyping bal := !bal + amount Delegation Inheritance function withdraw (amount : Nat): () = Conclusion bal := !bal − amount Appendix function balance () : Nat = !bal end The representation of a bank account, the reference cell bal, is local to deposit, withdraw and balance. Observation: The functions deposit, withdraw and balance belong together, but they are only loosely coupled. 9 / 28 Foundations of Objects Object-Oriented Programming Wish: linguistic support for integrating operations into a single entity. RALF HINZE Background val my-account = FVOP object FEOP local FOOP Introduction val bal = ref 0 Objects Classes in Open Recursion method deposit (amount : Nat): () = Subtyping Delegation bal := !bal + amount Inheritance Conclusion method withdraw (amount : Nat): () = Appendix bal := !bal − amount method balance : Nat = !bal end end The entity is called an object. As a first approximation an object can be seen as a record of functions. Syntactic changes: object ... end bracket; method instead of function. 10 / 28 Foundations of Method invocation Object-Oriented Programming RALF HINZE Background An integrated function aka method is invoked using the dot notation. FVOP FEOP (Recall: ≫ is the prompt of the evaluator). FOOP Introduction my-account.deposit (4711) Objects ≫ Classes () Open Recursion Subtyping ≫ my-account.withdraw (815) Delegation () Inheritance Conclusion ≫ my-account.withdraw (2765) Appendix () ≫ my-account.balance 1131 Jargon: we say “invoking a method on an object” or “sending an object a message”. 11 / 28 Foundations of Interfaces Object-Oriented Programming RALF HINZE Background Every well-formed expression has a type; object ... end is an expression; FVOP so what is the type of an object? FEOP FOOP type Account = Introduction Objects object Classes method deposit : Nat → () Open Recursion Subtyping method withdraw : Nat → () Delegation Inheritance method balance : Nat Conclusion end Appendix Recall: a type represents our static knowledge of an expression. An object is solely defined by its behaviour; the object’s internal representation does not appear in its type! An object type such as Account is also called an interface. An interface is the set of operations an object supports. 12 / 28 Foundations of Objects and Interfaces Object-Oriented Programming RALF HINZE A different implementation of a bank account with the same interface: Background FVOP val your-account = FEOP object FOOP local Introduction Objects val n = ref 0 Classes Open Recursion val history = array [10] i ⇒ ref 0 Subtyping Delegation function inc (n : Nat): Nat = (n + 1)% 10 Inheritance in Conclusion method deposit (amount : Nat): () = Appendix history.[inc (!n)] := !history.[!n] + amount; n := inc (!n) method withdraw (amount : Nat): () = history.[inc (!n)] := !history.[!n] − amount; n := inc (!n) method balance : Nat = !history.[!n] end end 13 / 28 Foundations of Demo Object-Oriented Programming RALF HINZE Background FVOP ≫ my-account.balance 1131 FEOP FOOP ≫ your-account.deposit 4711 Introduction () Objects Classes ≫ my-account.withdraw 1000; your-account.deposit 1000 Open Recursion () Subtyping Delegation ≫ my-account.balance Inheritance 131 Conclusion Appendix ≫ your-account.balance 5711 The objects my-account and your-account are interchangeable — at least from the point of view of the language. They respond to the same messages. Each object itself determines what code gets executed. 14 / 28 Foundations of Objects and Interfaces Object-Oriented Programming RALF HINZE The fact

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    22 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us