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.
Dr. Bertrand Meyer in his book 'Object-oriented Software Construction' (Prentice Hall) gives his 'seven steps to object-based (oriented) happiness'
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:
For example,name : object methods ...
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:
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.name : object variables ... methods ...
Instance data Shared methods Code
methods data field1 ... date fieldm -->
method1 ... methodn -->
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:
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.name : class instance variables ... class variables ... instance methods ... class methods ...
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:
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.name : class super class ... instance variables { as before }
Representation, Behavior, Code DYNAMIC/STATIC/INHERITANCE Inheritance and OOP Type hierarchy Semantics of inheritance in the functional paradigm.inheritance in the logic programming paradigm. object(structure,methodslist). isa(type1,type2). object(rectangle(Length,Width),[area(A is Length*Width)]).type op params = case op of f0 : f0 params ... fn : fn params otherwise : supertype op params where f0 params = def0 ... fn params = defn
Object supertype Shared methods Code
methods fields -->
methods --> Object subtype
methods shared fields new fields -->
shared methods new methods -->
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.
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$)