Object-Oriented Programming

Object-oriented programming is characterized by programming with objects, messages, and hierarchies of objects.

The surest way to improve programming productivity is so obvious that many programmers miss it. Simply write less code.
-- Samuel P. Harbison

Keywords and phrases: Abstract Data Type, object-based, object-oriented, Inheritance, Object, sub-type, super-type, sub-range, sub-class, super-class, polymorphism, overloading, dynamic type checking, Class, Instance, method, message


Object-oriented programming shifts the emphasis from data as passive elements defined by relations or acted on by functions and procedures to active elements interacting with their environment. In the context of imperative programming, the emphasis shifts from describing control flow to describing interacting objects.

Object-oriented programming developed out of simulation programs. The conceptual model used is that the structure of the simulation should reflect the environement that is being simulated. For example, if an industrial process is to be simulated, then there should be an object for each entity involved in the process. The objects interact by sending messages.

Each object is designed around a data invariant.

Object-oriented programming is an abstraction and generalization of imperative programming. Imperative programming involves a state and a set of operations to transform the state. Object-oriented programming involves collections of objects each with a state and a set of operations to transform the state. Thus, object-oriented programming focuses on data rather than on control. As in the real world, objects interact so object-oriented programming uses the metaphor of message passing to capture the interaction of objects.

Functional objects are like values, imperative objects are like variables, active objects are like processes.

Aternatively, OOP, an object is a parameter (function and logic), an object is a mutable self (imperative).

Programming in an imperative programming language requires the programmer to think in terms of data structures and algorithms for manipulating the data structure. That is, data is placed in a data structure and the data structure is manipulated by various procedures.

Programming in an object-oriented language requires the programmer to think in terms of a hierarchy of objects and the properties possessed by the objects. The emphasis is on generality and reusability.

Procedures and functions are the focus of programming in an imperative language. Object-oriented programming focuses on data, the objects and the operations required to satisfy a particular task.

Object-oriented programming, as typified by the Small-talk model, views the programming task as dealing with objects which interact by sending messages to each other. Concurrency is not necessarily implied by this model and destructive assignment is provided. In particular, to the notion of an abstract data type, OOP adds the notion of inheritance, a hierarchy of relationships among types. The idea of data is generalized from simple items in a domain to data type (a domain and associated operations) to an abstract data type (the addition of information hiding) to OOP \& inheritance.

Here are some definitions to enable us to speak the language of object-oriented programming.

I think a classification which helps is to classify languages as object-based and object-oriented. A report we recently prepared on OO technology trends reported that object-based languages support to varying degrees: object-based modularity, data abstraction (ADTs) encapsulation and garbage collection. Object-oriented languages additionally include to varying degrees: grouping objects into classes, relating those classes by inheritance, polymorphism and dynamic binding, and genericity.

Dr. Bertrand Meyer in his book 'Object-oriented Software Construction' (Prentice Hall) gives his 'seven steps to object-based (oriented) happiness'

  1. Object-based modular structure
  2. Data abstraction
  3. Automatic memory management
  4. Classes
  5. Inheritance
  6. Polymorphism and Dynamic Binding
  7. Multiple and Repeated Inheritance

Subtypes (subranges)

The subtype principle states that a subtype may appear wherever an element of the super type is expected.

Objects

Objects are collections of operations that share a state. The operations determine the messages (calls) to which the object can respond, while the shared state is hidden from the outside world and is accessible only to the object's operations. Variables representing the internal state of an object are called instance variables and its operations are called methods. Its collection of methods determines its interface and its behavior.

Objects which are collections of functions but which do not have a state are functional objects. Functional objects are like values, they have the object-like interface but no identity that persists between changes of state. Functional objects arise in logic and functional programming languages.

Syntactically, a functional object can be represented as:

name : object

   methods

   ...
For example,

Objects which have an updateable state are imperative objects. Imperative objects are like variables. They are the objects of Simula, Smalltalk and C++. They have a name, a collection of methods which are activated by the receipt of messages from other objects, and instance variables which are shared by the methods of the object but inaccessible to other objects.

Syntactically, an imperative object can be represented as:

name : object
  
   variables

   ...


   methods

   ...
Objects which may be active when a message arrives are active objects. In contrast, functional and imperative objects are passive unless activated by a message. Active objects have three modes: when there is nothing to do the object is dormant, when the agent is executing it is active, and when an object is waiting for a resource or the completion of subtasks it is waiting. Messages sent to an active object may have to wait in queue until the object finishes a task. Message passing among objects may be synchronous or asynchronous.


Figure M.N: Object implementation

Instance dataShared methodsCode
methods
data field1
...
date fieldm
-->
method1
...
methodn
-->

Classes

Classes serve as templates from which objects can be created. Classes have the same instance variables and operations as corresponding objects but their interpretation is different. Instance variables in an object represent actual variables while class instance variables are potential, being instantiated only when an object is created.

We may think of a class as specifying a behavior common to all objects of the class. The instance variables specify a structure (data structure) for realizing the behavior. The public operations of a class determine its behavior while the private instance variables determine its structure.

Private copies of a class can be created by a make-instance operation, which creates a copy of the class instance variables that may be acted on by the class operations.

Syntactically, a class can be represented as:

name : class

   instance variables
   ...

   class variables
   ...

   instance methods
   ...
   class methods
   ...
Classes specify the behavior common to all elements of the class. The operations of a class determine the behavior while the instance variables determine the structure.

Algebraic semantics

Many sorted algebras may be used to model classes.

Inheritance

Inheritance allows us to reuse the behavior of a class in the definition of new classes. Subclasses of a class inherit the operations of their parent class and may add new operations and new instance variables.

Inheritance captures a form of abstraction called super-abstraction, that complements data abstraction. Inheritance can express relations among behaviors such as classification, specialization, generalization, approximation, and evolution.

Inheritance classifies classes in much the way classes classify values. The ability to classify classes provides greater classification power and conceptual modeling power. Classification of classes may be referred to as second-order classification. Inheritance provides second-order sharing, management, and manipulation of behavior that complements first-order management of objects by classes.

Syntactically, inheritance may be specified in a class as:

name : class

   super class
   ...
   instance variables
   { as before }
What should be inherited? Should it be behavior or code: specification or implementation? Behavior and code hierarchies are rarely compatible with each other and are often negatively correlated because shared behavior and shared code have different requirements.

Representation, Behavior, Code

DYNAMIC/STATIC/INHERITANCE

Inheritance and OOP

Type hierarchy

Semantics of inheritance in the functional paradigm.

type op params = case op of
   f0 : f0 params
...
   fn : fn params
   otherwise : supertype op params
   where
   f0 params = def0
...
   fn params = defn
inheritance in the logic programming paradigm. object(structure,methodslist). isa(type1,type2). object(rectangle(Length,Width),[area(A is Length*Width)]).


Figure M.N: Implementation of inheritance

Object supertypeShared methodsCode
methods
fields
-->
methods
-->
Object subtype
methods
shared fields
new fields
-->
shared methods
new methods
-->

Algebraic semantics

Order-sorted algebras are required to capture the ordering relations among sorts that arise in subtypes and inheritance.

Types and Classes

The concept of a type and the concept of a class have much in common and depending on the point of view, they may be indistinguishable. The distinction between types and classes may be seen when we examine the compare the inheritance relationship between types and subtypes with the inheritance relationship between classes and subclasses.

Example: The natural numbers are a subtype of the integers but while subtraction is defined for all pairs of integers it is not defined for all pairs of natural numbers.

This is an example of subtype inheritance. Subtypes are defined by additional constraints imposed on a type. The set of values satisfying a subtype is a subset of the set of values satisfying the type and subtypes inherit a subset of the behaviors of the type.

Example: The integers are a subclass of the natural numbers since, the subtraction operation of the natural numbers can be extended to subtraction for integers.

Example: The rational numbers are a subclass of the integers since, they can be defined as pairs of natural numbers and the arithmetic operations on the rational numbers can be defined in terms of the arithmetic operations on natural numbers.

These are examples of subclass inheritance. Subclasses are defined by extending the class behavior. This means that subclasses are more loosely related to their parent than a subtype to a type. Both state and methods may be extended.

Subtyping strongly contrains behavior while subclassing is an unconstrained mechanism. It is the inheritance mechanism of OOP that distingushes between types and classes.

These examples illustrate that subtype inheritance is different from subclass inheritance. Subclasses may define behavior completely unrelated to the parent class.

Types are used for type checking while classes are used for generating and managing objects with uniform properties and behaviors. Every class is a type. However, not every type is a class, since predicates do not necessaryly determine object templates. We will use the term type to refer to structure and values while the term class will be used to refer to behavior.

Examples

Queue -- insert_rear, delete_front
Deque -- insert_front, delete_front, insert_rear, delete_rear
Stack -- push, pop
List -- cons, head, tail
Binary tree -- insert, remove, traverse
Doublely linked list -- 
Graph -- linkto, path,
Natural numbers -- Ds
Integers -- (=-,Ds)
Rationals
Reals -- (+-,Ds,Ds)
Complex (a,b) or (r,$\theta$)

Historical Perspectives and Further Reading

Much of this section follows Peter Wegner\cite{Wegner90}.

Exercises

  1. [time/difficulty] (section) Problem statement
  2. Stack
  3. Queue
  4. Tree
  5. Construct a ``turtle graphics''
  6. Construct a table handler
  7. Grammar
  8. Prime number sieve
  9. Account, Checking, Savings
  10. Point, circle

© 1996 by A. Aaby