Basic object oriented programming concepts

Basic object oriented programming concepts

Object oriented programming (OOP) is a special approach to coding. To understand what OOP is and why it is needed, it will be of use to recall some facts from the history of the development of information technologies. The first programs were entered into the computer using the switches on the front panel of the computer — at that moment computers occupied entire rooms.

Object oriented programming (OOP) is a special approach to coding. To understand what OOP is and why it is needed, it will be of use to recall some facts from the history of the development of information technologies. The first programs were entered into the computer using the switches on the front panel of the computer — at that moment computers occupied entire rooms.

This programming method was extremely inefficient - it could take several hours and sometimes a whole working day to connect cables and install the switches. The calculations themselves took just a few minutes. If one of the programmers (a group of employees worked on such computers) connected the cable or installed a switch incorrectly, then everything had to be crosschecked. In fact, the whole process began again.

Later, punch cards appeared. The program, i.e., a sequence of actions that the computer had to perform, was drawn on a punch card. The user of a calculating machine (at that time the computers were called this way) coded, the operator “drew” the program on the punch card, which was transferred to the operator of the computing department. After a while, the operator returned to the user the result of the program - a roll of paper with the results of calculations. There were no monitors then, and everything that the computer produced was printed on paper. Clearly, if an error was made in the calculations (from the user’s side, as computers do not mistake - they do exactly what is programmed), then the whole chain of actions (programmer, punch card operator, computer operator, check of results) was repeated anew.

The next stage in programming is the appearance of the Assembly language. This programming language allowed writing quite long programs for that time. But Assembly is a programming language of low level, i.e., all operations are performed at the hardware level. Let’s consider the simplest example. Now to perform an elementary action on PHP, for example, the addition of numbers, it suffices to write:

$result = 2+5;

To do the same operation  in Assembly language, you need to perform at least three actions — to load the first number (MOV command) into one of the registers, to load the second number into another register (again MOV command), and to sum up the registers by ADD command. The result will be placed in the third register. The names of the registers cannot be specified here, since they depend on the processor architecture, and this is one more problem of Assembly. If you need to transfer the program to a computer with a different architecture, then you will have to rewrite everything taking into account the features of the target architecture.

The requirements for software products and terms of their development grew exponentially. That is why high-level programming languages appeared. High-level language allows to write programs without thinking about the architecture of your processor. No, this does not mean that you can write a program in any high-level language that will work on a processor with any architecture in the end. Just when writing a program, it is unnecessary to know the processor architecture. You write a command like this:

$a = s + e;

and you do not need to think about where exactly the registers (or just RAM) store the values ​​assigned to the variables s and e. You also do not know where the value of the variable a will be placed. You just know that you can address it by the name a.

The first high-level language is FORTRAN (FORmula TRANslator).

The next stage is the appearance of structured programming. The problem is that the programs in the high-level language grew in size very quickly, which made them in fact unreadable due to the absence of any structure of the program itself. The structural programming implies the presence of a program structure and program blocks, as well as the rejection of unconditional transfer instructions (GOTO, JMP).

After allocating the program structure, it became necessary to create sub-programs which significantly shortened its code. It is much easier to write once the code for calculating a formula and form it as a function. Then to calculate 10 results by this formula, you will need to use this function 10 times, rather than writing the same code 10 times. This new class in programming was called procedural.

In time the procedural programming suffered the same fate as structural. The programs became so large that it was inconvenient to read them. A new approach to programming was necessary. This was object-oriented programming (OOP). OOP is based on three principles - encapsulation, polymorphism, inheritance. Let's find out what is what.

With encapsulation you can combine data and their processing code. Encapsulation protects both code and data from outside interference. The basic concept of OOP is a class. Roughly speaking, a class is a peculiar type of a variable. A class instance (a class variable type) is called an object. In turn, an object is a collection of data (properties) and functions (methods) for their processing. Data and processing methods are called class members.

Thus, an object is the result of encapsulation, since it includes both data and their processing code. Then you will understand how it works. In the meantime, imagine that an object is a bag packed according to the principle "all that is mine, I carry with me".

Class members are private and public. Public class members are available for other parts of the program that are not a part of the object. Private members are available only to the methods of the object itself.

Now let's speak about polymorphism. If you programmed in C language (common, not C ++), then you are probably familiar with the functions abs(), fabs(), labs(). All of them calculate the absolute value of a number, but each function is used for its own data type. If C supported polymorphism, then it would be possible to create one function abs(), but declare it three times for each data type, and the compiler has chosen the right option for the function, depending on the data type transferred to it. This practice is called reloading of functions. Reloading of functions greatly facilitates the work of the programmer. You need to remember several times fewer function names for writing a program.

Polymorphism allows to manipulate objects by creating a standard interface for similar actions.

It remains to talk about inheritance. One object can acquire the properties of another object using inheritance. Note that inheritance is not a copy of an object. When copying, an exact copy of the object is created, and during inheritance, this copy is supplemented with unique properties (new members). Inheritance can be compared with the birth of a child, when a new person inherits the "properties" of the parents, but at the same time is not an exact copy of one of the parents.

All the above was true for any full object-oriented programming language. The exception is JavaScript, where OOP support is rather limited. For example, there are no closed (private) or public properties and methods. All properties and methods are public. The creation of a class comes to a constructor function creation. There is no and will be no a full support for OOP in Javascript. But do not forget that this is not Java, but simply a scripting language for browser.