230 likes | 349 Views
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
E N D
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 • 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
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
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
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()
Computing in OOP • Create a hierarchical ontology of objects in the world • Simulate their communications and processes • Reuse the objects in other programs
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
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();
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
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
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’
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
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
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
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
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)
Are Subclasses Subtypes • Variables of type subclass can appear wherever variables of type parent are legal • No-brainer? • Not necessarily in C++!