Académique Documents
Professionnel Documents
Culture Documents
1. Basics
Azeddine Chikh
University of Tlemcen
Objective
1. Basics
2. Objects and Classes
3. Messages and communication between objects
4. Encapsulation and Object memory management
5. Inheritance and Polymorphism
6. Abstract class, Object cloning and Comparison
Plan
Habit of describing the world around us using the trio: <entity (or object), attribute, value>:
< Car, color, red>, <Car, brand, Peugeot >, <Citizen, size, large >, < citizen, age, 50>.
The objects (Car and Citizen) stand out because each is characterized by a set of
attributes (color, brand, age, size), taking on a particular, uniform value “over the entire
object ”.
The world of attributes is much less diverse than that of objects. This is one of the reasons
for grouping objects into classes and subclasses.
It is possible in all computer languages to store and manipulate objects in memory, such
as so many sets of attribute/value pairs.
Plan
Primitive types
Storing objects in memory
Primitive types
Central memory
Objects are structurally described by a first set of primitive type attributes, such as integer,
real or character, which is used to determine precisely how much space they occupy in
memory.
Storing objects in memory
Primitive types
Storing objects in memory
Primitive types
Relational databases
This is the most common method of storing data on permanent media in IT. Data is stored
as records in tables, through a set of attribute/value pairs, including a primary key
essential to singularizing each record. The key takes a unique value per record.
Relations are then established between two tables by a joining mechanism between the
primary key of the first table and the foreign key of the second one.
The disappearance of these keys in OO practice makes saving objects in these tables a
thorny problem in today's computing.
Storing objects in memory
Primitive types
Storing objects in memory
Primitive types
Although objects are very numerous and different, they can be grouped into a small
number of classes. These are the classes which are responsible for defining the type and
number of attributes which characterize them.
This way of proceeding is not innovative and is in no way the origin of OO. The simple
operation of storing and manipulating objects, in accordance with a "class" model, is not
what fundamentally distinguishes OO computing from that designated as "procedural" or
"functional".
Also throughout time, mathematicians, physicists or other scientists have manipulated
mathematical objects characterized by a set of attribute/value pairs.
Storing objects in memory
Memory space
In recent years, more and more processors have chosen a 64-bit architecture instead of
32, which notably implies a profound revision of all the addressing mechanisms in the
operating systems. Since then, computer scientists can feel at ease with the immensity of
the address space open to them: 264, or 18,446,744,073,709,551,616 bytes.
Indirect addressing
It is the possibility for a variable, not to be associated directly with data, but rather with a
physical address of a location containing this data. It becomes possible to defer the choice
of this address during the execution of the program, while naturally using the variable.
Several of these variables can then point to the same location because they share the
same address. Such a variable, whose value is an address, is called a pointer in C.
Storing objects in memory
When writing an OO program, we commonly access the same object through several
referents, created in different contexts of use. This multiplication of referents is a
determining element of the memory management associated with objects.
We will accept at this stage that it is useful for an object to remain in memory as long as it
is possible to refer to it. Without a referent, an object is inaccessible. The day we are no
longer even a number in any database, we are dead (socially, at least).
Plan
Passive version
Composite object
Between them, objects can enter into a composition-like relationship, where some are
contained within others and are only accessible from these others. Their existence
depends entirely on that of the objects that contain them.
Passive version
Passive and active version of an object
Active version
Activity of objects
Objects are not limited to being static. They move, change shape, color, mood, often
following direct interaction with other objects. Inert objects are inherently much less
interesting than those that are constantly changing. Thus, the object will be all the richer in
interest as it is subject to numerous and varied state transitions.
The car stops because the light has turned red and it starts again as soon as it turns green.
People cross when cars stop.
Change of states
The life cycle of an object, during the execution of an OO program, is limited to a
succession of changes of states, until its pure and simple disappearance from central
memory.
Who is the cause of the state changes ?
Answer in the rest of this course.
How to link operations and attributes ?
Answer in the rest of this course.
Passive and active version of an object
Active version
Plan
Class
A new data structure in OO, the main role of which is to unify within it all the attributes of
the object and all the operations concerning them. These operations are called methods
and group together a set of instructions relating to the attributes of the object.
For programmers from a procedural background, class attributes are like implicit
arguments passed to the method or even variables whose scope of action is limited to the
class alone. The methods take care of the entire active part of this small program now
called “class” and which tends to only represent a single concept of perceived reality.
Sending messages
The only mode of communication between two objects is the possibility for the first to
trigger on the second a method declared and defined in the class of the latter. We will call
this communication mechanism “message sending” from the first object to the second.
This expression takes more meaning for objects running on computers that are very
geographically distant, a situation really leading to a physical sending of messages from
one point to another on the globe.
Interactions between objects
Polymorphism
Polymorphism, a direct consequence of inheritance, allows the same message, the
existence of which is expected in a superclass, to be executed differently, depending on
whether the object which receives it is of a subclass or of another.
Thus, the object responsible for sending the message does not have to concern itself in its
code with the ultimate nature of the object which receives it and therefore with the way in
which it will execute it.
Introduction to inheritance and polymorphism