1 / 20

ICS 313: Programming Language Theory

ICS 313: Programming Language Theory. Chapter 12: Object Oriented Programming. Object Orientedness. Three essential features: ADTs Inheritance Polymorphism by Dynamic Binding. ADTs in OOP. Classes are Abstract Data Types: Syntactic encapsulation of data and operations on the data

Download Presentation

ICS 313: Programming Language Theory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 313:Programming Language Theory Chapter 12: Object Oriented Programming

  2. Object Orientedness • Three essential features: • ADTs • Inheritance • Polymorphism by Dynamic Binding

  3. ADTs in OOP • Classes are Abstract Data Types: • Syntactic encapsulation of data and operations on the data • Data accessible only through the defined operations • Classes are a natural extension to the concept of Abstract Data Types: • Apply principle of abstraction to a collection of related ADTs by factoring out commonalities into superclasses

  4. Inheritance • Motivated by software reuse: • Need to modify the original ADT • Need to model interrelationships and hierarchy • Inheritance addresses these issues: • Inherit functionality of an ADT with selective modifications • Inheritance hierarchy models the application domain

  5. Classes Objects (class instances) Subclass or derived class Superclass or parent class Methods Messages Instance variables Instance methods Class variables Class methods Public Private Protected Abstract Class Inheritance: Some Terms

  6. Polymorphism • Dynamic Binding of Messages to Method Definitions Class method • Variables of type class can reference objects of any subclass of class • Methods bound to definition dynamically depending on the referenced object ClassB method ClassA method ClassC method public Class var = new …; var.method()

  7. Computing in OOP • Create a hierarchical ontology of objects in the world • Simulate their communications and processes • Reuse the objects in other programs

  8. Design Issues

  9. Exclusivity of Objects • Purely object oriented • All types are classes • Everything is an object • Elegant but slow • SMALLTALK • Overlay an Object System • C++: OOP added to imperative language • CLOS: OOP added to functional language • Large and complex languages • Primitives types are not objects • Java • Need wrapper classes

  10. Wrapper Classes in Java • From a postfix evaluation application: evalStack = new Stack(); • Putting integers on the stack: evalStack.push(new Integer(value)); • Retrieving integers: ((Integer)evalStack.pop()).intValue();

  11. What is inherited? • (Caution: confusion possible) • Interface Inheritance (ADT interface) • Behaviors of methods (you can call them) • Safer but less efficient • Implementation Inheritance • Structure (you can access the data structures directly) • Protocol Inheritance • Only parameter profile and return type • Java Interfaces

  12. Class ClassA ClassB ClassC Single and Multiple Inheritance • Single inheritance: Smalltalk • Multiple inheritance: C++ • Goldfish extends Fish, Pet • Name collisions: which implementation should be used? • More complex dependencies

  13. Interfaces • Java avoids collisions with Interfaces • Only one implementation inherited public class Clock extends Applet implements Runnable • Requires Clock to implement methods of Runnable • Can lead to different complexities, e.g. using ‘has-a’ as substitute for ‘is-a’

  14. Allocation and Deallocation • Where allocated? • Static • Stack-dynamic • Heap: new • C++: all of these • Smalltalk: Heap only • Implicit or Explicit Deallocation? • C++: Explicit delete • Smalltalk, Java: Implicit

  15. Type Checking and Polymorphism • Binding of messages to methods must be dynamic when polymorphic variables are involved • When does type checking of this binding happen? • If you want strong typing, checking must be static • This restricts the language: overriding methods must have matching protocol • Dynamic type checking is more costly

  16. Dynamic and Static Binding • Can the programmer specify that some methods calls are statically bound? • Java: • Default is Dynamic • For static, define method to be final • C++: • Default is Static • Pointer of type base class may reference derived class • Declare function virtual for dynamic binding

  17. C++ Example: Static and Dynamic • class shape { • public: • virtual void draw() = 0; … } // pure virtual • class rectangle : public shape { • public: • virtual void draw() { … } …} • class square : public rectangle { • public: • virtual void draw() { … } … } • square s; • rectangle r; • shape &refsq = s; • refsq.draw() // dynamically bound (ptr to base class) • r.draw(); // statically bound

  18. Implementation Issues • Instance Data Storage • Static storage: class instance records • Instance variable addressing is constant offset • C++ • Dynamic storage: association list or dictionary • Python • Binding of Messages to Methods • Static: more efficent • Dynamic: requires pointers to methods • Need only store once in Virtual Method Table or class record (when classes are instances)

  19. End of Module

  20. Are Subclasses Subtypes • Variables of type subclass can appear wherever variables of type parent are legal • No-brainer? • Not necessarily in C++!

More Related