What is Object Oriented Programming (OOP)?
If you are starting into the programming world, and want to understand how Object Oriented Programming (OOP) works in practice, this guide is for you. Learn how to create a shopping cart object in Java and see how concepts such as classes, instances, variables and methods apply in everyday programming!
PRIMEIROS PASSOS TITECH
Laura Oliveira
7/24/20244 min read


If you're just starting out in programming, chances are you've heard about object orientation (OO). This paradigm is fundamental to many types of software development, and is widely used in modern languages, including Java.
But what exactly is object orientation?
According to Kurt Nørmark, "Object-oriented programming (also known by its acronym OOP) is a software analysis, design and programming model based on the composition and interaction between several units called objects."
In short, object orientation is a way of modeling real-world concepts in the virtual world using entities called objects.
To understand better, let's use an analogy with the movie "The Matrix." In the film, the character Neo has a virtual representation of his body inside the Matrix, which reflects his physical characteristics and behaviors. In this analogy, his virtual body would be the object, with physical characteristics corresponding to variables and behaviors to methods.
This representation is fundamental in OOP, as it allows developers to create programs that simulate the behavior of real entities. By using objects, programmers can create complex systems in an organized and scalable way.
Creating an Object: The Shopping Cart
Let's apply these concepts by creating a practical example: a shopping cart in Java. To do this, we will start by defining a class that will act as a "template" for the objects we will create.
Defining the Shopping Cart Class
The `CarrinhoCompras` class in Java defines the characteristics (or properties) and behaviors (or methods) of a shopping cart. The class is essentially a blueprint for the object, specifying everything it can do and how it is structured.
In the code above, the `CarrinhoCompras` class has three attributes: `tipoProduto`, `quantidadeProduto`, and `totalCompra`. Additionally, there is a `calcularSubTotal` method that calculates the total purchase based on the quantity of products and their unit price, determined by another method, `precoUnitario`.
Instantiating the Class: Creating the Object
To "bring life" to this class, we need to instantiate it, that is, create an object from this class. This is done in the program's main class, usually called `main`.
On line 54 where we write `CarrinhoCompras cart = new CarrinhoCompras();`, we are creating a new object of type `CarrinhoCompras`. After creating the object, we assign values to its variables: `tipoProduto`, `quantidadeProduto`, and then call the method `calcularSubTotal` to calculate the total value of the purchase.
Understanding OOP Concepts
The main concepts of OOP include classes, objects, attributes, methods, encapsulation, inheritance and polymorphism.
Classes and Objects: A class is a structure that defines the attributes and methods that an object can have. An object is an instance of a class.
Attributes and Methods: Attributes are the properties of an object (such as producttype), while methods are the functions or actions that an object can perform (such as calculateSubTotal).
Encapsulation: This concept refers to restricting direct access to some components of an object, protecting data integrity and avoiding external interference.
Inheritance: Allows one class to inherit the attributes and methods of another, promoting code reuse.
Polymorphism: Refers to the ability to use a single interface to represent different types of objects.
Benefits of OOP
OOP Advantages
Code Organization: Helps organize code in a logical manner, grouping data and functions that operate on that data into a single unit (object).
Code Reuse: With inheritance, it is possible to create new classes that reuse the code of existing classes, reducing redundancy.
Ease of Maintenance: Encapsulation makes it easier to maintain and modify the code without affecting other parts of the system.
Scalability: OOP allows the construction of more complex systems in a scalable way, as new objects can be created and integrated with relative ease.
Understanding the Process
This example illustrates how OOP allows us to create structures that are easy to understand and manipulate. By defining classes and creating objects, we can represent and work with data in an organized way. This not only makes coding easier, but also maintains and expands the software.
Furthermore, OOP promotes code reuse. For example, once we have a functional `CarrinhoCompras` class, we can use it in different parts of a program, or even in other programs, without having to rewrite the code.
Conclusion
Congratulations! You have just created your first object in Java using object-oriented concepts. Understanding and applying these concepts is crucial for any developer who wants to create robust and scalable software. As you progress, you will continue to encounter new patterns and practices that will help refine your OOP skills.
Remember that practice is key to becoming proficient in programming. Keep exploring, experimenting, and expanding your knowledge, and soon you'll be creating complex, effective systems with ease.






Laura Oliveira
Mobile Developer and Content Creator
Fascinated by technology since she was a teenager, she loves solving problems and learning new things.
I currently work bringing ideas to life for Android apps.
I have +2 million people impacted through the production of digital content.


2M+
3
Years Creating Content
Content Reach