PureBasic and the Object-Oriented
Programming
The Object-Oriented Programming introduces concepts as the object, the
inheritance or the polymorphism.
Before to see how the main concepts can be realized in PureBasic, concepts
must be defined first.
The notion of Object
An object has an internal state:
- The state is represented by the value of each inner components at
a given moment,
- A component is either a value or another object.
It uses and gives services:
- It interacts with the outside through functions called methods.
It is unique (notion of identity)
An object can be seen at two levels:
- By the services which it returns: external view (specification). It
is User side,
- By the way that services are implemented within it : internal view
(implementation). It is the Developer side.
From the point of view of the developer, the object is thus a contiguous
memory area containing information: variables called attributes
and functions called methods.
The fact that functions are called methods of an object, means the fact
that they are appropriate to the object and allow the manipulation of
the attributes of this object.
The notion of Class
It is about an extension of the type notion found in PureBasic.
In a given context, several objects can have the same structure and the
same behavior.
Then, they can by grouped together in the same "Class".
From the point of view of the developer, the Class defines the contain
of an object of this Class: the nature of its attributes (type of each
variable) and its methods (names, implementation).
If the type of a variable is an integer, the type of an object is its
Class.
The notion of instance
An instance is an object defined from a Class.
Such a process is called the instanciation.
It corresponds to the assignement of variables in PureBasic.
The object is normally initialized at this time.
The encapsulation
In theory, the manipulation of the object attributes should be able only
through the methods. This technique, which allows making visible to the
user only a part of the object, is called encapsulation.
The encapsulation advantage is to guarantee the integrity of the attributes.
Indeed, the developer is the only one who, through the methods given to
the user, manages the modifications allowed to an object.
At our level, it is at least what it shall be retain about encapsulation
concept.
The inheritance
The inheritance allows defining new Classes by using already existing
Classes.
From the point of view of the developer, it means being able to add attributes,
methods and even to modify some methods, to an existing Class in order
to define another one.
There are two kinds of inheritances:
- The simple inheritance: the new Class is defined from a single existing
Class
- The multiple inheritance: the new Class is defined from several existing
Classes
The multiple inheritance is complex to implement and it will not be approached
here.
Thus, the document deals only with the simple inheritance.
Terminology:
The Class which inherits from another Class, is often called Child Class
The Class which gives its inheritance to a Child Class is often called
Mother Class.
The overload
A method is overloaded if it realizes different actions according to the
nature of the aimed objects.
Let us take an example:
The following objects: circle, rectangle and triangle are geometrical
forms.
We can define for these objects the same Class with the given name: Form.
Thus, above objects are instances of the Class Form.
If we want to display objects, the Class Form needs to have a "Draw"
method.
So endowed, every object has a "Draw" method to display themselves.
Now, this method could not be the same, as we want to display a circle
or a rectangle.
The objects of the same Class use the same "Draw" method, but
the object nature (Rectangle, Triangle) specifies the implementation of
the method.
The "Draw" method is overloaded: for the user, displaying a
circle or a rectangle, it is made in the same way.
From the point of view of the developer, the methods implementation needs
to be different.
Instead of overloaded method, we can speak also about polymorph method
(having several forms).
Notion of abstract
Class
As shown above, a Class includes the definition of both the attributes
and the methods of an object.
Let us suppose that we cannot give the implementation of one of the methods
of the Class. The method is only a name without code. We speak then about
abstract method.
A Class containing at least an abstract method is qualified as abstract
Class.
We can wonder the reason to be of an abstract class, because an object
of a such Class cannot be create. Abstract Classes allow defining Object
Classes considered by opposition as concrete. The link between them
is made by inheritance where the concrete Class takes care in giving the
missing implementations to the inherited abstract methods.
Thus, abstract Classes have an interface responsibility, because they
describe the generic specification of all the Classes which inherit from
them.
[1-2-3-4-5-6-7-8-9]
Top of the page
|