Java Fundamentals: object-oriented principles
Practical explanation of the main OOP concepts, such as abstraction, encapsulation, inheritance, polymorphism, association, composition, and modularization.
Object orientation becomes clearer when we understand its concepts in practice, not just as loose definitions.
Object orientation in practice
Object orientation is not just theory.
It is a paradigm that can be applied in practice through languages such as Java, C#, Python, Kotlin, and others.
Object-Oriented Programming, also known as OOP, works with some fundamental concepts, such as:
- abstraction;
- encapsulation;
- inheritance;
- polymorphism.
Beyond these classic pillars, there are other important concepts for better organizing a system, such as:
- modularization;
- hierarchy;
- association;
- composition.
Abstraction
Abstraction is the ability to observe a real-world entity, identify its important characteristics and behaviors, and represent them inside a system.
In abstraction, we don’t bring everything from the real world into the code.
We bring only what is needed to solve the problem.
Imagine a vehicle registration system.
In that case, it may make sense to represent information such as:
- number of doors;
- engine;
- model;
- brand;
- year.
This information is extracted from the real world and represented in the system.
Another example would be a college system.
To represent a student, information such as the following may be needed:
- name;
- student ID;
- course;
- class;
- subjects.
Abstraction consists of choosing what really matters for that system.
Encapsulation
Encapsulation is the principle of protecting an object’s internal data and behavior, allowing access and modification only in a controlled way.
As the word itself suggests, to encapsulate is to protect something inside a structure.
A simple example is a blender.
A blender has a motor, wires, and internal mechanisms. Yet, to use it, a person doesn’t need to open the appliance or understand every internal detail.
They interact only with the available buttons, such as:
- on;
- off;
- speed 1;
- speed 2;
- pulse.
In object orientation, something similar happens.
The object protects its internal details and exposes only the methods needed for other parts of the system to interact with it.
In Java, this is normally done with access modifiers such as private, public, and protected.
Inheritance and hierarchy
Hierarchy is the organization between classes that have some kind of relationship with each other.
One of the best-known forms of hierarchy in OOP is inheritance.
Inheritance happens when a class inherits characteristics and behaviors from another class.
To evaluate whether the relationship should be inheritance, one question helps a lot:
Is a student a person, or does a student have a person?
In this case, it makes sense to say that a student is a person.
So an Aluno class can inherit characteristics from a Pessoa class.
For example, a person may have:
- name;
- CPF;
- date of birth.
Since a student is also a person, they can reuse these characteristics.
In addition, Aluno may have its own information, such as:
- student ID;
- course;
- class.
This is a relationship of the “is a” type.
In other words:
A Student is a Person.
Association
Not every relationship between classes should be done with inheritance.
Sometimes, a class merely uses or holds a reference to another.
Another question helps tell the difference:
Is the student a subject, or does the student have a subject?
A student is not a subject.
A student has a subject, or several subjects.
So this is not inheritance.
In this case, the Aluno class can hold a reference to Disciplina.
This relationship is called association.
In other words:
A Student has a Subject.
In a more realistic scenario:
A Student has several Subjects.
Composition
Composition is a stronger form of association.
It happens when a class is formed by another and that part normally belongs to the main object in that context.
A simple example is an Pedido with its ItemPedido items.
An order can have several items.
These items make sense within that order, because they represent the chosen products, quantities, and prices at that moment of the purchase.
The relationship is not:
An Order is an Order Item.
The correct relationship is:
An Order has order items.
Composition helps represent objects formed by internal parts.
Polymorphism
Polymorphism is the ability of different objects to respond to the same action in different ways.
Imagine an Animal class with an emitirSom() method.
A Cachorro class can implement this method by emitting a bark.
A Gato class can implement the same method by emitting a meow.
The action is the same:
emitirSom()
But the behavior changes according to the object.
This allows writing more flexible code, because different objects can follow the same general idea while executing specific behaviors.
Modularization
Modularization is the idea of dividing a large system into smaller parts that are more organized and easier to maintain.
In Object-Oriented Programming, this division happens mainly through classes.
Each class should represent an important responsibility, entity, or concept of the system.
In a college system, for example, we could have classes such as:
Aluno;Professor;Disciplina;Curso;Turma.
Each of these classes represents a part of the system.
This leaves the code more organized, makes maintenance easier, and improves reuse.
Summary
Object Orientation allows representing real-world problems inside code using objects, classes, and relationships between entities.
The best-known pillars of OOP are:
- abstraction;
- encapsulation;
- inheritance;
- polymorphism.
Beyond them, concepts such as modularization, hierarchy, association, and composition help structure the system better.
The main idea is creating code that is more organized, reusable, protected, and easy to maintain.