Object Oriented Programming - OOP

Object-oriented programming is a paradigm. A paradigm is an idea of ​​how to look and make things.

OOP is the way of looking at problems as a set of objects that interact to perform the tasks a program has to carry out.

Why are we talking about "programming paradigms"?

The biggest problem in programming is organizing a program so that it is easy to understand. If the program is well organized, it will be easier to correct and extend.

The solution is to divide the problem into more manageable subproblems. This creates a new problem: how do the solutions of the subproblems interact to achieve the final solution.

Now the dilemma boils down to how to divide the problem to reduce the conflict between the parties. The first step in this direction was the structured programming paradigm.

Structured Programming Paradigm

The idea of structured programming, is to eliminate the use of jumps (eg: instruction GOTO). These jumps create a "spaghetti code", difficult to understand. It is based on:

  1. Subroutines: to split the program into smaller subprograms.
  2. Blocks: ordered sequence of instructions.
  3. Selections: instructions to change the course of the program. Eg; "if", "switch" or "case" statement.
  4. Repetition: instructions which allow us to repeat until we reach a condition or we have gone through a collection. Eg; "for" and "while" statement.

What makes this paradigm powerful is the code division into subroutines or subprograms. This code encapsulation allows us to modify code without touching the code of another subprogram.

The weakest part of this paradigm is that it only divides code, it does not divide program data.

Object Oriented Programming Paradigm

This paradigm divides the program in objects.

If the program is about animals, we can have dogs and cats objects. If the program is about geometric figures, we will have triangles, squares and circles objects.

Each object encapsulates the data and the code related to the object.

In the case of the triangle, the length of the sides of the triangle would be data object and the instructions for calculating the area, would be the code.

Each object is specialized in something and is responsible for managing and protecting the data of the subject in which it is specialized.

Object fields or instance variables

The state of an object, or data, is stored in fields or variables known as instance variables of the object. It is recommended that these fields are only accessed from within the object.

Object methods

The ability of an object to do things, is given by the methods. Methods are procedures or subroutines associated with the object. These methods have free access to the object fields. OOP recommends that fields are only accessed in this way and not directly from another object.

Encapsulation and information hiding

The idea of ​​creating an object, is to hide information and complexity. The rest of the program will use the services offered by its methods, without knowing how it does it.

To access the fields of an object from outside, it can be done indirectly using object methods. In this way, we can modify the fields and the code of the method of our object, without changing in any way the multiple objects which are using the data through the method.

Imagine we have a triangle object with a field "area" and a "calculate_area" method. The rest of the code will access the area, using the method "calculate_area". For some reason, we decide we don´t need the field area and we delete it. The "calculate_area" method can calculate the area using the sides of the triangle.

If we allow other objects to use the field area, we would have to search for them and modify them. If we follow the recommendation and use the "calculate_area" method, we are not going to have to do this, because the modification will not affect them.

Classes, instances, prototypes and inheritance.

The idea of ​​object-oriented programming is to handle families of objects of the same type (eg: object type dog, cat, etc..). To create objects or instances of each family there are two methods: Classes or Prototypes. We will learn about this and Inheritance in the next tutorial: Classes, instances, prototypes and inheritance in OPP.