Introduction to Object Oriented Programming (OOP): Key Concepts and Examples

Introduction to Object Oriented Programming (OOP): Key Concepts and Examples

In the world of software development, there are different ways to structure code to make it more efficient, modular and maintainable. One of the most widely used approaches is Object Oriented Programming (OOP), a paradigm that is based on the creation and manipulation of objects instead of working only with functions and data structures.

What is Object Oriented Programming?

OOP is a programming model that organizes code into objects, which represent entities with characteristics (attributes) and behaviors (methods). These objects are created from classes, which function as molds or templates.

For example, if we want to represent a car in our code, we can create a class “Car”, where each car will have attributes such as make, model and color, and methods such as accelerate or brake.

In simple terms, OOP allows us to model the real world inside our programs in a more intuitive and structured way.

Main concepts of OOP

1. Classes and Objects

Class: It is a template that defines what characteristics and behaviors the objects created from it will have.

Object: It is an instance of a class with specific values.

Example

image.png

In this code, the class “Car” defines the attributes make and model, and the method start(). Then, we create an object “my_car” and execute its method to simulate that the car is running.

2. Encapsulation

Encapsulation is the principle that restricts direct access to an object's data, allowing it to be manipulated only through methods defined within the class. This protects the integrity of the data and prevents accidental modifications.

Example:

image.png

Here, __balance is a private attribute that can only be modified through the deposit() and get_balance() methods, which ensures the security of the account.

3. Inheritance

Inheritance allows a class (child) to inherit attributes and methods from another class (parent), avoiding code duplication and facilitating reuse.

Example:

image.png

The class “Moto” inherits the attributes and methods of “Vehículo”, but can also add new attributes as a type.

4. Polymorphism

Polymorphism allows different classes to use the same method, but with different implementations.

Example:

image.png

Thanks to polymorphism, we can call the same method make_sound() in different classes regardless of their internal implementation.

5. Abstraction

Abstraction allows to hide the internal implementation details and only expose the essential. It is used to create abstract classes, which define methods to be implemented by child classes.

Example in Python with the abc library:

image.png

Here, Animal is an abstract class and Cat must implement make_sound(), ensuring that each animal has its own version of the method.

Advantages of Object-Oriented Programming

Code Reusability: Inheritance allows reusing already defined classes.

Modularity and Maintainability: Each object has its own independent logic, which facilitates debugging and maintenance.

Scalability: New classes and functions can be added without affecting the existing code.

Real World Modeling: OOP allows to better represent real world objects and relationships in software.

Object Oriented Programming (OOP) is a powerful approach that helps to better structure code, making it more organized and reusable. By understanding fundamental principles such as classes, objects, encapsulation, inheritance, polymorphism and abstraction, developers can write more efficient and scalable programs.

Whether you are learning to program or want to improve your skills, mastering OOP is an essential step to working on professional software development projects. 

Want to keep learning about programming and technology? Follow us at TM Consulting for more content!

LinkedIn | X | Facebook | Web

Ready for the next step?

Tell us about your challenge and we will build a clear roadmap with you: assessment, proposal and measurable results.

Contact us