What is object oriented programming

What is Object Oriented Programming, a programming paradigm that allows complex applications to be developed while maintaining a clearer and more manageable code than other previous paradigms.

In this article we are going to explain what Object Oriented Programming is, also explaining some of the most important concepts that we must know when we want to program object oriented.

In order to understand this concept, which some of us choke on when learning programming, we must often unlearn previously known concepts, perhaps poorly explained, and free the mind to relearn in the best way. That is why we are going little by little.

What is Object Oriented Programming?

Let’s start with a formal definition, although don’t worry if you don’t understand everything, because later we will go into more detail on each point.

Object-oriented programming is defined as a programming paradigm, a specific way of programming, where the code is organized into units called classes, from which objects are created that relate to each other to achieve the objectives of the applications.

We can understand Object Oriented Programming (OOP) as a special way of programming, closer to how we would express things in real life than other types of programming, which allows applications to be better designed, reaching higher levels of complexity, without the code becomes unmanageable.

When programming object-oriented we have to learn to think about how to solve problems in a different way than it was done before, in structured programming. Now we will have to write our programs in terms of classes, objects, properties, methods and other things that we will see quickly to clarify concepts and give a small base that allows us to loosen up a bit with the concepts of this type of programming.

Object Oriented Programming Motivation

For years, programmers have been building very similar applications that solve the same problems over and over again. In order to ensure that the programmers’ efforts can be reused, the possibility of using modules was created. The first existing module was the function, which we are able to write once and call any number of times.

However, the function is very focused on providing a given functionality, but not as concerned with the data. It is true that the function can receive data as parameters and can return it, but it deals with a very volatile structure, focused on operations. It just does its job, processing the received parameters and returns a response.

In reality, data is closely linked to functionality in applications. For example we can imagine a point that moves across the screen. The point has coordinates and we can move it from one position to another, adding or subtracting values ​​to its coordinates. Before object-oriented programming it happened that each coordinate of the point had to be stored in a different variable (two variables to be exact: x, y) and the translation functions were stored elsewhere. This situation did not facilitate the organization of the code nor its reuse.

See also  Generate reports in PHP web application exportable to PDF

With Object Oriented Programming, the aim was to solve these situations, creating better conditions to be able to develop increasingly complex applications, without the code becoming chaotic. In addition, it was intended to give guidelines for doing things so that other people can use them and advance their work, which leads to greater facilities for code reuse.

OOP is not difficult, but it is a special way of thinking, sometimes subjective to whoever programs it, so the way of doing things can be different depending on the programmer. Although we can do programs in different ways, not all of them are correct, the difficult thing is not object-oriented programming but programming well. And programming well is essential because that way we can take advantage of all the advantages of OOP.

how to think about objects

Thinking in terms of objects is very similar to how we would in real life. For example, we are going to think of a car to try to model it in an OOP scheme.

We would say that the car is the main element that has a series of characteristics, such as color, model or brand. It also has a series of associated functionalities, such as starting, stopping or parking.

Therefore, thinking about objects requires analyzing what elements you are going to handle in your programs, trying to identify their characteristics and functionalities. Once we have an ecosystem of objects, they will collaborate with each other to solve the objectives of the applications.

Perhaps at first it may be a bit complex to make that leap, to think about objects, but over time it will be a task that you will carry out automatically.

Managing the class concept

In an object-oriented programming scheme “the car” would be what is known as “Class”. The class contains the definition of the characteristics of a model (the car), such as the color or the brand, together with the implementation of its functionalities, such as starting or stopping.

The characteristics defined in the class we call properties and the associated functionalities we call methods.

To understand this very important concept within Object Oriented Programming, we can think that the class is like a book, which describes how all the objects of the same type are. The car class describes how all the cars in the world are, that is, what properties they have and what functionalities they must be able to perform and, of course, how they are performed.

See also  Where is my php.ini?

Managing the concept of objects

From a class we can create any number of objects of that class. For example, from the class “the car” we can create a red car that is a Ford make and a Fiesta model, and another green that is a Seat make and an Ibiza model.

Therefore, objects are instances of a class, or concrete elements created from a class. You can understand the class as the mold and the objects as concretions created from the class mold.

Other concrete examples of classes and objects

To give another example, we are going to see how we would model a fraction in an OOP scheme, that is, that mathematical structure that has a numerator and a denominator that divides the numerator, for example 3/2.

The fraction will be the class and will have two properties, the numerator and the denominator. Then it could have various methods like simplifying, adding with another fraction or number, subtracting with another fraction, etc.

From the definition of a fraction (the class) we can build an indeterminate number of objects of type fraction. For example we can have the fraction object 2/5 or 3/9, 4/3, etc. All of those are objects of class fraction of integers.

We could use our fraction class in any number of programs, for example in a math program it will make use of the fraction class and build many objects of type fraction to do various calculations. But you could use that same fraction class in an accounting or billing program.

Another example could be the coordinate class, which has two properties, the x and y value. You could have another class “3D coordinate” which would need 3 x, y and z properties. The coordinates could be added to another coordinate, displayed on a graph, find the shortest path between two coordinates, etc.

These are just simple examples of classes, which also serve to highlight the reusability we can achieve with objects. The coordinate class can be used in an infinite number of graphics programs, maps, etc. For its part, the car class can be used in a car workshop management program or in a parking management program. Starting from the car class, they will create various car-type objects to carry out the operations both in the workshop application and in the parking application.

Objects collaborate with each other

In purely object-oriented languages, we will only have classes and objects. The classes will allow an indeterminate number of objects to be defined, which collaborate among themselves to solve problems.

With many objects of different classes we will be able to carry out the actions that we want to implement in the functionality of the application.

See also  Main features of OsCommerce

In addition, the applications themselves as a whole will also be defined by means of classes. That is, the car workshop will be a class, from which we can create the car workshop object, which will use car objects, tool class objects, mechanic class objects, spare part class objects, etc.

To continue we are going to define in a more formal way the concepts that we have just introduced previously.

Lessons in Object Oriented Programming

As you may have understood, classes are object declarations, they could also be defined as object abstractions. This means that the definition of an object is the class. When we program an object and define its characteristics and functionalities, what we are actually doing is programming a class. In the previous examples we were actually talking about the car or fraction classes because we were only defining, albeit above, their shapes.

properties in classes

Properties or attributes are the characteristics of objects or instances of a class. When we define a property we usually specify its name and its type.

Although it is not a very correct way of speaking, we can get used to the idea that the properties are something like variables where we will store the data related to the objects.

methods in classes

They are the functionalities associated with the objects, which are implemented or programmed within the classes. That is, when we are programming the classes, we call the functions that we create inside associated with those classes methods.

Although methods are like functions, it’s important that you call them methods to make it clear that they are functions that exist within the context of a class, functions that we can call on all objects created from a class.

Objects in Object Oriented Programming

Objects are instances of a class. From a class I can create instances (objects) of that class, which will therefore have the characteristics and functionalities defined in that class.

When we create an instance we have to specify the class from which it will be created. This action of creating an object from a class is called instantiating (which comes from a mistranslation of the word instace which means instance in English). For example, an object of class fraction is for example 3/5. The concept or definition of a fraction would be the class, but when we are already talking about a specific fraction 4/7, 8/1000, or any other, we call it an object.

To create an object you have…

Loading Facebook Comments ...
Loading Disqus Comments ...