Object-Oriented Programming in Java

Introduction

From this chapter onward, you move from procedural programs (one main method doing everything) to object-oriented design. Java is an object-oriented language: real projects organize code into classes and objects with clear responsibilities.

This chapter explains the three pillars—encapsulation, inheritance, and polymorphism—and how they fit the chapters ahead. You will not implement every detail here; you will build them step by step.

Prerequisites

Procedural vs Object-Oriented

Procedural styleObject-oriented style
One long mainMany classes, each with a role
Data and logic scatteredData + behavior grouped in objects
Hard to reuseReuse via classes and inheritance

You already used a taste of OOP with record Student in Ranking Game. Now you learn the full model.

Class and Object

  • Class: blueprint (design template)
  • Object: instance created from that class (one concrete thing)

Metaphor:

  • Class = cookie cutter
  • Object = each cookie cut from it
java
// Blueprint
public class Dog {
    String name;
 
    void bark() {
        System.out.println(name + " says: Woof!");
    }
}
 
// Instances
Dog dog1 = new Dog();
dog1.name = "Buddy";
dog1.bark();
 
Dog dog2 = new Dog();
dog2.name = "Luna";
dog2.bark();

Detailed syntax: Classes. Methods first: Methods.

Pillar 1: Encapsulation

Encapsulation hides internal details and exposes a controlled API.

  • Fields are often private
  • Other code uses public methods (getters/setters or behavior methods)
  • Protects invariants (e.g. balance never negative)
java
public class BankAccount {
    private double balance;
 
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
 
    public double getBalance() {
        return balance;
    }
}

Access modifiers are covered in Packages.

Pillar 2: Inheritance

Inheritance lets a child class reuse and extend a parent class.

java
public class Animal {
    public void eat() {
        System.out.println("Eating...");
    }
}
 
public class Cat extends Animal {
    public void meow() {
        System.out.println("Meow!");
    }
}

Benefits: less duplication, clearer hierarchies (EmployeeManager). Full chapter: Inheritance, Abstract Classes.

Pillar 3: Polymorphism

Polymorphism (“many forms”) means one reference can point to different concrete types, and behavior can vary at runtime.

java
Animal animal = new Cat();
animal.eat();  // if eat() is overridden, Cat's version may run

Two forms you will use:

How the OOP Chapters Fit Together

text
21 OOP overview (this chapter)
22 Methods — parameters, return, overload, static
23 Classes — fields, constructors, objects
24 Generics — type-safe containers
25 Enums — fixed constant sets
26 Inheritance — extends, super, override
27 Abstract classes — shared partial blueprints
28 Object class — equals, hashCode, toString
29 Packages — organize code, access control
30 Interfaces — contracts, multiple implementation
31 Polymorphism — upcast, downcast, instanceof
32 Annotations — metadata (@Override, etc.)
33 Functional programming — Lambda, Stream

Then Collections uses interfaces and generics heavily.

Why OOP Matters in Real Projects

  • Model domain concepts: Order, User, Payment
  • Team boundaries: one class per file, clear APIs
  • Frameworks expect it: Spring, Android SDK, JUnit
  • Testability: test one class in isolation

Common Beginner Mistakes

Putting Everything in main

Refactor into classes as soon as a program has distinct nouns (User, Game, Report).

Public Fields Everywhere

Prefer encapsulation with private fields and intentional public methods.

Deep Inheritance Trees Too Early

Favor composition (“has-a”) when inheritance (“is-a”) does not fit—advanced topic, but worth noting early.

What’s Next

Learn reusable behavior in Methods, then full class design in Classes.

FAQ

Is OOP the only way to write Java?

Java supports multiple styles, but application code is overwhelmingly class-based. Scripts and tiny tools can stay procedural longer; products grow into OOP quickly.

Do I need to master all three pillars immediately?

No. Learn encapsulation with classes first, then inheritance, then polymorphism—matching the chapter order.

A record is a compact class for immutable data carriers. Full classes offer more control (constructors, mutability, hierarchy).

When does polymorphism appear in daily coding?

Whenever you use an interface type (List<String> list = new ArrayList<>();) or override methods in a hierarchy.

Can I skip OOP and only learn collections?

Not recommended. Collections and frameworks assume you understand classes, interfaces, and generics.