Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm based mainly on two concepts: class and object. Commonly, OOP is used to separate a program into simple and reusable code segments (classes); these are the basis for creating individual instances called objects.
OOP Principles
Object-oriented programming is based on the following principles:
-
Encapsulation: The implementation and state of each object are kept private within a defined boundary or class. Other objects do not have access to this class or the authority to make changes, but can only call a list of public functions or methods. This data hiding feature provides greater security to the program and prevents unintentional data corruption.
-
Abstraction: Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. This concept helps developers make changes and additions more easily over time.
-
Inheritance: Relationships and subclasses can be assigned between objects, allowing developers to reuse common logic while maintaining a unique hierarchy. This OOP property forces a more complete data analysis, reduces development time, and ensures a higher level of accuracy.
-
Polymorphism: Objects can take on more than one form depending on the context. The program will determine what meaning or use is necessary for each execution of that object, reducing the need to duplicate code.
It is worth noting that in polymorphism there are usually 3 types, I will mention them, but I will write about them in more depth in another post.
- Overloading: The best known and applies when there are functions with the same name in classes that are completely independent of each other.
- Parametric: There are functions with the same name, but different parameters (name or type) are used. The method is selected depending on the type of data sent.
- Inclusion: This is when a method can be called without having to know its type, thus not taking into account the details of specialized classes, using a common interface.
What advantages does OOP have?
OOP is not the only paradigm that exists nor is it proposed to be the best, remember it is always good to consider advantages and disadvantages before using any tool, in this case its advantages are the following:
-
OOP models complex things as simple and reproducible structures
-
Reusable OOP objects can be used in all programs.
-
Allows specific class behavior through polymorphism.
-
Easier to debug, classes often contain all applicable information.
-
Secure, protects information through encapsulation
Some object-oriented programming languages
- C++
- C#
- Fortran
- JavaScript
- Objective-C
- Java
- R
- Perl
- PHP
- Python
- Ruby
- Swift
- Visual Basic
- Scala
- Dart
Hands-on
Very well, we have seen some theory now let's go to the code with a simple example, as mentioned OOP models things. In this case we will make a Person class, as you can see it has the properties name, age and occupation (We will do the example with Java).
class Person {
private String name;
private int age;
private String occupation;
public Person(String name, int age, String occupation) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
public void sayHello() {
System.out.print(
"Hello my name is " + name
+ " I am " + age
+ " years old and I am " + occupation
);
}
}
We will make an object of this class which will be individual, meaning it will have properties of the class but its own values.
Person programmer = new Person("Hugo", 24, "programmer");
programmer.sayHello();
By executing the sayHello method we will get the following output.
Hello my name is Hugo I am 24 years old and I am a programmer
We can have n number of objects each with their own values, for example.
Person dentist = new Person("Karla", 25, "dentist");
Person architect = new Person("Pedro", 30, "architect");
Person barber = new Person("Juan", 22, "barber");
Conclusion
Object-oriented programming requires thinking about the structure of each entity and planning at the beginning of coding. Looking at how to divide requirements into simple and reusable classes that can be used to design object instances. Overall, implementing OOP allows for better data structures and reusability capabilities, saving time in the long run.