In this chapter, we will see an introduction to what Object-Oriented Programming (OOP) is and how it differs from other programming paradigms.
Our journey will begin with a historical retrospective, then we will advance through its fundamental concepts such as classes and objects (we will see them in detail later), and conclude with the advantages offered by OOP.
# History of OOP
OOP did not emerge overnight; it was the result of a continuous effort by many programmers to make software more manageable, reliable, and adaptable.
The first paradigm adopted was structured programming, discovered by Dijkstra, who demonstrated that the use of unconditional jumps (goto instructions) is bad for the structure of the program. Subsequently, he replaced those jumps with if/else and while/do while/until instructions.
In the late 60s and early 70s, languages like Simula and Smalltalk introduced the concepts we consider the pillars of OOP today.
This was a paradigm shift, moving from seeing software as a sequence of procedures (structured programming) to seeing it as a set of entities that interact with each other, a way to model the real world.
Many use 3 words to define OOP: encapsulation, inheritance, and polymorphism, terms we will see later.
# Concepts of Class and Object
To understand OOP, we must start with its basic components: classes and objects.
Class
A class is a model, like a blueprint or a template. It is an abstract definition, describing the characteristics and the behaviors of something.
For example, if we consider a "Car" class, it could have characteristics (attributes) such as color, brand, and model, and behaviors (methods) such as starting or braking.
Object
An object is a concrete instance of a class.
Continuing with our example, an object would be a specific car, such as a blue 2020 Toyota Corolla.
It has the attributes and behaviors defined by the class, but with concrete values and states.
# Advantages of OOP
OOP offers several significant advantages over other programming paradigms:
-
Modularity: Code is organized into classes, facilitating its understanding, development, and maintenance.
-
Reusability: Classes can be reused in different parts of an application or in future projects.
-
Extensibility: It's easy to add new features or modify existing ones without altering the system.
-
Abstraction: Allows focusing on what an object does, rather than how it does it, hiding complicated details and showing only the essential.
-
Polymorphism: Objects of different classes can be treated as objects of a common class.
# Practical Example
In this example, we are going to compare the Structured approach and OOP.
We must build a system to manage vehicles in a mechanic shop.
In a structured approach, we might have separate functions for each operation, such as "repairCar", "paintCar", and so on.
Each function needs to know about the car, which can lead to repetitive and hard-to-maintain code.
// Car attributes
String brand = "Toyota";
String model = "Corolla";
String color = "Blue";
// Functions to operate on the car
void repairCar(String brand, String model) {
System.out.println("Repairing " + brand + " " + model);
}
void paintCar(String brand, String model, String color) {
System.out.println("Painting " + brand + " " + model + " in color " + color);
}
// Function calls
repairCar(brand, model);
paintCar(brand, model, color);
With the OOP paradigm, defining a "Car" class, with methods like "repair" and "paint", each car is an object, with its properties (its state) and its operations.
The code to handle a car remains organized and encapsulated within the class.
class Car {
String brand;
String model;
String color;
// Constructor
public Car(String brand, String model, String color) {
this.brand = brand;
this.model = model;
this.color = color;
}
// Method to repair the car
void repair() {
System.out.println("Repairing " + brand + " " + model);
}
// Method to paint the car
void paint(String newColor) {
this.color = newColor;
System.out.println("Painting " + brand + " " + model + " in color " + newColor);
}
}
// Using the Car class
Car myCar = new Car("Toyota", "Corolla", "Blue");
myCar.repair();
myCar.paint("Red");
# Conclusions
OOP is a powerful tool for every software developer. It offers a more intuitive and flexible way to think about how code and data are organized and related to each other.
Throughout this series of chapters, we will explore each of these concepts more deeply and how to apply them in Java, so you can write effective and elegant software.