Object Oriented Programming (OOP) is a programming paradigm, that is, a programming model or style that provides guidelines on how to work with it and is based on the concept of classes and objects. This type of programming is used to structure a software program in simple and reusable pieces of code planes (classes) to create individual instances of objects.
Historically, different programming paradigms have emerged. On the one hand, sequential languages such as COBOL or procedural languages such as Basic or C focus more on logic than on data. On the other hand, more modern languages such as Java, C# and Python use paradigms to define programs, with OOP being the most popular.
With the OOP paradigm, the goal is to stop focusing on the pure logic of programs and start thinking about objects, which forms the basis of this paradigm. This helps a lot in large systems, because instead of thinking in functions, one thinks in the relations or interactions of the different elements of the system.
A programmer designs a software program by organizing pieces of information and related behaviors into a template called a class. Then, individual objects are created from the class template. The entire software program is executed by having various objects interact with each other to create a larger program.
Why Object Oriented Programming?
OOP allows code to be reusable, organized and easy to maintain. It follows the software development principle used by many programmers DRY (Don't Repeat Yourself), so as not to duplicate code and to create efficient programs. It also prevents unwanted access to data or exposure of proprietary code through encapsulation and abstraction.
Classes, objects and instances
In short, to create OOP, classes are made and then objects are created from those classes, which form the model from which data and behaviors are structured. Perhaps the most important concept is to distinguish between class and object.
A class is a template, which defines in a generic way how objects of a certain type will be. For example, a class to represent animals can be called 'animal' and have a series of attributes, such as 'name' or 'age' (which are usually properties), and a series of behaviors that they can have, such as walking or eating, which in turn are implemented as methods of the class (functions).
A simple example of an object could be an animal. An animal has an age, so a new 'age' attribute is created, and, in addition, it can age, so a new method is defined. Data and logic. This is what is defined in many programs as the definition of a class, which is the global and generic definition of many objects.With the class, instances of an object can be created, each with its attributes defined independently. With this you can create a cat named Pepe, 5 years old, and another animal, this type dog and named Lucho, with an age of 3 years. Both are defined by the animal class, but they are two different instances. Thus, calling their methods can have different results. The two share logic, but each has its state independently.All these are tools that help to write better, cleaner and reusable code.
4 principles of Object Oriented Programming
There are four principles of OOP.
Encapsulation presents all the important information of an object inside the object and only exposes the chosen information to the outside world.
This property ensures that the information of an object is hidden from the outside world by grouping in a class the characteristics or attributes that have private access, and the behaviors or methods that have public access.
The encapsulation of each object is responsible for both its information and its state. The only way in which this can be modified is through the object's own methods. In this way, the internal attributes of an object are inaccessible from the outside, and can only be modified by calling the corresponding functions. In this way, the state is kept safe from improper or unexpected uses.
To explain encapsulation we will use a car as an example. The car shares public information through the brake lights or turn signals to indicate turns (public interface). By contrast, the internal interface, the driving mechanism of the car, is hidden under the hood. When driving a car, it is necessary to indicate movements to other drivers, but not to expose private data about fuel type or engine temperature, as this is a lot of data, which would confuse other drivers.
Another principle of Object Oriented Programming is abstraction, which occurs when the user interacts only with selected attributes and methods of an object, using simplified high-level tools to access a complex object.
In OOP, programs are usually very large and objects communicate quite a bit with each other. Thus, abstraction facilitates the maintenance of a large code, where various changes may occur over time.
Thus, abstraction is based on using simple things to represent complexity. Objects and classes represent underlying code, hiding complex details from the user. Thus, it is an extension of encapsulation. Continuing with the previous example, you don't need to know all the details about how a car engine works in order to drive it.
Inheritance defines hierarchical relationships between classes, so that common attributes and methods can be reused. By defining basic attributes and behaviors in a class, secondary classes can be created, extending the functionality of the main class and adding extra attributes and behaviors. This is one of the keys to Object Oriented Programming.
Following the animal example, a single animal class can be used and an animal type attribute can be added that specifies the type of animal. Different types of animals will require different methods, for example, reptiles must be able to lay eggs and fish must be able to swim. Even if animals have a common method, such as moving, the implementation would require many "if" statements to ensure the ideal movement behavior. For example, frogs jump, while snakes slither. The inheritance principle solves this problem.
Polymorphism lies in designing objects to share behaviors, which allows objects to be processed in different ways. It is the ability to present the same interface for different underlying ways or data types. By using inheritance, objects can override shared primary behaviors with specific secondary behaviors. Polymorphism allows the same method to execute different behaviors in two ways: method overriding and method overloading.
Benefits of Object Oriented Programming
● Code reuse.
● Converts complex things into simple reproducible structures.
● Avoids duplication of code.
● Allows teamwork thanks to encapsulation, since it minimizes the possibility of duplicating functions when different people work on the same object at the same time.
● As the class is well structured, it allows error correction in various places in the code.
● It protects information through encapsulation, since the object data can only be accessed through private properties and methods.
- Abstraction allows us to build more complex systems in a simpler and more organized way.
Today, OOP is the most widely used paradigm for designing applications and software. Its advantages are obvious, especially when complex programming challenges need to be solved. It also allows for better data structure and code reuse, which saves time in the long run.
However, this requires thinking through the program structure, planning at the beginning of coding, as well as analyzing the requirements in simple and reusable classes that can be used to design object instances.
And you, do you use Object Oriented Programming in your daily life? If you have any questions, Intelequia's team of professionals will be happy to answer them.
Write to Us!