Fundamentals 12 min read

Understanding Object‑Oriented Programming: Classes and Objects in Java

This article explains the core concepts of object‑oriented programming for Java beginners, covering the relationship between classes and objects, the four OOP principles, comparisons with procedural design, memory layout, and includes practical code examples to illustrate how to model real‑world entities in software.

Java Captain
Java Captain
Java Captain
Understanding Object‑Oriented Programming: Classes and Objects in Java

For beginners who are new to Java or any object‑oriented language, the ideas of classes and objects can be confusing; the author shares personal teaching experience and outlines a clear way to think about object‑oriented programming (OOP).

Object‑Oriented

According to a common definition, everything can be treated as an object: real‑world entities are abstracted into objects, and their relationships become classes and inheritance, enabling digital modeling of the real world.

Programming should mirror real‑world thinking; OOP is not just about writing code but also about designing solutions that are intuitive and easier to understand.

Why OOP Mirrors Real Life

Describing an animal with statements like “it has a duck‑like beak” (HAS‑A) or “it is a mammal” (IS‑A) efficiently captures attributes and classification. Combining these attributes forms a class (e.g., the platypus), while inheritance and polymorphism express shared traits and variations.

The four fundamental OOP features are listed:

Abstraction

Encapsulation

Inheritance

Polymorphism

Comparison with Early Structured Programming

Structured (procedural) programming treats a program as a collection of functions, whereas OOP treats objects as the primary entities and the program as a composition of interacting objects.

For example, designing a Gomoku game procedurally would involve a linear list of steps (start game, draw board, check win, repeat). In an OOP design the game is split into objects such as Player , Table , and Referee , each responsible for specific behavior.

/**
 * Player class
 */
public class Player {
    String name;               // player name
    boolean isFirst;           // is first mover
    int color_flag;            // 0‑white, 1‑black
    Table table;               // reference to board

    public Player(String name, boolean isFirst, int color_flag) {
        this.name = name;
        this.isFirst = isFirst;
        this.color_flag = color_flag;
    }

    /**
     * Play a move at (x, y)
     */
    public void play(int x, int y) throws Exception {
        if (this.table == null) {
            throw new IllegalArgumentException("Player not registered to a board!");
        }
        table.setNewPieces(x, y);
    }

    public void setTable(Table table) {
        this.table = table;
    }
}

/**
 * Table class
 */
public class Table {
    List
playerList = new ArrayList
();
    Referee referee;
    public Table() {
        referee = new Referee(this);
    }
    public void registPlayer(Player player) throws Exception {
        // ... check capacity, first‑move conflicts, etc.
        playerList.add(player);
        player.setTable(this);
    }
    public void setNewPieces(int x, int y) {
        // redraw board, invoke referee, etc.
        if (referee.isEnd()) {
            End();
        }
    }
    public void End() { /* ... */ }
}

/**
 * Referee class
 */
public class Referee {
    Table table;
    public Referee(Table table) { this.table = table; }
    public boolean isEnd() {
        // determine win/lose
        return false;
    }
}

Even though the same game logic is implemented, OOP encapsulates the steps inside class methods, highlighting that the difference lies in design rather than the amount of code.

Class and Object

A class is abstract; an object is concrete.

A class defines a template of attributes and behaviors (e.g., the concept of a mammal), while an object is a specific instance created with new (e.g., a tiger).

In Java, primitive types (int, boolean, etc.) are stored directly on the stack, whereas reference types (objects) store a reference on the stack and the actual object on the heap.

Memory Model (Stack vs. Heap)

Variables of primitive types and object references reside in the stack; the objects themselves reside in the heap. For example:

int a = 1;
Person p = new Person();

Here a holds the value 1 on the stack, while p holds a memory address that points to a Person instance on the heap.

Conclusion

OOP is not merely a coding technique but a mindset that encompasses analysis, design, and implementation. In OOP, the fundamental unit is the object, which encapsulates data; classes serve as reusable templates that can be instantiated to create concrete objects.

The author hopes the article helps readers clarify OOP concepts and encourages further discussion.

JavaProgramming FundamentalsoopClassesobjects
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.