Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming."— Presentation transcript:

1 (1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming

2 (2) Object Orientedness Three essential features: ADTs Inheritance Polymorphism by Dynamic Binding

3 (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 (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 (5) Inheritance: Some Terms 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

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

7 (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 (8) Design Issues

9 (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 (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 (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 (12) Single and Multiple Inheritance Single inheritance: Smalltalk Multiple inheritance: C++ Goldfish extends Fish, Pet Name collisions: which implementation should be used? More complex dependencies Class ClassAClassB ClassC

13 (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 (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 (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 (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 (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 (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 (19) End of Module

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


Download ppt "(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming."

Similar presentations


Ads by Google