Understanding Java as an Object-Oriented Language

In the real world, we naturally perceive our surroundings as objects, each with unique properties and behaviors. For example, a car has attributes like color, brand, and speed, and it can perform actions like accelerating or braking. Object-Oriented Programming (OOP) mirrors this way of thinking by organizing software into objects that combine data (attributes) and functionality (methods).
This approach simplifies complex problems, enabling developers to model the software in a way that reflects natural relationships. It fosters reusability, modularity, and clarity in code, making OOP an essential paradigm for designing robust software systems. Java’s implementation of classes and objects allows us to define templates (classes) for creating objects that represent real-world entities.
Example:A Car class can have attributes like color, brand, and maxSpeed and behaviors like start() or stop(). This organizational style keeps the code intuitive, allowing programmers to think in terms of tangible objects instead of abstract machine instructions.Classes and objects in JAVA: Comparison of objects from nature with software objects
Figure 1: Classes and objects in JAVA: Comparison of objects from nature with software objects
Object-oriented programming organizes software to reflect real-world structures as shown on figure 1. Instead of focusing on machine-level instructions, we work with "objects" that represent entities with specific properties and behaviors.
What is an Object?An object can be tangible (e.g., a pencil, keyboard) or intangible (e.g., a bank account). Objects share three key characteristics:Classes define the shared structure and behavior of similar objects. For example, the "Account" class outlines the properties and actions of all bank accounts in a system.Figure 1 illustrates the connection between objects in nature and software objects. The part of the picture on the left shows some properties for 4 objects: a tree, two cars and an airplane. In the part of the picture on the right, the software interpretation of those objects is shown. What is characteristic is that the cars marked as car1 and car2 are two different objects that have their own part of reserved memory (see the right part of the image below), but they have common properties, so we say that they belong to the same class. The class is defined only once, regardless of whether there are more objects that we can classify under a car. We can say that the objects car1 and car2 belong to the same class.Object-oriented programming models real-world entities as objects with state and behavior. For instance, a bank account has properties like balance and account holder, and methods like deposit or withdraw. These objects are defined by their classes.
En example:class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

 public void deposit(double amount) {
      // Adds the specified amount to the account balance.
        balance += amount;
    }

public void withdraw(double amount) {
if (amount <= balance) {
            balance -= amount;
        }
    }

public double getBalance() {
return balance;
    }
}
​In this example, BankAccount is a class that defines the state (balance) and behavior (deposit, withdraw) of the account.
In the main class, the bankAccount object is created and used as follows​:
public class Main {
public static void main(String[] args) {
        BankAccount account = new BankAccount(500);
        account.deposit(200);
        account.withdraw(100);
        System.out.println("Current balance: " + account.getBalance());
    }
}

Concept of an Object

Objects are the fundamental building blocks in object-oriented programming. They represent tangible or intangible entities from the real world, characterized by their unique identity, state, and behavior. These three attributes form the core of how objects operate within software systems.
1. Identity: Identity refers to the uniqueness of an object, distinguishing it from all others, even if they share the same attributes. It represents the specific instance of an object in memory.
Example: Imagine two cars with identical properties (e.g., brand, color, speed). Despite their similarity, they are distinct objects because they occupy separate memory locations and have unique identifiers.​Car car1 = new Car("Red", "Toyota", 180);
Car car2 = new Car("Red", "Toyota", 180);
// car1 and car2 are two distinct objects
2. StateThe state of an object is defined by its attributes and their current values. It represents the data that the object holds at a specific point in time.
Example: A car object may have attributes such as color, brand, and maximum speed, representing its state.class Car {
    String color;
    String brand;
int maxSpeed;

    Car(String color, String brand, int maxSpeed) {
this.color = color;
this.brand = brand;
this.maxSpeed = maxSpeed;
    }
}
Car car = new Car("Blue", "BMW", 240);
System.out.println("State of car: " + car.color + ", " + car.brand + ", " + car.maxSpeed + " km/h");
3. BehaviorBehavior defines what an object can do or how it reacts to certain actions. This is implemented as methods within the class.
Example: A car object may have methods like accelerate() or brake(), which define its behavior.class Car {
    String color;
    String brand;
int maxSpeed;

    Car(String color, String brand, int maxSpeed) {
this.color = color;
this.brand = brand;
this.maxSpeed = maxSpeed;
    }

void accelerate() {
        System.out.println(brand + " is accelerating.");
    }

void brake() {
        System.out.println(brand + " is braking.");
    }
}
Car car = new Car("Red", "Honda", 200);
car.accelerate(); // Output: Honda is accelerating.
car.brake();      // Output: Honda is braking.

Conclusion

​Objects are central to object-oriented programming because they enable developers to model real-world entities in software. Understanding identity, state, and behavior helps structure and organize code in a way that is closer to how things exist in the natural world. This approach simplifies problem-solving and makes applications easier to design, maintain, and extend.

Analogy with "blueprints"

In software development, classes can be compared to architectural blueprints used to build structures. Just as a blueprint provides a detailed design of a building (like its rooms, dimensions, and materials), a class serves as a design template for creating objects in programming.
For instance, consider the Car class. This class outlines the attributes (e.g., brand, color, maxSpeed) and behaviors (e.g., start(), accelerate(), brake()) that any car object should have. However, the class itself is not an actual car—it's merely the specification. When you use the class to create an object, like a specific car with a brand "Toyota" and a red color, you're bringing the blueprint to life, much like constructing a building from its design.
This analogy makes it easier to understand why we create classes: they are the foundational designs that enable us to build multiple, distinct objects with shared characteristics.

A call to action

Try it yourself!
Now that you’ve learned the basics of classes and objects, why not put it into practice? Create a Car class with the following properties:
Add behaviors such as:
Then, create two car objects: one red Toyota and one blue Honda. Test their methods by simulating actions like starting, accelerating, and braking. This exercise will help solidify your understanding of how classes and objects work in practice.
​For the next lesson, we’ll dive deeper into the constructors and encapsulation, exploring how to initialize objects effectively and protect their data. Keep experimenting!

Real-World Applications

Object-Oriented Programming (OOP) is a cornerstone of modern software development and is widely used across various industries to create scalable, maintainable, and intuitive systems. Here are some examples of its real-world applications:
By using OOP, these industries create software that is easier to understand, modify, and expand, reflecting the natural way we interact with the world.

​Next
​​Classes and Objects in Java​​>|