Object-Oriented Programming
Gimme, gimme yer homework! What is it? Construction and Destruction Inheritance Models Ad-hoc Polymorphism
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
1 of 20
What is OOP?
Support for objects, whatever those are Encapsulation Inheritance Polymorphism Various dimensions of capabilities Often tied to programming environment
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
2 of 20
Programming Environment
Interpreter/Compiler trade-off Additional or required tools, libraries, etc. Minimal: C++
Many compilers and even some interpreters Very few required libraries No required IDE
Maximal: C#
C# without .NET is crippled C# development is in context of entire .NET environment
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
3 of 20
So What Is an Object?
Intuitive sense: just look around you Traditional programmer sense: Data and functionality
An approach arising from Turing/von Neumann architectures These separated memory/tape from actions/state machine Realistically, we never have one without the other
What must it have?
Identity Capabilities Class?
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
4 of 20
Objects Need Identity
Is it value-based?
3 is the same number no matter where you get it Is the same true for objects? In general, no Polymorphic definition of equality can make things slippery
Is it intrinsic?
For most OOPLs, there is Explicit: C++ Implicit: Java, Eiffel, Lisp/CLOS some intrinsic identity: address
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
5 of 20
Capabilities
Message
Has a name Is sent somewhere What target(s) do depend on target(s), not sender
Method
Has a name Is used to respond to a message Is tied to the identity of the object
Are instance variables different? First, let’s consider encapsulation
G22.2110-001: Programming Languages Object-Oriented Programming May 28, 2002
6 of 20
Encapsulation
Object as a “black box”
You can push the buttons You can’t peek inside
A form of abstraction
Removal of extraneous details How is task accomplished? Not relevant Real world: “How” often resources, etc) is important (e.g., run time, shared
Modules provide encapsulation, but not identity
CLU, Modula-2 early examples of strong encapsulation Creating value-specific copies of modules provides identity
7 of 20
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
Goals of Encapsulation
Simplify programming: Only learn buttons on box Increase reusability
Encapsulation to produce generic chunks of capabilites Reuse whenever capability needed Problem: Genericity/utility trade-off
Increase maintainability
Isolate problems and solutions into smaller chunks of code Complex patterns can distribute capabilities
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
8 of 20
Encapsulation as a Design Pattern in C
Provide an opaque pointer
struct Stack; typedef struct Stack *StackPtr;
Provide a list of functions taking that ptr as param
void StackPush(StackPtr *, void *); void *StackPop(StackPtr *);
Struct definition at other end of ptr is unavailable
List of functions is list of buttons Struct definition is the contents of the black box
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
9 of 20
Instance Variables as Methods
Self: Names of slots
Pairs of get/set slots look like an instance variable Get/set slots could actually do more, e.g., instrumenting
Most languages provide state for an object that an external entity can change
Strictly speaking, breaks encapsulation External entity knows something about how object works Changes to the object could break functionality of external objects
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
10 of 20
Examples
C++: class/struct members
Methods can (almost) duplicate pure abstraction C++ requires function call operator () for methods, not for instance variables
Java: class members
Again, methods (almost) duplicate pure abstraction
Smalltalk, Self: external entity can’t tell
In Self, encapsulation is stronger: object itself can’t tell In Self, object sends message to itself to access “variable” Sender (self) can’t tell anything about response
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
11 of 20
Class
Categories or sets of objects If object is in set/class, it is an instance of the class
Declaration of what capabilities object supports
Class is therefore an encapsulation mechanism
Most languages: Class declaration specifies instance variables (i.e., mutable data)
Every instance of a class has the listed kinds of state and supports the listed messages/methods Can have ramifications for recompile decisions
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
12 of 20
Class as a Pattern in Self
BallClass
bounce roll new: WithMaterial: prototypeBall
BallClass new: WithMaterial:
parent* newSize newMaterial prototype ball copy size: newSize material: newMaterial
prototypeBall
size material parent*
a ball instance
size material parent*
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
13 of 20
Nature of Capabilities
Encapsulation Inheritance Ad-hoc polymorphism
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
14 of 20
Encapsulation
Access control
Public: Anyone can use it Class private: Only instances of my class can use it (C++, Java) Instance private: Only this instance can use it (Eiffel) Protected: I can use it and my subtypes can use it
Loopholes
C++ ugliness: friend, casts Eiffel: export to specific classes
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
15 of 20
Names
Prefixes based on class name
C++: classname::foo Java: com.nellardo.Dingo.boo Self: messages
Replacement names for inconveniently named objects
Eiffel: import allows renaming
Often tied to a (separate) module mechanism
Do objects provide encapsulation or just modules? More later
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
16 of 20
Inheritance
An object shares information with another Most OOPLs: instances share with class, class shares with other class(es) For classes, a subclass inherits from a superclass
All instance vars and methods for superclass are available in subclass In general, subclass may add inst vars and may add or replace methods
Issue: Multiplicity Issue: Mutability
G22.2110-001: Programming Languages Object-Oriented Programming May 28, 2002
17 of 20
Multiplicity in Inheritance
One and only one superclass? Or more? One leads to forests or trees
Forest: No common base class Tree: A single common base class
Many can cause ambiguity
What is shared and what is duplicated?
Many langs use one but allow mixins or interfaces
Mix-in: add a “flavor” Interface: All virtual methods
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
18 of 20
Polymorphism
Parametric polymorphism vs ad-hoc polymorphism Parametric: Polymorphic over a family of types Ad-hoc: A single type can be extended arbitrarily
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
19 of 20
Construction and Destruction
Construction
Primitive types have useful default values Objects may, but the compiler can’t necessarily deduce them E.g., color with opacity
Garbage collection
System reclaims memory when it can no longer be accessed
Destruction
Required for manual memory management Since you release mem yourself, must explicitly release any mem object allocated
G22.2110-001: Programming Languages
Object-Oriented Programming May 28, 2002
20 of 20