Vous êtes sur la page 1sur 34

Object Oriented Programming 1

Engineering track / 2nd year


2023-2024

 1. Basics

Azeddine Chikh

University of Tlemcen
Objective

 This chapter introduces the basic elements of


object-oriented programming (OOP). These
essentially concern the notions of object, class,
message and inheritance. At this stage, no
technical development is carried out.
General Plan

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

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Trio <entity, attribute, value>

 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

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Storing objects in memory

 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

 The referent of an object

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.

Referent to a unique object


 The name of a computer object, what makes it unique, is also what allows physical access
to it. The information received and contained by this referent is nothing other than the
memory address where this object is stored.
 A referent is a particular computer variable, associated with a symbolic name, coded on 64
bits and containing the physical address of a computer object.
Storing objects in memory

 Several referents for the same object

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

 Several referents for the same object


Storing objects in memory

 Several referents for the same object

 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

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Passive and active version of an object

 Passive version

The object and its constituents


 The OO, for practical reasons, encourages separating, in the description of any object, the
part useful for all the other objects which use it, from the part necessary for its own
functioning. It is necessary to physically separate what other objects must know about a
given object, in order to request its services, from what the latter requires for its operation,
that is to say the implementation of these same services.

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.

Dependence without composition


 The composition between objects is not enough to faithfully describe the reality that
surrounds us. Other modes of connection between objects will have to be considered,
which allow a first to easily connect to a second, but without the existence of the latter
being entirely conditioned by the existence of the first.
Passive and active version of an object

 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

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Introduction to class

 Methods and classes

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.

 The Traffic-light class could be defined as follows:


class Traffic-light
{
int color ;
change() {color = color + 1 ; if (color ==4) color = 1 ;}
}
Introduction to class

 On which specific object does the method execute?

Link the method to the object


 We link the method f(x) to the object a on which it must be applied, by means of an
instruction like a.f(x). By this writing, the method f(x) knows how to access only the
attributes of the object a that it can manipulate.

Object-oriented language and language manipulating objects


 Many programming languages, especially scripts for web development (JavaScript, VB Script), make it
possible to execute methods on objects whose classes pre-exist development. The programmer never
creates new classes but simply executes their methods on objects. We limit ourselves to using them as
we use the libraries of any programming language. The classes grouped in these libraries will have
been developed by other programmers and made available to you. OO programming begins as soon as
we are responsible for creating new classes, even if when writing codes, we rely largely on classes
written by others. It would rather be class-oriented programming. It is possible in Javascript to directly
create an object from another, following a mechanism resembling inheritance and generally called
prototyping. It is also the model for the Io and Self languages, widely recognized as OO.
 Determining which languages can actually claim to be OO has become a rather tricky question to
decide.
Plan

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Interactions between objects

 Interactions between objects


 Let’s consider two objects, traffic-light and car-seen-in-the-street, respective instances of
the Traffic-Light and Car classes. They are each characterized by an attribute: the integer
color for the light and, for the car, an integer speed which can take up to 130 possible
values.
Interactions between objects

 How objects communicate


Method
 This is a grouping of instructions similar to procedures, functions and routines found in all
programming languages, except that a method always executes on a specific object (as if
it was, implicitly, passed as an additional argument).

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

 How objects communicate


Event management
 Instead of sending a fire message to all the cars facing it (and of which it is unaware of the
nature and number), it would undoubtedly be more realistic to consider that the cars are
likely to observe the color transition of the fire and react accordingly without explicitly
receiving the command from the fire.
 This observation and event management mechanism is also a “plus” of OO programming
Plan

 Trio <entity, attribute, value>


 Storing objects in memory
 Passive and active version of an object
 Introduction to class
 Interactions between objects
 Introduction to inheritance and polymorphism
Introduction to inheritance and polymorphism

 From the most general to the most specific


Inheritance and taxonomy
 A key practice of OO is to organize classes together hierarchically or taxonomically, from
the most general to the most specific. We will talk about an “inheritance” mechanism
between classes.
 An object, instance of a class, will be both an instance of this class, but also of all those
which generalize it and from which it inherits. Any other object needing its services will
choose to process it according to the most appropriate hierarchical level.
Introduction to inheritance and polymorphism

 Context dependency of the right taxonomic level


Inheritance
 In our cognition and in our computers, the primary role of inheritance is to promote an
economy of representation and processing. Factoring what is common to several
subclasses into the same superclass offers capital advantages.
 We can omit writing in the definition of all subclasses what they inherit from superclasses.
It is common sense that the fewer instructions we write, the more reliable and easier to
maintain the code will be. If we learn from any class that it is a particular case of a general
class, we can automatically associate with it all the information characterizing the more
general class, without redefining them. Furthermore, this more specific class is only used
in much rarer cases, where it will be essential to exploit the information specific to it.
Introduction to inheritance and polymorphism

 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

 Heritage well received


 Cognitive sciences and artificial intelligence play a large role in the birth of OO computing.
 In cognitive science, this idea of an object is widespread. It reminds us that our knowledge
is not as disorganized as it seems, that blocks appear, making our cognition a set of
islands rather than a uniform ocean, blocks linked together. relationally and taxonomically
 This cognitive structuring reflects, in part, the reality that surrounds us, but above all our
way of perceiving and communicating it, while submitting to universal principles of
economy, simplicity and stability.
References

Vous aimerez peut-être aussi