Java as an object-oriented language

In the real world, we naturally see our surroundings as a set of objects, each with its own properties and behavior. For example, a car has properties such as color, brand and speed, and it can perform actions such as accelerating or braking. Object-oriented programming (OOP) follows this way of thinking: it organizes software into objects that combine data (attributes) and functionality (methods).

This approach simplifies complex problems, because it lets us model software so that it reflects the natural relationships between things. The code becomes clearer, easier to split into parts and easier to reuse, which makes OOP one of the basic ways of designing reliable software systems. In Java, classes and objects let us define templates (classes) for making objects that represent things from the real world.

Example: a class Automobil (car) can have the attributes color, brand and maximum speed, and behaviors such as start() or stop(). This way of organizing keeps the code easy to understand, because the programmer thinks about concrete objects instead of abstract machine instructions.

Classes and objects in Java: comparing objects from nature with software objects
Figure 1: Classes and objects in Java: comparing objects from nature with software objects

What is an object?

Object-oriented programming organizes software so that it reflects structures from the real world, as in figure 1. Instead of dealing with machine-level instructions, we work with “objects” that represent things with certain properties and behavior.

An object can be tangible (e.g. a pencil, a keyboard) or intangible (e.g. a bank account). All objects share three characteristics:

  • Identity: each object is unique and behaves as a whole.
  • State: objects have properties that can change.
  • Behavior: objects can perform actions or respond to actions.

Classes define the shared structure and behavior of similar objects. For example, an “Account” class describes the properties and actions of all bank accounts in a system.

Figure 1 shows the connection between objects from nature and software objects. The left part of the picture shows some properties of 4 objects: a tree, two cars and an airplane. The right part of the picture shows how these objects are represented in software. What is characteristic is that the cars marked auto1 and auto2 are two different objects, each with its own part of reserved memory, but they have common properties, so we say that they belong to the same class. The class is defined only once, no matter how many objects we can classify as cars.

Example: a bank account

Object-oriented programming models things from the real world as objects with state and behavior. For example, a bank account has properties such as the balance and the account holder, and methods such as deposit or withdrawal. These objects are defined by their classes.

class BankovniRacun {
    private double stanje;

    public BankovniRacun(double pocetnoStanje) {
        stanje = pocetnoStanje;
    }

    public void uplati(double iznos) {
        // Adds the given amount to the account balance.
        stanje += iznos;
    }

    public void isplati(double iznos) {
        // A withdrawal is possible only if there is enough money in the account.
        if (iznos <= stanje) {
            stanje -= iznos;
        }
    }

    public double getStanje() {
        return stanje;
    }
}

In this example BankovniRacun (bank account) is a class that defines the state (stanje, the balance) and the behavior (uplati – deposit, isplati – withdraw) of the account.

In the main class, the object racun (account) is created and used like this:

public class Main {
    public static void main(String[] args) {
        BankovniRacun racun = new BankovniRacun(500);
        racun.uplati(200);
        racun.isplati(100);
        System.out.println("Current balance: " + racun.getStanje());
    }
}

Program output:

Current balance: 600.0

The concept of an object

Objects are the basic building blocks of object-oriented programming. They represent tangible or intangible things from the real world, and they are characterized by a unique identity, state and behavior. These three properties are the basis of how objects work in software systems.

1. Identity

Identity is the uniqueness of an object, what tells it apart from all other objects, even when they have the same attribute values. Identity represents a specific instance of an object in memory.

Example: imagine two cars with the same properties (brand, color, speed). Although they are completely alike, they are two different objects, because they occupy different places in memory.

Automobil auto1 = new Automobil("Red", "Toyota", 180);
Automobil auto2 = new Automobil("Red", "Toyota", 180);
// auto1 and auto2 are two different objects

2. State

The state of an object is determined by its attributes and their current values. It is the data the object holds at a given moment.

Example: a car object can have the attributes color, brand and maximum speed, which make up its state.

class Automobil {
    String boja;
    String marka;
    int maksBrzina;

    Automobil(String boja, String marka, int maksBrzina) {
        this.boja = boja;
        this.marka = marka;
        this.maksBrzina = maksBrzina;
    }
}

// in the main method:
Automobil auto = new Automobil("Blue", "BMW", 240);
System.out.println("State of the car: " + auto.boja + ", " + auto.marka + ", " + auto.maksBrzina + " km/h");

3. Behavior

Behavior determines what an object can do and how it reacts to certain actions. In a class, behavior is written in the form of methods.

Example: a car object can have the methods ubrzaj() (accelerate) and koci() (brake), which determine its behavior.

class Automobil {
    String boja;
    String marka;
    int maksBrzina;

    Automobil(String boja, String marka, int maksBrzina) {
        this.boja = boja;
        this.marka = marka;
        this.maksBrzina = maksBrzina;
    }

    void ubrzaj() {
        System.out.println(marka + " is accelerating.");
    }

    void koci() {
        System.out.println(marka + " is braking.");
    }
}

// in the main method:
Automobil auto = new Automobil("Red", "Honda", 200);
auto.ubrzaj(); // Output: Honda is accelerating.
auto.koci();   // Output: Honda is braking.

Conclusion

Objects are at the center of object-oriented programming because they let us represent things from the real world in software. Understanding identity, state and behavior helps us organize code in a way that is closer to how things exist in nature. This approach makes solving problems easier, and applications become easier to design, maintain and extend.

The “blueprint” analogy

In software development, classes can be compared to the architectural blueprints used to build buildings. Just as a blueprint gives a detailed description of a building (rooms, dimensions, materials), a class serves as a template for making objects in a program.

Let's look at the class Automobil. It describes the attributes (e.g. brand, color, maxSpeed) and behaviors (e.g. start(), accelerate(), brake()) that every car object should have. However, the class itself is not a real car – it is only a description. When we make an object from the class, for example a specific red “Toyota”, the blueprint “comes to life”, much like a building constructed from its blueprint.

This analogy helps us understand why we make classes: they are the basic descriptions from which we can make many different objects with shared properties.

Try it yourself

Now that you know the basics of classes and objects, try them out in practice. Make a class Automobil with the following properties:

  • brand (e.g. "Toyota")
  • color (e.g. "Red")
  • maxSpeed (e.g. 200 km/h).

Add the behaviors:

  • accelerate() – increases the speed,
  • brake() – decreases the speed,
  • start() – simulates turning the car on.

Then make two objects: a red Toyota and a blue Honda. Try their methods by simulating starting, accelerating and braking. This exercise will help you better understand how classes and objects work in practice.

In the next lessons we look more closely at constructors and encapsulation: how to give objects starting values and how to protect their data.

Real-world applications

Object-oriented programming is one of the foundations of modern software development, and it is used in many areas to build systems that are easy to extend, maintain and understand. Here are a few examples:

  • Video game development: objects represent characters, environments and the rules of the game. For example, a “Player” class can have properties such as health and score, and actions such as jump or attack. Game engines such as Unity rely heavily on OOP.
  • Financial software: banking systems represent accounts, transactions and users as objects. For example, a “Transaction” object can hold the amount, the date and the type of change, so the system is easier to extend and errors are easier to find.
  • Online shops: products, categories and customer actions are organized with objects. A “Product” class can have the attributes name, price and quantity in stock, and methods for adding to the cart or calculating discounts.
  • Healthcare systems: medical software represents patients, appointments and records as objects. This division makes it easier to add new features, such as remote consultations.
  • Automotive industry: vehicle simulation software and self-driving systems represent the engine, sensors and navigation as objects.

By using OOP, these areas get software that is easier to understand, change and extend, and that reflects the natural way we experience the world.

Check your knowledge

Open the quiz

Quiz: Java as an object-oriented language

1. What three characteristics does every object have?




2. What is a class?




3. Two objects auto1 and auto2 have the same attribute values. Are they the same object?




4. How is the behavior of an object written in a class?