Object Oriented Concepts
Object Oriented Concepts
Each object implements its own module and is responsible for initializing and
destroying itself correctly.
You might ask: So what? Isn't this just a fancier modular programming technique?
You were right, if this would be all about object-orientation. Fortunately, it is not,
object-oriented programming is a new programming technique.
Object-Oriented Concepts
i = 1; /* Assign 1 to integer i */
j = 2; /* Assign 2 to integer j */
k = i + j; /* Assign the sum of i and j to k */
Let's play with the above code fragment and outline the relationship to the ADT
Integer. The first line defines three instances i, j and k of type Integer.
Consequently, for each instance the special operation constructor should be called.
In our example, this is internally done by the compiler. The compiler reserves
memory to hold the value of an integer and ``binds'' the corresponding name to it.
If you refer to i you actually refer to this memory area which was ``constructed'' by
the definition of i. Optionally, compilers might choose to initialize the memory, for
example, they might set it to 0 (zero).
sets the value of i to be 1. Therefore we can describe this line with help of the ADT
notation as follows:
Perform operation set with argument 1 on the Integer instance i. This is written as
follows: i.set(1).
We now have a representation at two levels. The first level is the ADT level where
we express everything that is done to an instance of this ADT by the invocation of
defined operations. At this level, pre- and postconditions are used to describe what
actually happens. In the following example, these conditions are enclosed in curly
brackets.
i := 1;
Let's stress these levels a little bit further and have a look at the line
k = i + j;
Obviously, ``+'' was chosen to implement the add operation. We could read the
part ``i + j'' as ``add the value of j to the value of i'', thus at the ADT level this
results in
Class
In the example above as well as in examples which follow we use a notation which
is not programming language specific. In this notation class {...} denotes the
definition of a class. Enclosed in the curly brackets are two sections attributes: and
methods: which define the implementation of the data structure and operations of
the corresponding ADT. Again we distinguish the two levels with different terms:
At the implementation level we speak of ``attributes'' which are elements of the
data structure at the ADT level. The same applies to ``methods'' which are the
implementation of the ADT operations.
In our example, the data structure consists of only one element: a signed sequence
of digits. The corresponding attribute is an ordinary integer of a programming
language . We only define two methods setValue() and addValue() representing
the two operations set and add.
Object
Objects are uniquely identifiable by a name. Therefore you could have two
distinguishable objects with the same set of values. This is similar to ``traditional''
programming languages where you could have, say two integers i and j both of
which equal to ``2''. Please notice the use of ``i'' and ``j'' in the last sentence to
name the two integers. We refer to the set of values at a particular time as the state
of the object.
The state of the object changes according to the methods which are applied to it.
We refer to these possible sequence of state changes as the behaviour of the object:
We now have two main concepts of object-orientation introduced, class and object.
Object-oriented programming is therefore the implementation of abstract data
types or, in more simple words, the writing of classes. At runtime instances of
these classes, the objects, achieve the goal of the program by changing their states.
Consequently, you can think of your running program as a collection of objects.
The question arises of how these objects interact?
Message
A running program is a pool of objects where objects are created, destroyed and
interacting. This interacting is based on messages which are sent from one object
to another asking the recipient to apply a method on itself. To give you an
understanding of this communication, let's come back to the class Integer. In our
pseudo programming language we could create new objects and invoke methods
on them. For example, we could use
to express the fact, that the integer object i should set its value to 1. This is the
message ``Apply method setValue with argument 1 on yourself.'' sent to object i.
We notate the sending of a message with ``.''. This notation is also used in C++;
other object-oriented languages might use other notations, for example ``- ''.
In our example, the message and the method which should be applied once the
message is received have the same name: We send ``setValue with argument 1'' to
object i which applies ``setValue(1)''.
Summary
How does this view help us developing software? To answer this question let's
recall how we have developed software for procedural programming languages.
The first step was to divide the problem into smaller manageable pieces. Typically
these pieces were oriented to the procedures which were taken place to solve the
problem, rather than the involved data.
Exercises
1. Class.
(a) What distinguishes a class from an ADT?
(b) Design a class for the ADT Complex. What representations do you
choose for the ADT operations? Why?
2. Interacting objects. Have a look to your tasks of your day life. Choose one
which does not involve too many steps (for example, watching TV, cooking a
meal, etc.). Describe this task in procedural and object-oriented form. Try to
begin viewing the world to consist of objects.
3. Object view. Regarding the last exercise, what problems do you encounter?
4. Messages.
(a) Why do we talk about ``messages'' rather than ``procedure calls''?
(b) Name a few messages which make sense in the Internet environment.
(You must therefore identify objects.)
(c) Why makes the term ``message'' more sense in the environment of the
last exercise, than the term ``procedure call''?